| Total Complexity | 10 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | abstract class ModelCollection implements CreatableFromArray, \Countable, \Iterator |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $items = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Current iterator key. |
||
| 16 | * |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | protected $key = 0; |
||
| 20 | |||
| 21 | 11 | protected function __construct() |
|
| 22 | { |
||
| 23 | 11 | } |
|
| 24 | |||
| 25 | 7 | public static function createFromArray(array $data) |
|
| 26 | { |
||
| 27 | 7 | $collection = new static(); |
|
| 28 | 7 | foreach ($data as $itemData) { |
|
| 29 | 6 | $collection->items[] = $collection->createItem($itemData); |
|
| 30 | } |
||
| 31 | |||
| 32 | 7 | return $collection; |
|
| 33 | } |
||
| 34 | |||
| 35 | 2 | public function getItems(): array |
|
| 36 | { |
||
| 37 | 2 | return $this->items; |
|
| 38 | } |
||
| 39 | |||
| 40 | 9 | public function count(): int |
|
| 41 | { |
||
| 42 | 9 | return \count($this->items); |
|
| 43 | } |
||
| 44 | |||
| 45 | 1 | public function key() |
|
| 46 | { |
||
| 47 | 1 | return $this->key; |
|
| 48 | } |
||
| 49 | |||
| 50 | 4 | public function next(): void |
|
| 51 | { |
||
| 52 | 4 | ++$this->key; |
|
| 53 | 4 | } |
|
| 54 | |||
| 55 | 1 | public function valid(): bool |
|
| 56 | { |
||
| 57 | 1 | return $this->key < $this->count(); |
|
| 58 | } |
||
| 59 | |||
| 60 | 6 | public function current() |
|
| 63 | } |
||
| 64 | |||
| 65 | 2 | public function rewind(): void |
|
| 68 | 2 | } |
|
| 69 | |||
| 70 | abstract protected function createItem(array $data); |
||
| 71 | } |
||
| 72 |