maxalmonte14 /
phpcollections
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace PHPCollections\Collections; |
||
| 6 | |||
| 7 | use Countable; |
||
| 8 | use JsonSerializable; |
||
| 9 | use OutOfRangeException; |
||
| 10 | use PHPCollections\DataHolder; |
||
| 11 | use PHPCollections\Exceptions\InvalidOperationException; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The base class for countable and |
||
| 15 | * JSON serializable collections. |
||
| 16 | */ |
||
| 17 | abstract class BaseCollection implements Countable, JsonSerializable |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The data container. |
||
| 21 | * |
||
| 22 | * @var \PHPCollections\DataHolder |
||
| 23 | */ |
||
| 24 | protected $dataHolder; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Initializes the dataHolder property. |
||
| 28 | * |
||
| 29 | * @param array $data |
||
| 30 | */ |
||
| 31 | 78 | public function __construct(array $data = []) |
|
| 32 | { |
||
| 33 | 78 | $this->dataHolder = new DataHolder($data); |
|
| 34 | 78 | } |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Reinitializes the dataHolder property. |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | 3 | public function clear(): void |
|
| 42 | { |
||
| 43 | 3 | $this->dataHolder = new DataHolder(); |
|
| 44 | 3 | } |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Checks if the collection |
||
| 48 | * contains a given value. |
||
| 49 | * |
||
| 50 | * @param mixed $needle |
||
| 51 | * |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | 5 | public function contains($needle): bool |
|
| 55 | { |
||
| 56 | 5 | return in_array($needle, $this->toArray()); |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns the length of the collection. |
||
| 61 | * |
||
| 62 | * @return int |
||
| 63 | */ |
||
| 64 | 40 | public function count(): int |
|
| 65 | { |
||
| 66 | 40 | return count($this->toArray()); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Gets the difference between two collections. |
||
| 71 | * |
||
| 72 | * @param \PHPCollections\Collections\BaseCollection $collection |
||
| 73 | * |
||
| 74 | * @return \PHPCollections\Collections\BaseCollection |
||
| 75 | */ |
||
| 76 | abstract public function diff(self $collection): self; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Checks if the given index |
||
| 80 | * exists in the collection. |
||
| 81 | * |
||
| 82 | * @param mixed $offset |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | 6 | public function exists($offset): bool |
|
| 87 | { |
||
| 88 | 6 | return $this->dataHolder->offsetExists($offset); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Determines if two collections are equal. |
||
| 93 | * |
||
| 94 | * @param \PHPCollections\Collections\BaseCollection $collection |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | abstract public function equals(self $collection): bool; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Fills the collection with a set of data. |
||
| 102 | * |
||
| 103 | * @param array $data |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | 2 | public function fill(array $data): void |
|
| 108 | { |
||
| 109 | 2 | foreach ($data as $entry) { |
|
| 110 | 2 | $this->add($entry); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 111 | } |
||
| 112 | 2 | } |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Gets the first element in the collection. |
||
| 116 | * |
||
| 117 | * @throws \OutOfRangeException |
||
| 118 | * |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | 6 | public function first() |
|
| 122 | { |
||
| 123 | 6 | if ($this->isEmpty()) { |
|
| 124 | 1 | throw new OutOfRangeException('You\'re trying to get data from an empty collection'); |
|
| 125 | } |
||
| 126 | |||
| 127 | 6 | return array_values($this->toArray())[0]; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Checks if the collection is empty. |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | 24 | public function isEmpty(): bool |
|
| 136 | { |
||
| 137 | 24 | return $this->count() === 0; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Defines the behavior of the collection |
||
| 142 | * when json_encode is called. |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function jsonSerialize(): array |
||
| 147 | { |
||
| 148 | return $this->toArray(); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the last element of the collection. |
||
| 153 | * |
||
| 154 | * @throws \OutOfRangeException |
||
| 155 | * |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | 7 | public function last() |
|
| 159 | { |
||
| 160 | 7 | if ($this->isEmpty()) { |
|
| 161 | 1 | throw new OutOfRangeException('You\'re trying to get data from an empty collection'); |
|
| 162 | } |
||
| 163 | |||
| 164 | 7 | return array_values($this->toArray())[$this->count() - 1]; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns a portion of the collection. |
||
| 169 | * |
||
| 170 | * @param int $offset |
||
| 171 | * @param int|null $lenght |
||
| 172 | * |
||
| 173 | * @return \PHPCollections\Collections\BaseCollection |
||
| 174 | */ |
||
| 175 | abstract public function slice(int $offset, ?int $lenght): ?self; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns the sum of a set of values. |
||
| 179 | * |
||
| 180 | * @param callable $callback |
||
| 181 | * |
||
| 182 | * @throws \PHPCollections\Exceptions\InvalidOperationException |
||
| 183 | * |
||
| 184 | * @return float |
||
| 185 | */ |
||
| 186 | 6 | public function sum(callable $callback): float |
|
| 187 | { |
||
| 188 | 6 | $sum = 0; |
|
| 189 | |||
| 190 | 6 | foreach ($this->dataHolder as $value) { |
|
| 191 | 6 | if (!is_numeric($result = call_user_func($callback, $value))) { |
|
| 192 | 3 | throw new InvalidOperationException('You cannot sum non-numeric values'); |
|
| 193 | } |
||
| 194 | |||
| 195 | 3 | $sum += $result; |
|
| 196 | } |
||
| 197 | |||
| 198 | 3 | return $sum; |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns a plain array with |
||
| 203 | * your dictionary data. |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | 51 | public function toArray(): array |
|
| 208 | { |
||
| 209 | 51 | return $this->dataHolder->getContainer(); |
|
| 210 | } |
||
| 211 | } |
||
| 212 |