1
|
|
|
<?php |
2
|
|
|
namespace Jmw\Collection\Set; |
3
|
|
|
|
4
|
|
|
use Jmw\Collection\Lists\Tuple; |
5
|
|
|
use Jmw\Collection\Exception\InvalidTypeException; |
6
|
|
|
use Jmw\Collection\Lists\ArrayList; |
7
|
|
|
/** |
8
|
|
|
* A set of Tuple elements, which ensures that only Tuple elements |
9
|
|
|
* may be added to the set, and no Tuple element may be duplicated |
10
|
|
|
* @author john |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class TupleSet extends SetAbstract |
14
|
|
|
{ |
15
|
|
|
protected $arrayList; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Construct a TupleSet from an array |
19
|
|
|
* Each key=>value pair will become a Tuple<key, value> |
20
|
|
|
* @param unknown $array |
21
|
|
|
*/ |
22
|
|
|
public function __construct($array) |
23
|
|
|
{ |
24
|
|
|
$this->clear(); |
25
|
|
|
foreach ($array as $value1=>$value2) |
26
|
|
|
{ |
27
|
|
|
$this->add(new Tuple($value1, $value2)); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ensures that this collection contains the specified element (optional operation). |
33
|
|
|
* @param multitype $element |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
|
|
public function add($element) |
37
|
|
|
{ |
38
|
|
|
$changed = false; |
39
|
|
|
if($element instanceof Tuple) |
40
|
|
|
{ |
41
|
|
|
if(!$this->contains($element)) |
42
|
|
|
{ |
43
|
|
|
$this->arrayList->add($element); |
44
|
|
|
$changed = true; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
else |
48
|
|
|
{ |
49
|
|
|
throw new InvalidTypeException('Jmw\Collection\Lists\Tuple', gettype($element)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
$changed; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Removes all of the elements from this collection (optional operation). |
56
|
|
|
*/ |
57
|
|
|
public function clear() |
58
|
|
|
{ |
59
|
|
|
$this->arrayList = new ArrayList(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns an iterator over the elements in this collection. |
64
|
|
|
* @return IteratorInterface |
65
|
|
|
*/ |
66
|
|
|
public function iterator() |
67
|
|
|
{ |
68
|
|
|
return new SetIterator($this); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Removes a single instance of the specified element from this collection, if it is present (optional operation). |
73
|
|
|
* @param boolean $changed |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public function remove($tuple) |
76
|
|
|
{ |
77
|
|
|
$iterator = $this->arrayList->iterator(); |
78
|
|
|
|
79
|
|
|
while($iterator->hasNext()) |
80
|
|
|
{ |
81
|
|
|
$element = $iterator->next(); |
82
|
|
|
if($element->equals($tuple)) |
83
|
|
|
{ |
84
|
|
|
$iterator->remove(); |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the number of elements in this collection. |
93
|
|
|
*/ |
94
|
|
|
public function size() |
95
|
|
|
{ |
96
|
|
|
return $this->arrayList->size(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns an array containing all of the elements in this collection. |
101
|
|
|
*/ |
102
|
|
|
public function toArray() |
103
|
|
|
{ |
104
|
|
|
return $this->arrayList->toArray(); |
105
|
|
|
} |
106
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: