|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Collections; |
|
4
|
|
|
|
|
5
|
|
|
use Collections\Iterator\SetIterator; |
|
6
|
|
|
use Collections\Traits\SetLikeTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Set is an ordered set-style collection. |
|
10
|
|
|
* |
|
11
|
|
|
* Like all objects in PHP, Sets have reference-like semantics. When a caller |
|
12
|
|
|
* passes a Set to a callee, the callee can modify the Set and the caller will |
|
13
|
|
|
* see the changes. Sets do not have "copy-on-write" semantics. |
|
14
|
|
|
* |
|
15
|
|
|
* Sets preserve insertion order of the elements. When iterating over a Set, |
|
16
|
|
|
* the elements appear in the order they were inserted. Also, Sets do not |
|
17
|
|
|
* automagically convert integer-like strings (ex. "123") into integers. |
|
18
|
|
|
* |
|
19
|
|
|
* Sets only support integer values and string values. If a value of a |
|
20
|
|
|
* different type is used, an exception will be thrown. |
|
21
|
|
|
* |
|
22
|
|
|
* In general, Sets do not support "$c[$k]" style syntax. Adding an element |
|
23
|
|
|
* using "$c[] = .." syntax is supported. |
|
24
|
|
|
* |
|
25
|
|
|
* Set do not support iteration while elements are being added or removed. |
|
26
|
|
|
* When an element is added or removed, all iterators that point to the Set |
|
27
|
|
|
* shall be considered invalid. |
|
28
|
|
|
* |
|
29
|
|
|
* Sets do not support taking elements by reference. If binding assignment (=&) |
|
30
|
|
|
* is used when adding a new element to a Set (ex. "$c[] =& ..."), or if a Set |
|
31
|
|
|
* is used with foreach by reference, an exception will be thrown. |
|
32
|
|
|
*/ |
|
33
|
|
|
class Set implements SetInterface, \ArrayAccess |
|
34
|
|
|
{ |
|
35
|
|
|
use SetLikeTrait; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($array = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->init($array); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritDoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getIterator() |
|
46
|
|
|
{ |
|
47
|
|
|
return new SetIterator($this->container); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|