|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Schema\Collections; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Schema\Collections\Exception\ObjectAlreadyExists; |
| 8 | +use Doctrine\DBAL\Schema\Collections\Exception\ObjectDoesNotExist; |
| 9 | +use Doctrine\DBAL\Schema\Name\UnqualifiedName; |
| 10 | +use Doctrine\DBAL\Schema\OptionallyNamedObject; |
| 11 | + |
| 12 | +use function array_splice; |
| 13 | +use function count; |
| 14 | +use function strtolower; |
| 15 | + |
| 16 | +/** |
| 17 | + * An ordered set of {@link OptionallyNamedObject}s with names being {@link UnqualifiedName}. |
| 18 | + * |
| 19 | + * New objects are added to the end of the set. The order of elements is preserved during modification. |
| 20 | + * |
| 21 | + * If an object is unnamed, it can be added to the set but cannot be referenced and, therefore, modified or removed |
| 22 | + * from the set. Two unnamed objects are considered as having different names. |
| 23 | + * |
| 24 | + * @internal |
| 25 | + * |
| 26 | + * @template E of OptionallyNamedObject<UnqualifiedName> |
| 27 | + * @template-implements ObjectSet<E> |
| 28 | + */ |
| 29 | +final class OptionallyUnqualifiedNamedObjectSet implements ObjectSet |
| 30 | +{ |
| 31 | + /** @var list<E> */ |
| 32 | + private array $elements = []; |
| 33 | + |
| 34 | + /** @var array<string, int> */ |
| 35 | + private array $elementPositionsByKey = []; |
| 36 | + |
| 37 | + /** @phpstan-param E ...$elements */ |
| 38 | + public function __construct(OptionallyNamedObject ...$elements) |
| 39 | + { |
| 40 | + foreach ($elements as $element) { |
| 41 | + $this->add($element); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public function isEmpty(): bool |
| 46 | + { |
| 47 | + return count($this->elements) === 0; |
| 48 | + } |
| 49 | + |
| 50 | + public function get(UnqualifiedName $elementName): ?OptionallyNamedObject |
| 51 | + { |
| 52 | + $key = $this->getKey($elementName); |
| 53 | + |
| 54 | + if (isset($this->elementPositionsByKey[$key])) { |
| 55 | + return $this->elements[$this->elementPositionsByKey[$key]]; |
| 56 | + } |
| 57 | + |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + public function add(object $element): void |
| 62 | + { |
| 63 | + $elementName = $element->getObjectName(); |
| 64 | + |
| 65 | + if ($elementName !== null) { |
| 66 | + $key = $this->getKey($elementName); |
| 67 | + |
| 68 | + if (isset($this->elementPositionsByKey[$key])) { |
| 69 | + throw ObjectAlreadyExists::new($elementName); |
| 70 | + } |
| 71 | + |
| 72 | + $this->elementPositionsByKey[$key] = count($this->elements); |
| 73 | + } |
| 74 | + |
| 75 | + $this->elements[] = $element; |
| 76 | + } |
| 77 | + |
| 78 | + public function remove(UnqualifiedName $elementName): void |
| 79 | + { |
| 80 | + $key = $this->getKey($elementName); |
| 81 | + |
| 82 | + if (! isset($this->elementPositionsByKey[$key])) { |
| 83 | + throw ObjectDoesNotExist::new($elementName); |
| 84 | + } |
| 85 | + |
| 86 | + $this->removeByKey($key); |
| 87 | + } |
| 88 | + |
| 89 | + public function modify(UnqualifiedName $elementName, callable $modification): void |
| 90 | + { |
| 91 | + $key = $this->getKey($elementName); |
| 92 | + |
| 93 | + if (! isset($this->elementPositionsByKey[$key])) { |
| 94 | + throw ObjectDoesNotExist::new($elementName); |
| 95 | + } |
| 96 | + |
| 97 | + $position = $this->elementPositionsByKey[$key]; |
| 98 | + |
| 99 | + $this->replace($key, $position, $modification($this->elements[$position])); |
| 100 | + } |
| 101 | + |
| 102 | + public function clear(): void |
| 103 | + { |
| 104 | + $this->elements = $this->elementPositionsByKey = []; |
| 105 | + } |
| 106 | + |
| 107 | + /** {@inheritDoc} */ |
| 108 | + public function toList(): array |
| 109 | + { |
| 110 | + return $this->elements; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Replaces the element corresponding to the old key with the provided element. |
| 115 | + * |
| 116 | + * @phpstan-param E $element |
| 117 | + * |
| 118 | + * @throws ObjectAlreadyExists If an element with the same name as the element name already exists. |
| 119 | + */ |
| 120 | + private function replace(string $oldKey, int $position, OptionallyNamedObject $element): void |
| 121 | + { |
| 122 | + $elementName = $element->getObjectName(); |
| 123 | + |
| 124 | + if ($elementName !== null) { |
| 125 | + $newKey = $this->getKey($elementName); |
| 126 | + |
| 127 | + if ($newKey !== $oldKey) { |
| 128 | + if (isset($this->elementPositionsByKey[$newKey])) { |
| 129 | + throw ObjectAlreadyExists::new($elementName); |
| 130 | + } |
| 131 | + |
| 132 | + unset($this->elementPositionsByKey[$oldKey]); |
| 133 | + |
| 134 | + $this->elementPositionsByKey[$newKey] = $position; |
| 135 | + } |
| 136 | + } else { |
| 137 | + unset($this->elementPositionsByKey[$oldKey]); |
| 138 | + } |
| 139 | + |
| 140 | + // @phpstan-ignore assign.propertyType |
| 141 | + $this->elements[$position] = $element; |
| 142 | + } |
| 143 | + |
| 144 | + private function removeByKey(string $key): void |
| 145 | + { |
| 146 | + $position = $this->elementPositionsByKey[$key]; |
| 147 | + |
| 148 | + array_splice($this->elements, $position, 1); |
| 149 | + unset($this->elementPositionsByKey[$key]); |
| 150 | + |
| 151 | + foreach ($this->elementPositionsByKey as $elementKey => $elementPosition) { |
| 152 | + if ($elementPosition <= $position) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + $this->elementPositionsByKey[$elementKey]--; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private function getKey(UnqualifiedName $name): string |
| 161 | + { |
| 162 | + return strtolower($name->getIdentifier()->getValue()); |
| 163 | + } |
| 164 | +} |
0 commit comments