1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Collection; |
9
|
|
|
|
10
|
|
|
class Collection implements CollectionInterface, \Countable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $collection = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Collection constructor. |
19
|
|
|
* @param array $collection |
20
|
50 |
|
*/ |
21
|
|
|
public function __construct(array $collection = []) |
22
|
50 |
|
{ |
23
|
45 |
|
$this->collection = $collection; |
24
|
|
|
} |
25
|
5 |
|
|
26
|
5 |
|
/** |
27
|
5 |
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function get($name) |
30
|
|
|
{ |
31
|
|
|
if ($this->has($name)) { |
32
|
|
|
if (strpos($name, '.') === false) { |
33
|
|
|
return $this->collection[$name]; |
34
|
51 |
|
} else { |
35
|
|
|
$parts = explode('.', $name); |
36
|
51 |
|
$scope = &$this->collection; |
37
|
|
|
$count = count($parts) - 1; |
38
|
|
|
for ($i = 0; $i < $count; $i++) { |
39
|
|
|
if (!isset($scope[$parts[$i]])) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
46 |
|
$scope = &$scope[$parts[$i]]; |
43
|
|
|
} |
44
|
46 |
|
return isset($scope[$parts[$i]]) ? $scope[$parts[$i]] : null; |
45
|
46 |
|
} |
46
|
|
|
} else { |
47
|
|
|
$class = explode('\\', static::class); |
48
|
|
|
$class = end($class); |
49
|
|
|
throw new \RuntimeException("Object `$name` does not exist in $class."); |
50
|
25 |
|
} |
51
|
|
|
} |
52
|
25 |
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function has($name) |
57
|
|
|
{ |
58
|
1 |
|
if (strpos($name, '.') === false) { |
59
|
|
|
return array_key_exists($name, $this->collection); |
60
|
1 |
|
} else { |
61
|
|
|
list($head,) = explode('.', $name); |
62
|
|
|
return array_key_exists($head, $this->collection); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
40 |
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
40 |
|
*/ |
69
|
|
|
public function set($name, $object) |
70
|
|
|
{ |
71
|
|
|
$this->collection[$name] = $object; |
72
|
|
|
} |
73
|
|
|
|
74
|
41 |
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
41 |
|
*/ |
77
|
41 |
|
public function getIterator() |
78
|
|
|
{ |
79
|
|
|
return new \ArrayIterator($this->collection); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
1 |
|
*/ |
85
|
1 |
|
public function offsetExists($offset) |
86
|
|
|
{ |
87
|
|
|
return $this->has($offset); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
2 |
|
*/ |
93
|
|
|
public function offsetGet($offset) |
94
|
|
|
{ |
95
|
|
|
return $this->get($offset); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function offsetSet($offset, $value) |
102
|
|
|
{ |
103
|
|
|
$this->set($offset, $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function offsetUnset($offset) |
110
|
|
|
{ |
111
|
|
|
unset($this->collection[$offset]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function count() |
118
|
|
|
{ |
119
|
|
|
return count($this->collection); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function toArray() |
126
|
|
|
{ |
127
|
|
|
return iterator_to_array($this); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|