1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the kaloa/util package. |
5
|
|
|
* |
6
|
|
|
* For full copyright and license information, please view the LICENSE file |
7
|
|
|
* that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Kaloa\Util; |
11
|
|
|
|
12
|
|
|
use ArrayAccess; |
13
|
|
|
use ArrayIterator; |
14
|
|
|
use Countable; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use IteratorAggregate; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractSet implements ArrayAccess, Countable, IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $_managedClass = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $_container = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @param mixed $obj |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
3 |
|
public function add($obj) |
41
|
|
|
{ |
42
|
3 |
|
if (!$obj instanceof $this->_managedClass) { |
43
|
1 |
|
throw new InvalidArgumentException('Argument has to be of type "' |
44
|
1 |
|
. $this->_managedClass . '"'); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$this->offsetSet(null, $obj); |
48
|
2 |
|
} |
49
|
|
|
|
50
|
4 |
|
public function offsetSet($offset, $value) { |
51
|
4 |
|
if (!$value instanceof $this->_managedClass) { |
52
|
1 |
|
throw new InvalidArgumentException('Argument has to be of type "' |
53
|
1 |
|
. $this->_managedClass . '"'); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
if (null === $offset) { |
57
|
2 |
|
$this->_container[] = $value; |
58
|
2 |
|
} else { |
59
|
2 |
|
$this->_container[$offset] = $value; |
60
|
|
|
} |
61
|
3 |
|
} |
62
|
|
|
|
63
|
1 |
|
public function offsetExists($offset) |
64
|
|
|
{ |
65
|
1 |
|
return isset($this->_container[$offset]); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
public function offsetUnset($offset) |
69
|
|
|
{ |
70
|
2 |
|
unset($this->_container[$offset]); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function offsetGet($offset) |
74
|
|
|
{ |
75
|
1 |
|
return isset($this->_container[$offset]) |
76
|
1 |
|
? $this->_container[$offset] |
77
|
1 |
|
: null; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function getIterator() |
81
|
|
|
{ |
82
|
1 |
|
return new ArrayIterator($this->_container); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function count() |
86
|
|
|
{ |
87
|
1 |
|
return count($this->_container); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|