1 | <?php |
||
8 | class AbstractCollection extends \ArrayObject |
||
9 | { |
||
10 | protected $allowedClass; |
||
11 | |||
12 | /** |
||
13 | * AbstractCollection constructor. |
||
14 | * @param array $input |
||
15 | * @param int $flags |
||
16 | * @param string $iterator_class |
||
17 | */ |
||
18 | 77 | public function __construct($input = array(), $flags = 0, $iterator_class = "ArrayIterator") |
|
19 | { |
||
20 | 77 | foreach ($input as $value) { |
|
21 | 73 | if (!$this->isValidElement($value)) { |
|
22 | 73 | $this->throwInvalid($value); |
|
23 | } |
||
24 | } |
||
25 | |||
26 | 76 | parent::__construct($input, $flags, $iterator_class); |
|
27 | 76 | } |
|
28 | |||
29 | /** |
||
30 | * @param mixed $value |
||
31 | */ |
||
32 | public function append($value) |
||
40 | |||
41 | /** |
||
42 | * @param mixed $input |
||
43 | * @return array the old array |
||
44 | */ |
||
45 | 1 | public function exchangeArray($input) |
|
46 | { |
||
47 | 1 | foreach ($input as $value) { |
|
48 | 1 | if (!$this->isValidElement($value)) { |
|
49 | 1 | $this->throwInvalid($value); |
|
50 | } |
||
51 | } |
||
52 | |||
53 | 1 | return parent::exchangeArray($input); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param mixed $index |
||
58 | * @param mixed $value |
||
59 | */ |
||
60 | 19 | public function offsetSet($index, $value) |
|
61 | { |
||
62 | 19 | if (!$this->isValidElement($value)) { |
|
63 | 2 | $this->throwInvalid($value); |
|
64 | } |
||
65 | |||
66 | 17 | parent::offsetSet($index, $value); |
|
67 | 17 | } |
|
68 | |||
69 | 77 | protected function isValidElement($value) |
|
73 | |||
74 | 4 | protected function throwInvalid($value) |
|
75 | { |
||
76 | 4 | throw new \InvalidArgumentException("Can not add element of type '" . (is_object($value) ? get_class($value) : gettype($value)) . "' to Collection of type '" . get_class($this) . "'"); |
|
78 | |||
79 | /** |
||
80 | * Allow the class to be serialized to php using var_export |
||
81 | * @param array $data |
||
82 | * @return static |
||
83 | */ |
||
84 | public static function __set_state(array $data) |
||
88 | } |
||
89 |