1 | <?php |
||
2 | /** |
||
3 | * Collection. |
||
4 | * |
||
5 | * @package WPSteak |
||
6 | */ |
||
7 | |||
8 | declare(strict_types=1); |
||
9 | |||
10 | namespace WPSteak\Entities; |
||
11 | |||
12 | /** |
||
13 | * Collection class. |
||
14 | */ |
||
15 | class Collection implements CollectionInterface { |
||
16 | |||
17 | use \DusanKasan\Knapsack\CollectionTrait; |
||
18 | |||
19 | /** |
||
20 | * Pool. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private $pool = []; |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
25 | |||
26 | /** |
||
27 | * Position. |
||
28 | * |
||
29 | * @var integer |
||
30 | */ |
||
31 | private $position = 0; |
||
32 | |||
33 | /** |
||
34 | * Add entity. |
||
35 | * |
||
36 | * @param EntityInterface $entity Entity. |
||
37 | * @param mixed $key Key. |
||
38 | * @return void |
||
39 | */ |
||
40 | 7 | public function add_entity( EntityInterface $entity, $key = null ) { |
|
41 | 7 | if ( is_null( $key ) ) { |
|
42 | 6 | $this->pool[] = $entity; |
|
43 | 6 | return; |
|
44 | } |
||
45 | |||
46 | 1 | $this->pool[ $key ] = $entity; |
|
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * Current. |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | 1 | public function current() { |
|
55 | 1 | return $this->pool[ $this->position ]; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Key. |
||
60 | * |
||
61 | * @return integer |
||
62 | */ |
||
63 | 1 | public function key() { |
|
64 | 1 | return $this->position; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Next position. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | 1 | public function next() { |
|
73 | 1 | ++$this->position; |
|
74 | 1 | } |
|
75 | |||
76 | /** |
||
77 | * Rewind position. |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | 1 | public function rewind() { |
|
82 | 1 | $this->position = 0; |
|
83 | 1 | } |
|
84 | |||
85 | /** |
||
86 | * Is a valid position. |
||
87 | * |
||
88 | * @return boolean |
||
89 | */ |
||
90 | 1 | public function valid() { |
|
91 | 1 | return isset( $this->pool[ $this->position ] ); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Offset exists. |
||
96 | * |
||
97 | * @param mixed $offset Offset. |
||
98 | * @return boolean |
||
99 | */ |
||
100 | 1 | public function offsetExists( $offset ) { |
|
101 | 1 | return isset( $this->pool[ $offset ] ); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Offset get. |
||
106 | * |
||
107 | * @param mixed $offset Offset. |
||
108 | * @return mixed|null |
||
109 | */ |
||
110 | 5 | public function offsetGet( $offset ) { |
|
111 | 5 | if ( isset( $this->pool[ $offset ] ) ) { |
|
112 | 5 | return $this->pool[ $offset ]; |
|
113 | } |
||
114 | |||
115 | 2 | return null; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Offset set. |
||
120 | * |
||
121 | * @param mixed $offset Offset. |
||
122 | * @param mixed $value Value. |
||
123 | * @return void |
||
124 | */ |
||
125 | 5 | public function offsetSet( $offset, $value ) { |
|
126 | 5 | $this->add_entity( $value, $offset ); |
|
127 | 5 | } |
|
128 | |||
129 | /** |
||
130 | * Offset unset. |
||
131 | * |
||
132 | * @param mixed $offset Offset. |
||
133 | * @return void |
||
134 | */ |
||
135 | 1 | public function offsetUnset( $offset ) { |
|
136 | 1 | unset( $this->pool[ $offset ] ); |
|
137 | 1 | } |
|
138 | |||
139 | /** |
||
140 | * Total count. |
||
141 | * |
||
142 | * @return integer |
||
143 | */ |
||
144 | 1 | public function count() : int { |
|
145 | 1 | return count( $this->pool ); |
|
146 | } |
||
147 | } |
||
148 |