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
|
|
|
return $this->collection[$name]; |
33
|
|
|
} else { |
34
|
51 |
|
$class = explode('\\', static::class); |
35
|
|
|
$class = end($class); |
36
|
51 |
|
throw new \RuntimeException("Object `$name` does not exist in $class."); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
46 |
|
*/ |
43
|
|
|
public function has($name) |
44
|
46 |
|
{ |
45
|
46 |
|
return array_key_exists($name, $this->collection); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
25 |
|
*/ |
51
|
|
|
public function set($name, $object) |
52
|
25 |
|
{ |
53
|
|
|
$this->collection[$name] = $object; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
1 |
|
*/ |
59
|
|
|
public function getIterator() |
60
|
1 |
|
{ |
61
|
|
|
return new \ArrayIterator($this->collection); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
40 |
|
*/ |
67
|
|
|
public function offsetExists($offset) |
68
|
40 |
|
{ |
69
|
|
|
return $this->has($offset); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
41 |
|
*/ |
75
|
|
|
public function offsetGet($offset) |
76
|
41 |
|
{ |
77
|
41 |
|
return $this->get($offset); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
1 |
|
*/ |
83
|
|
|
public function offsetSet($offset, $value) |
84
|
1 |
|
{ |
85
|
1 |
|
$this->set($offset, $value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
2 |
|
*/ |
91
|
|
|
public function offsetUnset($offset) |
92
|
2 |
|
{ |
93
|
|
|
unset($this->collection[$offset]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function count() |
100
|
|
|
{ |
101
|
|
|
return count($this->collection); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function toArray() |
108
|
|
|
{ |
109
|
|
|
return iterator_to_array($this); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|