ListIds.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\ListScreenRepository\Sort; |
| 6 | |
| 7 | use AC\ListScreenCollection; |
| 8 | use AC\ListScreenRepository\Sort; |
| 9 | |
| 10 | class ListIds implements Sort |
| 11 | { |
| 12 | private array $list_ids; |
| 13 | |
| 14 | public function __construct(array $list_ids = []) |
| 15 | { |
| 16 | $this->list_ids = $list_ids; |
| 17 | } |
| 18 | |
| 19 | public function sort(ListScreenCollection $list_screens): ListScreenCollection |
| 20 | { |
| 21 | if (! $list_screens->count()) { |
| 22 | return $list_screens; |
| 23 | } |
| 24 | |
| 25 | $lists = []; |
| 26 | |
| 27 | foreach ($list_screens as $list_screen) { |
| 28 | $lists[(string)$list_screen->get_id()] = $list_screen; |
| 29 | } |
| 30 | |
| 31 | $ordered = new ListScreenCollection(); |
| 32 | |
| 33 | foreach (array_unique($this->list_ids) as $list_id) { |
| 34 | if (! isset($lists[$list_id])) { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | $ordered->add($lists[$list_id]); |
| 39 | |
| 40 | unset($lists[$list_id]); |
| 41 | } |
| 42 | |
| 43 | foreach ($lists as $list_screen) { |
| 44 | $ordered->add($list_screen); |
| 45 | } |
| 46 | |
| 47 | return $ordered; |
| 48 | } |
| 49 | |
| 50 | } |
| 51 |