| 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 |
* Return an array of collection items |
| 156 |
* |
| 157 |
* @param $isFrontEndRequest |
| 158 |
* |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public function toArray($isFrontEndRequest = false) |
| 162 |
{ |
| 163 |
$array = []; |
| 164 |
|
| 165 |
foreach ($this->items as $item) { |
| 166 |
$array[] = $item->toArray($isFrontEndRequest); |
| 167 |
} |
| 168 |
|
| 169 |
return $array; |
| 170 |
} |
| 171 |
} |
| 172 |
|