1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CRM library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\Crm; |
7
|
|
|
|
8
|
|
|
use Slince\Crm\Exception\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class RegistryCollection implements \IteratorAggregate, \ArrayAccess |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Registry[] |
14
|
|
|
*/ |
15
|
|
|
protected $registries = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* RegistryCollection constructor. |
19
|
|
|
* @param array $registries |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $registries = []) |
22
|
|
|
{ |
23
|
|
|
$this->registries = $registries; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add a registry |
28
|
|
|
* @param Registry $registry |
29
|
|
|
*/ |
30
|
|
|
public function add(Registry $registry) |
31
|
|
|
{ |
32
|
|
|
$this->registries[] = $registry; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add a registry |
37
|
|
|
* @param Registry $registry |
38
|
|
|
*/ |
39
|
|
|
public function remove(Registry $registry) |
40
|
|
|
{ |
41
|
|
|
foreach ($this->registries as $index => $_registry) { |
42
|
|
|
if ($registry == $_registry) { |
43
|
|
|
unset($this->registries[$index]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get all registries |
50
|
|
|
* @return Registry[] |
51
|
|
|
*/ |
52
|
|
|
public function all() |
53
|
|
|
{ |
54
|
|
|
return $this->registries; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convert to array |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function toArray() |
62
|
|
|
{ |
63
|
|
|
return array_map(function (Registry $registry) { |
64
|
|
|
return $registry->toArray(); |
65
|
|
|
}, $this->registries); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $data |
70
|
|
|
* @throws InvalidArgumentException |
71
|
|
|
* @return static |
72
|
|
|
*/ |
73
|
|
|
public static function fromArray($data) |
74
|
|
|
{ |
75
|
|
|
return new static(array_map(function ($registryData) { |
76
|
|
|
return Registry::create($registryData); |
77
|
|
|
}, $data)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getIterator() |
84
|
|
|
{ |
85
|
|
|
return new \ArrayIterator($this->registries); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function offsetExists($offset) |
92
|
|
|
{ |
93
|
|
|
return isset($this->registries[$offset]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function offsetGet($offset) |
100
|
|
|
{ |
101
|
|
|
return $this->registries[$offset]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function offsetUnset($offset) |
108
|
|
|
{ |
109
|
|
|
unset($this->registries[$offset]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function offsetSet($offset, $value) |
116
|
|
|
{ |
117
|
|
|
$this->registries[$offset] = $value; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|