|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\API\Collection; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Implements a 'typed array' structure |
|
7
|
|
|
*/ |
|
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
|
83 |
|
public function __construct($input = array(), $flags = 0, $iterator_class = "ArrayIterator") |
|
19
|
|
|
{ |
|
20
|
83 |
|
foreach ($input as $value) { |
|
21
|
79 |
|
if (!$this->isValidElement($value)) { |
|
22
|
79 |
|
$this->throwInvalid($value); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
82 |
|
parent::__construct($input, $flags, $iterator_class); |
|
27
|
82 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param mixed $value |
|
31
|
|
|
*/ |
|
32
|
|
|
public function append($value) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$this->isValidElement($value)) { |
|
35
|
|
|
$this->throwInvalid($value); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
parent::append($value); |
|
39
|
|
|
} |
|
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
|
83 |
|
protected function isValidElement($value) |
|
70
|
|
|
{ |
|
71
|
83 |
|
return is_a($value, $this->allowedClass); |
|
72
|
|
|
} |
|
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) . "'"); |
|
77
|
|
|
} |
|
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) |
|
85
|
|
|
{ |
|
86
|
|
|
return new static($data); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|