BannerContextResolver.php
44 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Admin\Banner; |
| 6 | |
| 7 | use AC\TableScreen; |
| 8 | |
| 9 | class BannerContextResolver |
| 10 | { |
| 11 | /** @var BannerContext[] */ |
| 12 | private array $contexts; |
| 13 | |
| 14 | /** |
| 15 | * @param BannerContext[] $contexts |
| 16 | */ |
| 17 | public function __construct(array $contexts = []) |
| 18 | { |
| 19 | $this->contexts = $contexts; |
| 20 | } |
| 21 | |
| 22 | public function resolve(TableScreen $table_screen): ?BannerContext |
| 23 | { |
| 24 | $active = []; |
| 25 | |
| 26 | foreach ($this->contexts as $context) { |
| 27 | if ($context->is_active($table_screen)) { |
| 28 | $active[] = $context; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if (empty($active)) { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | usort($active, static function (BannerContext $a, BannerContext $b): int { |
| 37 | return $a->get_priority() - $b->get_priority(); |
| 38 | }); |
| 39 | |
| 40 | return $active[0]; |
| 41 | } |
| 42 | |
| 43 | } |
| 44 |