forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.php
More file actions
81 lines (65 loc) · 1.67 KB
/
Copy pathCollection.php
File metadata and controls
81 lines (65 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Collection;
use Countable;
use IteratorAggregate;
class Collection implements Countable, IteratorAggregate
{
protected array $values = [];
public function all(): array
{
return $this->values;
}
public function get(string $name): mixed
{
if ($this->has($name)) {
return $this->values[$name];
}
throw $this->notFound($name);
}
public function has(string $name): bool
{
return array_key_exists($name, $this->values);
}
public function set(string $name, mixed $object)
{
$this->values[$name] = $object;
}
public function remove(string $name): void
{
if ($this->has($name)) {
unset($this->values[$name]);
}
}
public function count(): int
{
return count($this->values);
}
public function select(callable $callback): array
{
$values = [];
foreach ($this->values as $key => $value) {
if ($callback($value, $key)) {
$values[$key] = $value;
}
}
return $values;
}
/**
* @return \ArrayIterator|\Traversable
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new \ArrayIterator($this->values);
}
protected function notFound(string $name): \InvalidArgumentException
{
return new \InvalidArgumentException("Element \"$name\" not found in collection.");
}
}