|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Blueprint; |
| 4 | + |
| 5 | +use Kirby\Toolkit\Str; |
| 6 | + |
| 7 | +/** |
| 8 | + * Collects the props of all fields in a blueprint |
| 9 | + * |
| 10 | + * @copyright Bastian Allgeier |
| 11 | + * @license https://getkirby.com/license |
| 12 | + * @since 6.0.0 |
| 13 | + * @unstable |
| 14 | + */ |
| 15 | +class FieldsRegistry |
| 16 | +{ |
| 17 | + // Props of all fields, keyed by their declared name |
| 18 | + protected array $fields = []; |
| 19 | + |
| 20 | + // Declared names of all fields, keyed by their lowercase name |
| 21 | + protected array $names = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * Claims the given names without touching their props. |
| 25 | + * The props can still be any of the shortcuts a blueprint |
| 26 | + * allows, as they are only normalized later. |
| 27 | + */ |
| 28 | + public function __construct(array $fields = []) |
| 29 | + { |
| 30 | + foreach ($fields as $name => $props) { |
| 31 | + $this->fields[$name] = $props; |
| 32 | + $this->names[Str::lower((string)$name)] = $name; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Adds the props of the given fields under names that are |
| 38 | + * not taken yet and returns them, keyed by the names they |
| 39 | + * ended up with. A list is keyed by the name in the props. |
| 40 | + */ |
| 41 | + public function add(array $fields): array |
| 42 | + { |
| 43 | + $added = []; |
| 44 | + |
| 45 | + foreach ($fields as $name => $props) { |
| 46 | + if (is_int($name) === true) { |
| 47 | + $name = $props['name']; |
| 48 | + } |
| 49 | + |
| 50 | + if ($this->has($name) === true) { |
| 51 | + $props = $this->conflict($name, $props); |
| 52 | + $name = $props['name']; |
| 53 | + } |
| 54 | + |
| 55 | + $this->names[Str::lower((string)$name)] = $name; |
| 56 | + $added[$name] = $this->fields[$name] = $props; |
| 57 | + } |
| 58 | + |
| 59 | + return $added; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates an error field for a name that is already taken. |
| 64 | + * The error gets a unique name of its own, so that the field |
| 65 | + * which claimed the name first can stay intact. |
| 66 | + */ |
| 67 | + protected function conflict(string|int $name, array $props): array |
| 68 | + { |
| 69 | + $count = 1; |
| 70 | + |
| 71 | + while ($this->has($name . '-duplicate-' . $count) === true) { |
| 72 | + $count++; |
| 73 | + } |
| 74 | + |
| 75 | + return [ |
| 76 | + 'label' => $props['label'] ?? 'Error', |
| 77 | + 'name' => $name . '-duplicate-' . $count, |
| 78 | + 'text' => 'The field <strong>"' . $name . '"</strong> already exists in your blueprint', |
| 79 | + 'theme' => 'negative', |
| 80 | + 'type' => 'info', |
| 81 | + ]; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns the props of a single field. The lookup is |
| 86 | + * case-insensitive, just like the namespace. |
| 87 | + */ |
| 88 | + public function get(string|int $name): array|null |
| 89 | + { |
| 90 | + $name = $this->names[Str::lower((string)$name)] ?? null; |
| 91 | + |
| 92 | + return $name !== null ? $this->fields[$name] : null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks if a name is already taken |
| 97 | + */ |
| 98 | + public function has(string|int $name): bool |
| 99 | + { |
| 100 | + return isset($this->names[Str::lower((string)$name)]) === true; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the props of all fields, |
| 105 | + * keyed by their declared name |
| 106 | + */ |
| 107 | + public function toArray(): array |
| 108 | + { |
| 109 | + return $this->fields; |
| 110 | + } |
| 111 | +} |
0 commit comments