1
|
|
|
<?php declare(strict_types=1); |
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 mixed[] |
14
|
|
|
*/ |
15
|
|
|
protected $values = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed[] $collection |
19
|
|
|
*/ |
20
|
63 |
|
public function __construct(array $collection = []) |
21
|
|
|
{ |
22
|
63 |
|
$this->values = $collection; |
23
|
63 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
*/ |
30
|
46 |
|
public function get(string $name) |
31
|
|
|
{ |
32
|
46 |
|
if ($this->has($name)) { |
33
|
41 |
|
return $this->values[$name]; |
34
|
|
|
} else { |
35
|
5 |
|
return $this->throwNotFound($name); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
58 |
|
public function has(string $name): bool |
43
|
|
|
{ |
44
|
58 |
|
return array_key_exists($name, $this->values); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
56 |
|
public function set(string $name, $object) |
51
|
|
|
{ |
52
|
56 |
|
$this->values[$name] = $object; |
53
|
56 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
31 |
|
public function getIterator() |
59
|
|
|
{ |
60
|
31 |
|
return new \ArrayIterator($this->values); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
23 |
|
public function offsetExists($offset): bool |
67
|
|
|
{ |
68
|
23 |
|
return $this->has($offset); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
30 |
|
public function offsetGet($offset) |
75
|
|
|
{ |
76
|
30 |
|
return $this->get($offset); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
42 |
|
public function offsetSet($offset, $value) |
83
|
|
|
{ |
84
|
42 |
|
$this->set($offset, $value); |
85
|
42 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function offsetUnset($offset) |
91
|
|
|
{ |
92
|
1 |
|
unset($this->values[$offset]); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function count(): int |
99
|
|
|
{ |
100
|
1 |
|
return count($this->values); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed[] |
105
|
|
|
*/ |
106
|
3 |
|
public function select(callable $callback): array |
107
|
|
|
{ |
108
|
3 |
|
$values = []; |
109
|
|
|
|
110
|
3 |
|
foreach ($this as $key => $value) { |
111
|
3 |
|
if ($callback($value, $key)) { |
112
|
3 |
|
$values[$key] = $value; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
return $values; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
* @throws \InvalidArgumentException |
122
|
|
|
*/ |
123
|
|
|
public function first() |
124
|
|
|
{ |
125
|
|
|
if ($this->count() === 0) { |
126
|
|
|
throw new \InvalidArgumentException("no elements found in collection."); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return array_values($this->values)[0]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return mixed[] |
134
|
|
|
*/ |
135
|
|
|
public function toArray(): array |
136
|
|
|
{ |
137
|
|
|
return iterator_to_array($this); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
protected function throwNotFound(string $name) |
141
|
|
|
{ |
142
|
1 |
|
throw new \InvalidArgumentException("`$name` not found in collection."); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|