-
Notifications
You must be signed in to change notification settings - Fork 0
/
BindingCollection.php
152 lines (138 loc) · 3.21 KB
/
BindingCollection.php
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Binding Collection class
*
* @package SOW\BindingBundle
* @author Thomas LEDUC <[email protected]>
* @link https://github.com/SonOfWinter/BindingBundle
*/
namespace SOW\BindingBundle;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use Symfony\Component\Config\Resource\ResourceInterface;
use Traversable;
/**
* Class BindingCollection
*
* @package SOW\BindingBundle
*/
class BindingCollection implements IteratorAggregate, Countable
{
/**
* @var Binding[]
*/
private array $bindings = [];
/**
* @var array
*/
private array $resources = [];
/**
* @return ArrayIterator|Traversable
*/
public function getIterator(): Traversable | ArrayIterator
{
return new ArrayIterator($this->bindings);
}
/**
* Return number of binding element
*
* @return int
*/
public function count(): int
{
return count($this->bindings);
}
/**
* Add a binding element
*
* @param Binding $binding
*
* @return void
*/
public function addBinding(Binding $binding): void
{
if (array_key_exists($binding->getKey(), $this->bindings)) {
unset($this->bindings[$binding->getKey()]);
}
$this->bindings[$binding->getKey()] = $binding;
}
/**
* Get all binding elements
*
* @return array
*/
public function all(): array
{
return $this->bindings;
}
/**
* Get a binding element by key
*
* @param $key
*
* @return null|Binding
*/
public function get($key): ?Binding
{
return $this->bindings[$key] ?? null;
}
/**
* Remove a binding element by key
*
* @param string|string[] $key The binding key or an array of binding keys
*
* @return void
*/
public function remove(string|array $key): void
{
if (!empty($key)) {
foreach ((array)$key as $k) {
unset($this->bindings[$k]);
}
}
}
/**
* Merge the collection with a new collection
*
* @param BindingCollection $collection
*
* @return void
*/
public function mergeCollection(BindingCollection $collection): void
{
foreach ($collection->all() as $key => $binding) {
if (array_key_exists($key, $this->bindings)) {
unset($this->bindings[$key]);
}
$this->bindings[$key] = $binding;
}
foreach ($collection->getResources() as $resource) {
$this->addResource($resource);
}
}
/**
* Returns an array of resources loaded to build this collection.
*
* @return array
*/
public function getResources(): array
{
return array_values($this->resources);
}
/**
* Adds a resource for this collection. If the resource already exists
* it is not added.
*
* @param ResourceInterface $resource
*
* @return void
*/
public function addResource(ResourceInterface $resource): void
{
$key = (string) $resource;
if (!isset($this->resources[$key])) {
$this->resources[$key] = $resource;
}
}
}