|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Spameri\ElasticQuery\Aggregation\Terms; |
| 4 | + |
| 5 | +class OrderCollection implements |
| 6 | + \Spameri\ElasticQuery\Collection\SimpleCollectionInterface, |
| 7 | + \Countable |
| 8 | +{ |
| 9 | + |
| 10 | + /** |
| 11 | + * @var array<\Spameri\ElasticQuery\Aggregation\Terms\Order> |
| 12 | + */ |
| 13 | + private array $collection; |
| 14 | + |
| 15 | + |
| 16 | + public function __construct( |
| 17 | + \Spameri\ElasticQuery\Aggregation\Terms\Order ... $collection |
| 18 | + ) { |
| 19 | + foreach ($collection as $order) { |
| 20 | + $this->add($order); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + public function remove(string $key): bool |
| 26 | + { |
| 27 | + if (isset($this->collection[$key])) { |
| 28 | + unset($this->collection[$key]); |
| 29 | + |
| 30 | + return TRUE; |
| 31 | + } |
| 32 | + |
| 33 | + return FALSE; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + public function get(string $key): ?\Spameri\ElasticQuery\Aggregation\Terms\Order |
| 38 | + { |
| 39 | + return $this->collection[$key] ?? NULL; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + public function isValue(string $key): bool |
| 44 | + { |
| 45 | + return isset($this->collection[$key]); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public function keys(): array |
| 50 | + { |
| 51 | + return \array_keys($this->collection); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + public function clear(): void |
| 56 | + { |
| 57 | + $this->collection = []; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * @param \Spameri\ElasticQuery\Aggregation\Terms\Order $item |
| 63 | + */ |
| 64 | + public function add($item): void |
| 65 | + { |
| 66 | + $this->collection[$item->key()] = $item; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public function getIterator(): \ArrayIterator |
| 71 | + { |
| 72 | + return new \ArrayIterator($this->collection); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public function count(): int |
| 77 | + { |
| 78 | + return \count($this->collection); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public function toArray(): array |
| 83 | + { |
| 84 | + if (\count($this->collection) === 1) { |
| 85 | + return \reset($this->collection)->toArray(); |
| 86 | + } |
| 87 | + |
| 88 | + $array = []; |
| 89 | + foreach ($this->collection as $order) { |
| 90 | + $array[] = $order->toArray(); |
| 91 | + } |
| 92 | + |
| 93 | + return $array; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments