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