| 1 | <?php |
||
| 7 | abstract class ModelCollectionOrdered extends ModelCollection |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $order = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * {@inheritdoc} |
||
| 16 | */ |
||
| 17 | 2 | public static function createFromArray(array $data) |
|
| 18 | { |
||
| 19 | 2 | $collection = new static(); |
|
| 20 | |||
| 21 | 2 | foreach ($data[static::getItemsDataName()] as $itemId => $itemData) { |
|
| 22 | 2 | $collection->items[$itemId] = $collection->createItem($itemData); |
|
| 23 | } |
||
| 24 | 2 | $collection->order = $data['order']; |
|
| 25 | |||
| 26 | 2 | return $collection; |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | */ |
||
| 32 | 2 | public function current() |
|
| 33 | { |
||
| 34 | 2 | return $this->items[$this->order[$this->key]]; |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 2 | public function key() |
|
| 41 | { |
||
| 42 | 2 | return $this->order[$this->key]; |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | */ |
||
| 48 | 2 | public function valid() |
|
| 49 | { |
||
| 50 | 2 | return $this->key < count($this->order); |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return string[] |
||
| 55 | */ |
||
| 56 | 2 | public function getOrder(): array |
|
| 57 | { |
||
| 58 | 2 | return $this->order; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns the name of the data containing the items to be sorted. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | abstract protected static function getItemsDataName(); |
||
| 67 | } |
||
| 68 |