Completed
Push — master ( 34e257...1bc588 )
by
unknown
04:04
created

ItemIdSnakValue::someValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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