PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.1
Booking for Appointments and Events Calendar – Amelia v2.1
2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Domain / Collection / AbstractCollection.php
ameliabooking / src / Domain / Collection Last commit date
AbstractCollection.php 4 months ago Collection.php 1 year ago
AbstractCollection.php
190 lines
1 <?php
2
3 namespace AmeliaBooking\Domain\Collection;
4
5 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
6
7 /**
8 * Class AbstractCollection
9 *
10 * @package AmeliaBooking\Domain\Collection
11 */
12 class AbstractCollection
13 {
14 /** @var array|null */
15 protected $items = [];
16
17 /**
18 * AbstractCollection constructor.
19 *
20 * @param array $items
21 *
22 * @throws InvalidArgumentException
23 */
24 public function __construct($items = null)
25 {
26 if (!is_array($items)) {
27 return;
28 }
29
30 foreach ($items as $item) {
31 $this->addItem($item);
32 }
33
34 $this->items = $items;
35 }
36
37 /**
38 * Add an object to the collection by putting it in the $items array at a
39 * specified location specified by $key (if no key is provided, we let
40 * PHP pick the next available index). If an attempt is made to add an object
41 * using a key that already exists, an exception should be thrown to prevent
42 * inadvertently overwriting existing information
43 *
44 * @param $item
45 * @param null $key
46 * @param bool $force
47 *
48 * @throws InvalidArgumentException
49 */
50 public function addItem($item, $key = null, $force = false)
51 {
52 if ($key !== null) {
53 $this->placeItem($item, $key, $force);
54
55 return;
56 }
57
58 $this->items[] = $item;
59 }
60
61 /**
62 * Take the key as a parameter indicating which items are targeted for removal.
63 *
64 * @param $key
65 *
66 * @throws InvalidArgumentException
67 */
68 public function deleteItem($key)
69 {
70 if (!$this->keyExists($key)) {
71 throw new InvalidArgumentException("Invalid key {$key}.");
72 }
73
74 unset($this->items[$key]);
75 }
76
77 /**
78 * Take the key as a parameter indicating which items are targeted for retrieval.
79 *
80 * @param $key
81 *
82 * @return mixed
83 * @throws InvalidArgumentException
84 */
85 public function getItem($key)
86 {
87 if (!$this->keyExists($key)) {
88 throw new InvalidArgumentException("Invalid key {$key}.");
89 }
90
91 return $this->items[$key];
92 }
93
94 /**
95 * Returns items list
96 *
97 * @return array|null
98 */
99 public function getItems()
100 {
101 return $this->items;
102 }
103
104 /**
105 * List of keys to any external code
106 *
107 * @return array
108 */
109 public function keys()
110 {
111 return array_keys($this->items);
112 }
113
114 /**
115 * How many items are in the collection.
116 *
117 * @return int
118 */
119 public function length()
120 {
121 return count($this->items);
122 }
123
124 /**
125 * Determining whether a given key exists in the collection
126 *
127 * @param $key
128 *
129 * @return bool
130 */
131 public function keyExists($key)
132 {
133 return isset($this->items[$key]);
134 }
135
136 /**
137 * Place an item on the specific position
138 *
139 * @param $item
140 * @param $key
141 * @param $force
142 *
143 * @throws InvalidArgumentException
144 */
145 public function placeItem($item, $key, $force)
146 {
147 if ($force === false && $this->keyExists($key)) {
148 throw new InvalidArgumentException("Key {$key} already in use.");
149 }
150
151 $this->items[$key] = $item;
152 }
153
154 /**
155 * Prepend an item to the collection
156 *
157 * @param $item
158 * @param $key
159 * @param $force
160 *
161 * @throws InvalidArgumentException
162 */
163 public function prependItem($item, $key, $force)
164 {
165 if ($force === false && $this->keyExists($key)) {
166 throw new InvalidArgumentException("Key {$key} already in use.");
167 }
168
169 $this->items = [$key => $item] + $this->items;
170 }
171
172 /**
173 * Return an array of collection items
174 *
175 * @param $isFrontEndRequest
176 *
177 * @return array
178 */
179 public function toArray($isFrontEndRequest = false)
180 {
181 $array = [];
182
183 foreach ($this->items as $item) {
184 $array[] = $item->toArray($isFrontEndRequest);
185 }
186
187 return $array;
188 }
189 }
190