1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Entity; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Countable; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Immutable set of ItemId objects. Unordered and unique. |
12
|
|
|
* The constructor filters out duplicates. |
13
|
|
|
* |
14
|
|
|
* @since 0.7.4 |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0-or-later |
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
18
|
|
|
*/ |
19
|
|
|
class ItemIdSet implements IteratorAggregate, Countable { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ItemId[] |
23
|
|
|
*/ |
24
|
|
|
private $ids = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ItemId[] $ids |
28
|
|
|
* |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
|
|
public function __construct( array $ids = [] ) { |
32
|
|
|
foreach ( $ids as $id ) { |
33
|
14 |
|
if ( !( $id instanceof ItemId ) ) { |
34
|
14 |
|
throw new InvalidArgumentException( 'Every element in $ids must be an instance of ItemId' ); |
35
|
10 |
|
} |
36
|
|
|
|
37
|
|
|
$this->ids[$id->getNumericId()] = $id; |
38
|
|
|
} |
39
|
10 |
|
} |
40
|
14 |
|
|
41
|
14 |
|
/** |
42
|
|
|
* @see Countable::count |
43
|
|
|
* |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function count() { |
47
|
|
|
return count( $this->ids ); |
48
|
2 |
|
} |
49
|
2 |
|
|
50
|
|
|
/** |
51
|
|
|
* @see IteratorAggregate::getIterator |
52
|
|
|
* |
53
|
|
|
* @return Iterator|ItemId[] |
54
|
|
|
*/ |
55
|
|
|
public function getIterator() { |
56
|
|
|
return new ArrayIterator( $this->ids ); |
57
|
3 |
|
} |
58
|
3 |
|
|
59
|
|
|
/** |
60
|
|
|
* @since 2.5 |
61
|
|
|
* |
62
|
|
|
* @return string[] |
63
|
|
|
*/ |
64
|
|
|
public function getSerializations() { |
65
|
|
|
return array_values( |
66
|
3 |
|
array_map( |
67
|
3 |
|
function( ItemId $id ) { |
68
|
3 |
|
return $id->getSerialization(); |
69
|
3 |
|
}, |
70
|
2 |
|
$this->ids |
71
|
3 |
|
) |
72
|
3 |
|
); |
73
|
3 |
|
} |
74
|
3 |
|
|
75
|
|
|
/** |
76
|
|
|
* @param ItemId $id |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function has( ItemId $id ) { |
81
|
|
|
return array_key_exists( $id->getNumericId(), $this->ids ); |
82
|
2 |
|
} |
83
|
2 |
|
|
84
|
|
|
/** |
85
|
|
|
* @see Countable::equals |
86
|
|
|
* |
87
|
|
|
* @since 0.1 |
88
|
|
|
* |
89
|
|
|
* @param mixed $target |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function equals( $target ) { |
94
|
|
|
if ( $this === $target ) { |
95
|
7 |
|
return true; |
96
|
7 |
|
} |
97
|
3 |
|
|
98
|
|
|
return $target instanceof self |
99
|
|
|
&& $this->ids == $target->ids; |
100
|
|
|
} |
101
|
7 |
|
|
102
|
|
|
} |
103
|
|
|
|