Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
ItemIdSnakValue
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
10 / 10
22
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromItemId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 someValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 noValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 fromSnak
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
6
 isValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSomeValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isNoValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItemId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 matchesSnak
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace WikibaseQuality\ConstraintReport\ConstraintCheck;
4
5use DomainException;
6use InvalidArgumentException;
7use Wikibase\DataModel\Entity\EntityIdValue;
8use Wikibase\DataModel\Entity\ItemId;
9use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10use Wikibase\DataModel\Snak\PropertySomeValueSnak;
11use Wikibase\DataModel\Snak\PropertyValueSnak;
12use Wikibase\DataModel\Snak\Snak;
13
14/**
15 * A value that can either be an item ID, some value (unknown value), or no value.
16 *
17 * @author Lucas Werkmeister
18 * @license GPL-2.0-or-later
19 */
20class ItemIdSnakValue {
21
22    /**
23     * @var ItemId|null
24     */
25    private $itemId = null;
26
27    /**
28     * @var bool
29     */
30    private $some = false;
31
32    /**
33     * @var bool
34     */
35    private $no = false;
36
37    private function __construct() {
38    }
39
40    /**
41     * Get an {@link ItemIdSnakValue} from the given $itemId.
42     *
43     * @param ItemId $itemId
44     * @return self
45     */
46    public static function fromItemId( ItemId $itemId ) {
47        $ret = new self;
48        $ret->itemId = $itemId;
49        return $ret;
50    }
51
52    /**
53     * Get an {@link ItemIdSnakValue} that wraps an unknown value.
54     *
55     * @return self
56     */
57    public static function someValue() {
58        $ret = new self;
59        $ret->some = true;
60        return $ret;
61    }
62
63    /**
64     * Get an {@link ItemIdSnakValue} that wraps no value.
65     *
66     * @return self
67     */
68    public static function noValue() {
69        $ret = new self;
70        $ret->no = true;
71        return $ret;
72    }
73
74    /**
75     * Get an {@link ItemIdSnakValue} that matches the given snak.
76     *
77     * @param Snak $snak
78     *
79     * @throws InvalidArgumentException
80     * @return self
81     */
82    public static function fromSnak( Snak $snak ) {
83        switch ( true ) {
84            case $snak instanceof PropertyValueSnak:
85                $dataValue = $snak->getDataValue();
86                if ( $dataValue instanceof EntityIdValue ) {
87                    $itemId = $dataValue->getEntityId();
88                    if ( $itemId instanceof ItemId ) {
89                        return self::fromItemId( $itemId );
90                    }
91                }
92                break;
93            case $snak instanceof PropertySomeValueSnak:
94                return self::someValue();
95            case $snak instanceof PropertyNoValueSnak:
96                return self::noValue();
97        }
98
99        throw new InvalidArgumentException( 'Snak must contain item ID value or be a somevalue / novalue snak' );
100    }
101
102    /**
103     * Check whether this {@link ItemIdSnakValue} contains a known value or not.
104     *
105     * @return bool
106     */
107    public function isValue() {
108        return $this->itemId !== null;
109    }
110
111    /**
112     * Check whether this {@link ItemIdSnakValue} contains an unknown value or not.
113     *
114     * @return bool
115     */
116    public function isSomeValue() {
117        return $this->some;
118    }
119
120    /**
121     * Check whether this {@link ItemIdSnakValue} contains no value or not.
122     *
123     * @return bool
124     */
125    public function isNoValue() {
126        return $this->no;
127    }
128
129    /**
130     * Get the item ID contained in this {@link ItemIdSnakValue}.
131     * Only valid if {@link isValue} is true.
132     *
133     * @throws DomainException if this value does not contain an item ID
134     * @return ItemId
135     */
136    public function getItemId() {
137        if ( !$this->isValue() ) {
138            throw new DomainException( 'This value does not contain an item ID.' );
139        }
140        return $this->itemId;
141    }
142
143    /**
144     * Check whether this value matches the given $snak
145     * (same kind and, if contains known value, same value).
146     *
147     * @param Snak $snak
148     * @return bool
149     */
150    public function matchesSnak( Snak $snak ) {
151        switch ( true ) {
152            case $snak instanceof PropertyValueSnak:
153                $dataValue = $snak->getDataValue();
154                return $this->isValue() &&
155                    $dataValue instanceof EntityIdValue &&
156                    $dataValue->getEntityId() instanceof ItemId &&
157                    $dataValue->getEntityId()->equals( $this->getItemId() );
158            case $snak instanceof PropertySomeValueSnak:
159                return $this->isSomeValue();
160            case $snak instanceof PropertyNoValueSnak:
161                return $this->isNoValue();
162        }
163    }
164
165}