ComponentFactory
2 days ago
Control
2 days ago
Type
2 days ago
AttributeCollection.php
2 days ago
AttributeFactory.php
2 days ago
Children.php
2 days ago
Component.php
2 days ago
ComponentBuilder.php
2 days ago
ComponentCollection.php
2 days ago
ComponentFactory.php
2 days ago
ConditionalComponentFactory.php
2 days ago
ConditionalComponentFactoryCollection.php
2 days ago
Config.php
2 days ago
ConfigCollection.php
2 days ago
ConfigFactory.php
2 days ago
Context.php
2 days ago
Control.php
2 days ago
DefaultSettingsBuilder.php
2 days ago
Encoder.php
2 days ago
Formatter.php
2 days ago
FormatterCollection.php
2 days ago
ComponentCollection.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Setting; |
| 6 | |
| 7 | use AC\Collection; |
| 8 | |
| 9 | final class ComponentCollection extends Collection |
| 10 | { |
| 11 | private int $added = 0; |
| 12 | |
| 13 | private const ADDED_SEPARATOR = '.'; |
| 14 | |
| 15 | /** |
| 16 | * @var string[] |
| 17 | */ |
| 18 | private array $priorities = []; |
| 19 | |
| 20 | public function __construct(array $data = [], int $priority = 10) |
| 21 | { |
| 22 | foreach ($data as $component) { |
| 23 | $this->add($component, $priority); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | private function get_priority_key(int $priority): string |
| 28 | { |
| 29 | return $priority . self::ADDED_SEPARATOR . $this->added++; |
| 30 | } |
| 31 | |
| 32 | private function get_priority_from_key(string $priority_key): int |
| 33 | { |
| 34 | return (int)strstr($priority_key, self::ADDED_SEPARATOR, true); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Priority is taken into account when iterating. E.g. 1 comes before 5. |
| 39 | */ |
| 40 | public function add(Component $component, int $priority = 10): ComponentCollection |
| 41 | { |
| 42 | $this->data[$this->get_priority_key($priority)] = $component; |
| 43 | |
| 44 | ksort($this->data, SORT_NATURAL); |
| 45 | |
| 46 | $this->priorities = array_keys($this->data); |
| 47 | |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | public function current(): Component |
| 52 | { |
| 53 | return current($this->data); |
| 54 | } |
| 55 | |
| 56 | public function current_priority(): int |
| 57 | { |
| 58 | return $this->get_priority_from_key($this->priorities[$this->index]); |
| 59 | } |
| 60 | |
| 61 | public function merge(ComponentCollection $collection): ComponentCollection |
| 62 | { |
| 63 | $merged = new ComponentCollection(); |
| 64 | |
| 65 | foreach ($this as $component) { |
| 66 | $merged->add($component, $this->current_priority()); |
| 67 | } |
| 68 | |
| 69 | foreach ($collection as $component) { |
| 70 | $merged->add($component, $collection->current_priority()); |
| 71 | } |
| 72 | |
| 73 | return $merged; |
| 74 | } |
| 75 | |
| 76 | public function find(string $name, ?ComponentCollection $settings = null): ?Component |
| 77 | { |
| 78 | $settings = $settings ?: $this; |
| 79 | |
| 80 | foreach ($settings as $setting) { |
| 81 | if ($setting->has_children()) { |
| 82 | $found = $this->find($name, $setting->get_children()->get_iterator()); |
| 83 | |
| 84 | if ($found) { |
| 85 | return $found; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if ($setting->has_input() && $name === $setting->get_input()->get_name()) { |
| 90 | return $setting; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 |