1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Collection\Util\Extension; |
4
|
|
|
|
5
|
|
|
use Bdf\Collection\CollectionInterface; |
6
|
|
|
use Bdf\Collection\Stream\StreamInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait for implements CollectionInterface delegation objects |
10
|
|
|
* |
11
|
|
|
* <code> |
12
|
|
|
* class MyDelegate implements CollectionInterface |
13
|
|
|
* { |
14
|
|
|
* use CollectionDelegationTrait; |
15
|
|
|
* |
16
|
|
|
* public function __construct(CollectionInterface $collection) |
17
|
|
|
* { |
18
|
|
|
* $this->setCollection($collection); |
19
|
|
|
* } |
20
|
|
|
* } |
21
|
|
|
* </code> |
22
|
|
|
* |
23
|
|
|
* @see CollectionInterface |
24
|
|
|
*/ |
25
|
|
|
trait CollectionDelegationTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var CollectionInterface |
29
|
|
|
*/ |
30
|
|
|
private $collection; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the inner collection |
35
|
|
|
* |
36
|
|
|
* @param CollectionInterface $collection |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
12 |
|
private function setCollection(CollectionInterface $collection) |
41
|
|
|
{ |
42
|
12 |
|
$this->collection = $collection; |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @see CollectionInterface::add() |
47
|
|
|
*/ |
48
|
1 |
|
public function add($element): bool |
49
|
|
|
{ |
50
|
1 |
|
return $this->collection->add($element); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @see CollectionInterface::addAll() |
55
|
|
|
*/ |
56
|
1 |
|
public function addAll(iterable $elements): bool |
57
|
|
|
{ |
58
|
1 |
|
return $this->collection->addAll($elements); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see CollectionInterface::replace() |
63
|
|
|
*/ |
64
|
1 |
|
public function replace(iterable $elements): bool |
65
|
|
|
{ |
66
|
1 |
|
return $this->collection->replace($elements); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @see CollectionInterface::remove() |
71
|
|
|
*/ |
72
|
1 |
|
public function remove($element, bool $strict = false): bool |
73
|
|
|
{ |
74
|
1 |
|
return $this->collection->remove($element, $strict); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @see CollectionInterface::clear() |
79
|
|
|
*/ |
80
|
1 |
|
public function clear(): void |
81
|
|
|
{ |
82
|
1 |
|
$this->collection->clear(); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @see CollectionInterface::empty() |
87
|
|
|
*/ |
88
|
1 |
|
public function empty(): bool |
89
|
|
|
{ |
90
|
1 |
|
return $this->collection->empty(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see CollectionInterface::contains() |
95
|
|
|
*/ |
96
|
1 |
|
public function contains($element, bool $strict = false): bool |
97
|
|
|
{ |
98
|
1 |
|
return $this->collection->contains($element, $strict); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see CollectionInterface::toArray() |
103
|
|
|
*/ |
104
|
1 |
|
public function toArray(): array |
105
|
|
|
{ |
106
|
1 |
|
return $this->collection->toArray(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @see CollectionInterface::count() |
111
|
|
|
*/ |
112
|
1 |
|
public function count(): int |
113
|
|
|
{ |
114
|
1 |
|
return count($this->collection); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see CollectionInterface::getIterator() |
119
|
|
|
*/ |
120
|
1 |
|
public function getIterator(): \Traversable |
121
|
|
|
{ |
122
|
1 |
|
return $this->collection->getIterator(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @see CollectionInterface::forEach() |
127
|
|
|
*/ |
128
|
1 |
|
public function forEach(callable $consumer): void |
129
|
|
|
{ |
130
|
1 |
|
$this->collection->forEach($consumer); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @see CollectionInterface::stream() |
135
|
|
|
*/ |
136
|
1 |
|
public function stream(): StreamInterface |
137
|
|
|
{ |
138
|
1 |
|
return $this->collection->stream(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|