|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\ClassVariance\Classmaps; |
| 6 | + |
| 7 | +/** |
| 8 | + * Immutable map of CSS class group definitions and their conflict rules. |
| 9 | + * |
| 10 | + * Each entry in $classGroups maps a group ID to a list of matchers. |
| 11 | + * Three matcher shapes are supported: |
| 12 | + * |
| 13 | + * string → exact class name match |
| 14 | + * e.g. 'block' matches only 'block' |
| 15 | + * |
| 16 | + * ['prefix'] → wildcard prefix match |
| 17 | + * e.g. ['p'] matches 'p', 'p-4', 'p-[2rem]' |
| 18 | + * |
| 19 | + * ['prefix' => ['v1', 'v2']] → constrained prefix match |
| 20 | + * matches 'prefix-v1', 'prefix-v2', etc. |
| 21 | + * Use '' as a suffix to match the bare prefix |
| 22 | + * e.g. ['border' => ['', '2', '4']] matches |
| 23 | + * 'border', 'border-2', 'border-4' |
| 24 | + * |
| 25 | + * Iteration order determines priority: the first group whose matcher fires wins. |
| 26 | + * Constrained prefix groups should come before wildcard prefix groups when both |
| 27 | + * share the same prefix (e.g. font-size before text-color for the 'text' prefix). |
| 28 | + * |
| 29 | + * $conflictingClassGroups maps a group ID to the list of group IDs it supersedes. |
| 30 | + * When a class from group A is encountered during merge, all previously accumulated |
| 31 | + * classes from groups listed under A are removed. |
| 32 | + * |
| 33 | + * Use extend() to merge additional definitions on top of an existing map (additive), |
| 34 | + * or override() to replace specific group definitions entirely. |
| 35 | + */ |
| 36 | +final readonly class Classmap |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @param array<string, list<string|array<string, list<string>>|array{0: string}>> $classGroups |
| 40 | + * @param array<string, list<string>> $conflictingClassGroups |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + public array $classGroups = [], |
| 44 | + public array $conflictingClassGroups = [], |
| 45 | + ) {} |
| 46 | + |
| 47 | + /** |
| 48 | + * Find the group ID for the given class, or null if unknown. |
| 49 | + */ |
| 50 | + public function findGroup(string $class): ?string |
| 51 | + { |
| 52 | + foreach ($this->classGroups as $groupId => $matchers) { |
| 53 | + foreach ($matchers as $matcher) { |
| 54 | + if (is_string($matcher)) { |
| 55 | + // Exact match |
| 56 | + if ($matcher === $class) { |
| 57 | + return $groupId; |
| 58 | + } |
| 59 | + |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if (! is_array($matcher)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + if (array_is_list($matcher)) { |
| 68 | + // ['prefix'] — wildcard: matches prefix itself or prefix-{anything} |
| 69 | + $prefix = $matcher[0]; |
| 70 | + |
| 71 | + if ($class === $prefix || str_starts_with($class, $prefix . '-')) { |
| 72 | + return $groupId; |
| 73 | + } |
| 74 | + } else { |
| 75 | + // ['prefix' => ['suffix1', 'suffix2']] — constrained suffix list |
| 76 | + $prefix = array_key_first($matcher); |
| 77 | + $suffixes = $matcher[$prefix]; |
| 78 | + |
| 79 | + foreach ($suffixes as $suffix) { |
| 80 | + if ($suffix === '' && $class === $prefix) { |
| 81 | + return $groupId; |
| 82 | + } |
| 83 | + |
| 84 | + if ($suffix !== '' && $class === $prefix . '-' . $suffix) { |
| 85 | + return $groupId; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Return a new map with $additions merged in. |
| 97 | + * For groups that exist in both maps, matchers are concatenated (additive). |
| 98 | + * Conflicting group lists are merged by union. |
| 99 | + */ |
| 100 | + public function extend(self $additions): self |
| 101 | + { |
| 102 | + $groups = $this->classGroups; |
| 103 | + |
| 104 | + foreach ($additions->classGroups as $groupId => $matchers) { |
| 105 | + $groups[$groupId] = array_merge($groups[$groupId] ?? [], $matchers); |
| 106 | + } |
| 107 | + |
| 108 | + $conflicts = $this->conflictingClassGroups; |
| 109 | + |
| 110 | + foreach ($additions->conflictingClassGroups as $groupId => $conflicting) { |
| 111 | + $conflicts[$groupId] = array_values(array_unique([ |
| 112 | + ...($conflicts[$groupId] ?? []), |
| 113 | + ...$conflicting, |
| 114 | + ])); |
| 115 | + } |
| 116 | + |
| 117 | + return new self($groups, $conflicts); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Return a new map with specific groups replaced entirely by $replacements. |
| 122 | + * Groups not present in $replacements are kept as-is. |
| 123 | + */ |
| 124 | + public function override(self $replacements): self |
| 125 | + { |
| 126 | + return new self( |
| 127 | + array_replace($this->classGroups, $replacements->classGroups), |
| 128 | + array_replace($this->conflictingClassGroups, $replacements->conflictingClassGroups), |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments