From bf6655a534a6775d30cafa67bd801276bda1d98d Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 13 Aug 2024 23:45:21 +0200 Subject: =?UTF-8?q?VERSION=200.2=20doctrine=20ORM=20et=20entit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../orm/src/Mapping/Driver/AttributeReader.php | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 vendor/doctrine/orm/src/Mapping/Driver/AttributeReader.php (limited to 'vendor/doctrine/orm/src/Mapping/Driver/AttributeReader.php') diff --git a/vendor/doctrine/orm/src/Mapping/Driver/AttributeReader.php b/vendor/doctrine/orm/src/Mapping/Driver/AttributeReader.php new file mode 100644 index 0000000..2de622a --- /dev/null +++ b/vendor/doctrine/orm/src/Mapping/Driver/AttributeReader.php @@ -0,0 +1,146 @@ +, bool> */ + private array $isRepeatableAttribute = []; + + /** + * @psalm-return class-string-map> + * + * @template T of MappingAttribute + */ + public function getClassAttributes(ReflectionClass $class): array + { + return $this->convertToAttributeInstances($class->getAttributes()); + } + + /** + * @return class-string-map> + * + * @template T of MappingAttribute + */ + public function getMethodAttributes(ReflectionMethod $method): array + { + return $this->convertToAttributeInstances($method->getAttributes()); + } + + /** + * @return class-string-map> + * + * @template T of MappingAttribute + */ + public function getPropertyAttributes(ReflectionProperty $property): array + { + return $this->convertToAttributeInstances($property->getAttributes()); + } + + /** + * @param class-string $attributeName The name of the annotation. + * + * @return T|null + * + * @template T of MappingAttribute + */ + public function getPropertyAttribute(ReflectionProperty $property, string $attributeName) + { + if ($this->isRepeatable($attributeName)) { + throw new LogicException(sprintf( + 'The attribute "%s" is repeatable. Call getPropertyAttributeCollection() instead.', + $attributeName, + )); + } + + return $this->getPropertyAttributes($property)[$attributeName] ?? null; + } + + /** + * @param class-string $attributeName The name of the annotation. + * + * @return RepeatableAttributeCollection + * + * @template T of MappingAttribute + */ + public function getPropertyAttributeCollection( + ReflectionProperty $property, + string $attributeName, + ): RepeatableAttributeCollection { + if (! $this->isRepeatable($attributeName)) { + throw new LogicException(sprintf( + 'The attribute "%s" is not repeatable. Call getPropertyAttribute() instead.', + $attributeName, + )); + } + + return $this->getPropertyAttributes($property)[$attributeName] ?? new RepeatableAttributeCollection(); + } + + /** + * @param array $attributes + * + * @return class-string-map> + * + * @template T of MappingAttribute + */ + private function convertToAttributeInstances(array $attributes): array + { + $instances = []; + + foreach ($attributes as $attribute) { + $attributeName = $attribute->getName(); + assert(is_string($attributeName)); + // Make sure we only get Doctrine Attributes + if (! is_subclass_of($attributeName, MappingAttribute::class)) { + continue; + } + + $instance = $attribute->newInstance(); + assert($instance instanceof MappingAttribute); + + if ($this->isRepeatable($attributeName)) { + if (! isset($instances[$attributeName])) { + $instances[$attributeName] = new RepeatableAttributeCollection(); + } + + $collection = $instances[$attributeName]; + assert($collection instanceof RepeatableAttributeCollection); + $collection[] = $instance; + } else { + $instances[$attributeName] = $instance; + } + } + + return $instances; + } + + /** @param class-string $attributeClassName */ + private function isRepeatable(string $attributeClassName): bool + { + if (isset($this->isRepeatableAttribute[$attributeClassName])) { + return $this->isRepeatableAttribute[$attributeClassName]; + } + + $reflectionClass = new ReflectionClass($attributeClassName); + $attribute = $reflectionClass->getAttributes()[0]->newInstance(); + + return $this->isRepeatableAttribute[$attributeClassName] = ($attribute->flags & Attribute::IS_REPEATABLE) > 0; + } +} -- cgit v1.2.3