From e81903bf796c8c88a7fad7efce14570ce068ef99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 26 Jan 2024 17:50:52 +0100 Subject: [PATCH 1/4] chore: remove Doctrine Annotations --- composer.json | 1 - .../{annotations.php => attributes.php} | 4 +- docs/examples/reverse.php | 4 +- psalm.xml.dist | 4 +- src/Bridge/PhpSpreadsheet/composer.json | 1 - src/Bridge/Spout/composer.json | 1 - .../Symfony/Command/SerializeCommand.php | 4 +- .../Symfony/Command/ValidateCommand.php | 4 +- .../ArrayReference.php | 6 +- .../ColumnReference.php | 7 +- .../HeaderReference.php | 7 +- .../{Annotation => Attribute}/Options.php | 7 +- .../{Annotation => Attribute}/Reference.php | 2 +- .../Exception/AnnotationException.php | 38 ---------- .../Exception/AttributeException.php | 32 +++++++++ .../AttributeDriver.php} | 72 ++++++------------- .../Test/Functional/FunctionalTestCase.php | 30 ++++---- src/Xezilaires/Test/Model/Product.php | 2 +- .../Test/Model/ProductWithAttributes.php | 2 +- src/Xezilaires/composer.json | 1 - 20 files changed, 87 insertions(+), 142 deletions(-) rename docs/examples/{annotations.php => attributes.php} (88%) rename src/Xezilaires/{Annotation => Attribute}/ArrayReference.php (86%) rename src/Xezilaires/{Annotation => Attribute}/ColumnReference.php (78%) rename src/Xezilaires/{Annotation => Attribute}/HeaderReference.php (78%) rename src/Xezilaires/{Annotation => Attribute}/Options.php (85%) rename src/Xezilaires/{Annotation => Attribute}/Reference.php (91%) delete mode 100644 src/Xezilaires/Exception/AnnotationException.php create mode 100644 src/Xezilaires/Exception/AttributeException.php rename src/Xezilaires/Metadata/{Annotation/AnnotationDriver.php => Attribute/AttributeDriver.php} (56%) diff --git a/composer.json b/composer.json index e030347..df0fe9f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,6 @@ "symfony/serializer": "^6.4 || ^7.0" }, "require-dev": { - "doctrine/annotations": "^1.12", "matthiasnoback/symfony-dependency-injection-test": "^4.0", "nyholm/nsa": "^1.1", "nyholm/symfony-bundle-test": "dev-master", diff --git a/docs/examples/annotations.php b/docs/examples/attributes.php similarity index 88% rename from docs/examples/annotations.php rename to docs/examples/attributes.php index 07c6a2b..ce0f981 100644 --- a/docs/examples/annotations.php +++ b/docs/examples/attributes.php @@ -26,13 +26,13 @@ $iteratorFactory = new \Xezilaires\SpreadsheetIteratorFactory($normalizer, [ \Xezilaires\Bridge\PhpSpreadsheet\Spreadsheet::class, ]); -$annotationDriver = new \Xezilaires\Metadata\Annotation\AnnotationDriver(); +$attributeDriver = new \Xezilaires\Metadata\Attribute\AttributeDriver(); // $iterator = $iteratorFactory->fromFile( // https://github.com/sigwinhq/xezilaires/raw/master/resources/fixtures/products.xlsx new \SplFileObject(__DIR__.'/../../src/Xezilaires/Test/resources/fixtures/products.xlsx'), - $annotationDriver->getMetadataMapping(Product::class) + $attributeDriver->getMetadataMapping(Product::class) ); $out = iterator_to_array($iterator); diff --git a/docs/examples/reverse.php b/docs/examples/reverse.php index af199a5..80942f4 100644 --- a/docs/examples/reverse.php +++ b/docs/examples/reverse.php @@ -26,13 +26,13 @@ $iteratorFactory = new \Xezilaires\SpreadsheetIteratorFactory($normalizer, [ \Xezilaires\Bridge\PhpSpreadsheet\Spreadsheet::class, ]); -$annotationDriver = new \Xezilaires\Metadata\Annotation\AnnotationDriver(); +$attributeDriver = new \Xezilaires\Metadata\Attribute\AttributeDriver(); // $iterator = $iteratorFactory->fromFile( // https://github.com/sigwinhq/xezilaires/raw/master/resources/fixtures/products.xlsx new \SplFileObject(__DIR__.'/../../src/Xezilaires/Test/resources/fixtures/products.xlsx'), - $annotationDriver->getMetadataMapping(Product::class, ['reverse' => true]) + $attributeDriver->getMetadataMapping(Product::class, ['reverse' => true]) ); $out = iterator_to_array($iterator); diff --git a/psalm.xml.dist b/psalm.xml.dist index 364d7af..9c433db 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -34,7 +34,7 @@ - + @@ -46,7 +46,7 @@ - + diff --git a/src/Bridge/PhpSpreadsheet/composer.json b/src/Bridge/PhpSpreadsheet/composer.json index bd3940f..30d67a0 100644 --- a/src/Bridge/PhpSpreadsheet/composer.json +++ b/src/Bridge/PhpSpreadsheet/composer.json @@ -15,7 +15,6 @@ "sigwin/xezilaires": "^0.6" }, "require-dev": { - "doctrine/annotations": "^1.11", "phpunit/phpunit": "^9.6", "symfony/phpunit-bridge": "^6.4 || ^7.0", "symfony/property-access": "^6.4 || ^7.0", diff --git a/src/Bridge/Spout/composer.json b/src/Bridge/Spout/composer.json index 92322c3..9cf7bdf 100644 --- a/src/Bridge/Spout/composer.json +++ b/src/Bridge/Spout/composer.json @@ -15,7 +15,6 @@ "sigwin/xezilaires": "^0.6" }, "require-dev": { - "doctrine/annotations": "^1.11", "nyholm/nsa": "^1.1", "phpunit/phpunit": "^9.6", "symfony/phpunit-bridge": "^6.4 || ^7.0", diff --git a/src/Bridge/Symfony/Command/SerializeCommand.php b/src/Bridge/Symfony/Command/SerializeCommand.php index db331e0..2200c71 100644 --- a/src/Bridge/Symfony/Command/SerializeCommand.php +++ b/src/Bridge/Symfony/Command/SerializeCommand.php @@ -21,7 +21,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Serializer\Encoder\CsvEncoder; use Xezilaires\IteratorFactory; -use Xezilaires\Metadata\Annotation\AnnotationDriver; +use Xezilaires\Metadata\Attribute\AttributeDriver; use Xezilaires\Serializer; #[AsCommand( @@ -122,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int break; } - $driver = new AnnotationDriver(); + $driver = new AttributeDriver(); $iterator = $this->iteratorFactory->fromFile( new \SplFileObject($path), $driver->getMetadataMapping($class, ['reverse' => $reverse, 'sequential' => true]) diff --git a/src/Bridge/Symfony/Command/ValidateCommand.php b/src/Bridge/Symfony/Command/ValidateCommand.php index 3b6494b..450006d 100644 --- a/src/Bridge/Symfony/Command/ValidateCommand.php +++ b/src/Bridge/Symfony/Command/ValidateCommand.php @@ -22,7 +22,7 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Validator\ConstraintViolationInterface; use Xezilaires\IteratorFactory; -use Xezilaires\Metadata\Annotation\AnnotationDriver; +use Xezilaires\Metadata\Attribute\AttributeDriver; use Xezilaires\Validator; #[AsCommand( @@ -79,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $style = new SymfonyStyle($input, $output); $style->title('Xezilaires validate'); - $driver = new AnnotationDriver(); + $driver = new AttributeDriver(); $totalCount = 0; $totalInvalid = 0; diff --git a/src/Xezilaires/Annotation/ArrayReference.php b/src/Xezilaires/Attribute/ArrayReference.php similarity index 86% rename from src/Xezilaires/Annotation/ArrayReference.php rename to src/Xezilaires/Attribute/ArrayReference.php index c22eceb..7d1ae2a 100644 --- a/src/Xezilaires/Annotation/ArrayReference.php +++ b/src/Xezilaires/Attribute/ArrayReference.php @@ -11,12 +11,8 @@ * with this source code in the file LICENSE. */ -namespace Xezilaires\Annotation; +namespace Xezilaires\Attribute; -/** - * @Annotation - * @Target({"PROPERTY"}) - */ #[\Attribute(\Attribute::TARGET_PROPERTY)] final class ArrayReference { diff --git a/src/Xezilaires/Annotation/ColumnReference.php b/src/Xezilaires/Attribute/ColumnReference.php similarity index 78% rename from src/Xezilaires/Annotation/ColumnReference.php rename to src/Xezilaires/Attribute/ColumnReference.php index 1a59e30..652e9b6 100644 --- a/src/Xezilaires/Annotation/ColumnReference.php +++ b/src/Xezilaires/Attribute/ColumnReference.php @@ -11,13 +11,8 @@ * with this source code in the file LICENSE. */ -namespace Xezilaires\Annotation; +namespace Xezilaires\Attribute; -/** - * @Annotation - * @Target({"ANNOTATION", "PROPERTY"}) - * @NamedArgumentConstructor - */ #[\Attribute(\Attribute::TARGET_PROPERTY)] final class ColumnReference implements Reference { diff --git a/src/Xezilaires/Annotation/HeaderReference.php b/src/Xezilaires/Attribute/HeaderReference.php similarity index 78% rename from src/Xezilaires/Annotation/HeaderReference.php rename to src/Xezilaires/Attribute/HeaderReference.php index 25e0e33..a1c820f 100644 --- a/src/Xezilaires/Annotation/HeaderReference.php +++ b/src/Xezilaires/Attribute/HeaderReference.php @@ -11,13 +11,8 @@ * with this source code in the file LICENSE. */ -namespace Xezilaires\Annotation; +namespace Xezilaires\Attribute; -/** - * @Annotation - * @Target({"ANNOTATION", "PROPERTY"}) - * @NamedArgumentConstructor - */ #[\Attribute(\Attribute::TARGET_PROPERTY)] final class HeaderReference implements Reference { diff --git a/src/Xezilaires/Annotation/Options.php b/src/Xezilaires/Attribute/Options.php similarity index 85% rename from src/Xezilaires/Annotation/Options.php rename to src/Xezilaires/Attribute/Options.php index 8d148cf..204e704 100644 --- a/src/Xezilaires/Annotation/Options.php +++ b/src/Xezilaires/Attribute/Options.php @@ -11,13 +11,8 @@ * with this source code in the file LICENSE. */ -namespace Xezilaires\Annotation; +namespace Xezilaires\Attribute; -/** - * @Annotation - * @Target({"CLASS"}) - * @NamedArgumentConstructor - */ #[\Attribute(\Attribute::TARGET_CLASS)] final class Options { diff --git a/src/Xezilaires/Annotation/Reference.php b/src/Xezilaires/Attribute/Reference.php similarity index 91% rename from src/Xezilaires/Annotation/Reference.php rename to src/Xezilaires/Attribute/Reference.php index abc3ea3..871d6fe 100644 --- a/src/Xezilaires/Annotation/Reference.php +++ b/src/Xezilaires/Attribute/Reference.php @@ -11,7 +11,7 @@ * with this source code in the file LICENSE. */ -namespace Xezilaires\Annotation; +namespace Xezilaires\Attribute; /** * Marker interface Reference. diff --git a/src/Xezilaires/Exception/AnnotationException.php b/src/Xezilaires/Exception/AnnotationException.php deleted file mode 100644 index b370d77..0000000 --- a/src/Xezilaires/Exception/AnnotationException.php +++ /dev/null @@ -1,38 +0,0 @@ - $references - */ - public static function tooManyReferencesDefined(\ReflectionProperty $property, array $references): self - { - return new self('Too many references defined for '.$property->getName()); - } - - public static function failedCreatingAnnotationReader(DoctrineAnnotationException $exception): self - { - return new self('Failed creating annotation reader', 0, $exception); - } -} diff --git a/src/Xezilaires/Exception/AttributeException.php b/src/Xezilaires/Exception/AttributeException.php new file mode 100644 index 0000000..e4755f3 --- /dev/null +++ b/src/Xezilaires/Exception/AttributeException.php @@ -0,0 +1,32 @@ + $references + */ + public static function tooManyReferencesDefined(\ReflectionProperty $property, array $references): self + { + return new self('Too many references defined for '.$property->getName()); + } +} diff --git a/src/Xezilaires/Metadata/Annotation/AnnotationDriver.php b/src/Xezilaires/Metadata/Attribute/AttributeDriver.php similarity index 56% rename from src/Xezilaires/Metadata/Annotation/AnnotationDriver.php rename to src/Xezilaires/Metadata/Attribute/AttributeDriver.php index 3664349..29f5b1b 100644 --- a/src/Xezilaires/Metadata/Annotation/AnnotationDriver.php +++ b/src/Xezilaires/Metadata/Attribute/AttributeDriver.php @@ -11,36 +11,17 @@ * with this source code in the file LICENSE. */ -namespace Xezilaires\Metadata\Annotation; +namespace Xezilaires\Metadata\Attribute; -use Doctrine\Common\Annotations\AnnotationReader; -use Xezilaires\Annotation; -use Xezilaires\Exception\AnnotationException; +use Xezilaires\Attribute; +use Xezilaires\Exception\AttributeException; use Xezilaires\Metadata\ArrayReference; use Xezilaires\Metadata\ColumnReference; use Xezilaires\Metadata\HeaderReference; use Xezilaires\Metadata\Mapping; -final class AnnotationDriver +final class AttributeDriver { - private AnnotationReader $reader; - - /** - * @throws \RuntimeException if Doctrine's Annotations component is not available - */ - public function __construct(?AnnotationReader $reader = null) - { - if (false === class_exists(AnnotationReader::class)) { - throw new \RuntimeException('Xezilaires annotations support requires Doctrine Annotations component. Install "doctrine/annotations" to use it.'); - } - - try { - $this->reader = $reader ?? new AnnotationReader(); - } catch (\Doctrine\Common\Annotations\AnnotationException $exception) { - throw AnnotationException::failedCreatingAnnotationReader($exception); - } - } - /** * @throws \ReflectionException * @@ -60,17 +41,17 @@ private function getColumns(\ReflectionClass $reflectionClass): array { $columns = []; foreach ($reflectionClass->getProperties() as $reflectionProperty) { - $arrayAnnotation = $this->getPropertyAnnotationOrAttribute( + $arrayAnnotation = $this->getPropertyAttribute( $reflectionProperty, - Annotation\ArrayReference::class + Attribute\ArrayReference::class ); - $columnAnnotation = $this->getPropertyAnnotationOrAttribute( + $columnAnnotation = $this->getPropertyAttribute( $reflectionProperty, - Annotation\ColumnReference::class + Attribute\ColumnReference::class ); - $headerAnnotation = $this->getPropertyAnnotationOrAttribute( + $headerAnnotation = $this->getPropertyAttribute( $reflectionProperty, - Annotation\HeaderReference::class + Attribute\HeaderReference::class ); if ($arrayAnnotation === null && $columnAnnotation === null && $headerAnnotation === null) { @@ -80,7 +61,7 @@ private function getColumns(\ReflectionClass $reflectionClass): array if (($arrayAnnotation xor $columnAnnotation xor $headerAnnotation) === false) { // if any is set, only one is allowed - throw AnnotationException::tooManyReferencesDefined($reflectionProperty, [$arrayAnnotation, $columnAnnotation, $headerAnnotation]); + throw AttributeException::tooManyReferencesDefined($reflectionProperty, [$arrayAnnotation, $columnAnnotation, $headerAnnotation]); } switch (true) { @@ -98,7 +79,7 @@ private function getColumns(\ReflectionClass $reflectionClass): array $reference = new ArrayReference($references); break; default: - throw AnnotationException::unsupportedAnnotation(); + throw AttributeException::unsupportedAttribute(); } $columns[$reflectionProperty->getName()] = $reference; @@ -109,7 +90,7 @@ private function getColumns(\ReflectionClass $reflectionClass): array private function getOptions(\ReflectionClass $reflectionClass, ?array $additionalOptions = null): array { - $options = $this->getClassAnnotationOrAttribute($reflectionClass, Annotation\Options::class); + $options = $this->getClassAttribute($reflectionClass, Attribute\Options::class); if ($additionalOptions !== null) { $options = array_replace($options, $additionalOptions); } @@ -117,20 +98,13 @@ private function getOptions(\ReflectionClass $reflectionClass, ?array $additiona return array_filter($options); } - private function createReference(Annotation\Reference $annotation): ColumnReference|HeaderReference + private function createReference(Attribute\Reference $annotation): ColumnReference|HeaderReference { - switch (true) { - case $annotation instanceof Annotation\ColumnReference: - $reference = new ColumnReference($annotation->column); - break; - case $annotation instanceof Annotation\HeaderReference: - $reference = new HeaderReference($annotation->header); - break; - default: - throw AnnotationException::unsupportedAnnotation(); - } - - return $reference; + return match (true) { + $annotation instanceof Attribute\ColumnReference => new ColumnReference($annotation->column), + $annotation instanceof Attribute\HeaderReference => new HeaderReference($annotation->header), + default => throw AttributeException::unsupportedAttribute(), + }; } /** @@ -138,14 +112,14 @@ private function createReference(Annotation\Reference $annotation): ColumnRefere * * @param class-string $name */ - private function getClassAnnotationOrAttribute(\ReflectionClass $reflection, string $name): array + private function getClassAttribute(\ReflectionClass $reflection, string $name): array { $attribute = current($reflection->getAttributes($name)); if ($attribute !== false) { return $attribute->getArguments(); } - return (array) $this->reader->getClassAnnotation($reflection, $name); + return []; } /** @@ -155,13 +129,13 @@ private function getClassAnnotationOrAttribute(\ReflectionClass $reflection, str * * @phpstan-return T|null */ - private function getPropertyAnnotationOrAttribute(\ReflectionProperty $reflection, string $name) + private function getPropertyAttribute(\ReflectionProperty $reflection, string $name) { $attribute = current($reflection->getAttributes($name)); if ($attribute !== false) { return $attribute->newInstance(); } - return $this->reader->getPropertyAnnotation($reflection, $name); + return null; } } diff --git a/src/Xezilaires/Test/Functional/FunctionalTestCase.php b/src/Xezilaires/Test/Functional/FunctionalTestCase.php index f1028a1..38c885c 100644 --- a/src/Xezilaires/Test/Functional/FunctionalTestCase.php +++ b/src/Xezilaires/Test/Functional/FunctionalTestCase.php @@ -18,8 +18,8 @@ use Symfony\Component\Serializer\Serializer; use Xezilaires\Exception\MappingException; use Xezilaires\Exception\SpreadsheetException; -use Xezilaires\Metadata\Annotation\AnnotationDriver; use Xezilaires\Metadata\ArrayReference; +use Xezilaires\Metadata\Attribute\AttributeDriver; use Xezilaires\Metadata\ColumnReference; use Xezilaires\Metadata\HeaderReference; use Xezilaires\Metadata\Mapping; @@ -113,7 +113,7 @@ public function testCanLoadFlatFixtureWithArrayReference(): void } /** - * @uses \Xezilaires\Annotation\HeaderReference + * @uses \Xezilaires\Attribute\HeaderReference */ public function testCanLoadSparseFixtureWithHeaderReference(): void { @@ -140,19 +140,19 @@ public function testCanLoadSparseFixtureWithHeaderReference(): void } /** - * @uses \Xezilaires\Annotation\ArrayReference - * @uses \Xezilaires\Annotation\ColumnReference - * @uses \Xezilaires\Annotation\HeaderReference - * @uses \Xezilaires\Annotation\Options - * @uses \Xezilaires\Metadata\Annotation\AnnotationDriver - * * @throws \ReflectionException * @throws \RuntimeException * @throws \ReflectionException + * + * @uses \Xezilaires\Attribute\ArrayReference + * @uses \Xezilaires\Attribute\ColumnReference + * @uses \Xezilaires\Attribute\HeaderReference + *@uses \Xezilaires\Attribute\Options + * @uses \Xezilaires\Metadata\Attribute\AttributeDriver */ public function testCanLoadSparseFixtureWithAnnotations(): void { - $driver = new AnnotationDriver(); + $driver = new AttributeDriver(); $mapping = $driver->getMetadataMapping(Product::class); $iterator = $this->createIterator( @@ -168,18 +168,18 @@ public function testCanLoadSparseFixtureWithAnnotations(): void } /** - * @uses \Xezilaires\Annotation\ColumnReference - * @uses \Xezilaires\Annotation\HeaderReference - * @uses \Xezilaires\Annotation\Options - * @uses \Xezilaires\Metadata\Annotation\AnnotationDriver - * * @throws \ReflectionException * @throws \RuntimeException * @throws \ReflectionException + * + * @uses \Xezilaires\Attribute\ColumnReference + * @uses \Xezilaires\Attribute\HeaderReference + * @uses \Xezilaires\Attribute\Options + *@uses \Xezilaires\Metadata\Attribute\AttributeDriver */ public function testCanLoadSparseFixtureWithNativeAttributes(): void { - $driver = new AnnotationDriver(); + $driver = new AttributeDriver(); $mapping = $driver->getMetadataMapping(ProductWithAttributes::class); $iterator = $this->createIterator( diff --git a/src/Xezilaires/Test/Model/Product.php b/src/Xezilaires/Test/Model/Product.php index 0b9ec56..2181612 100644 --- a/src/Xezilaires/Test/Model/Product.php +++ b/src/Xezilaires/Test/Model/Product.php @@ -15,7 +15,7 @@ use Symfony\Component\Serializer\Annotation as Serializer; use Symfony\Component\Validator\Constraints as Assert; -use Xezilaires\Annotation as XLS; +use Xezilaires\Attribute as XLS; #[XLS\Options(start: 2, header: 1)] final class Product diff --git a/src/Xezilaires/Test/Model/ProductWithAttributes.php b/src/Xezilaires/Test/Model/ProductWithAttributes.php index fedaf36..701d608 100644 --- a/src/Xezilaires/Test/Model/ProductWithAttributes.php +++ b/src/Xezilaires/Test/Model/ProductWithAttributes.php @@ -13,7 +13,7 @@ namespace Xezilaires\Test\Model; -use Xezilaires\Annotation as XLS; +use Xezilaires\Attribute as XLS; #[XLS\Options(header: 1, start: 2)] final class ProductWithAttributes diff --git a/src/Xezilaires/composer.json b/src/Xezilaires/composer.json index 2b4e904..81be64d 100644 --- a/src/Xezilaires/composer.json +++ b/src/Xezilaires/composer.json @@ -15,7 +15,6 @@ "symfony/serializer": "^6.4 || ^7.0" }, "require-dev": { - "doctrine/annotations": "^1.12", "nyholm/nsa": "^1.1", "openspout/openspout": "^4.0", "phpunit/phpunit": "^9.6", From bafc0d769ca7a94bfb7c89e8fe052dd082b26e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Mon, 29 Jan 2024 10:04:10 +0100 Subject: [PATCH 2/4] chore: remove Doctrine Annotations --- src/Bridge/PhpSpreadsheet/composer.json | 2 +- src/Bridge/Spout/composer.json | 2 +- src/Bridge/Symfony/composer.json | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Bridge/PhpSpreadsheet/composer.json b/src/Bridge/PhpSpreadsheet/composer.json index 30d67a0..b212f0b 100644 --- a/src/Bridge/PhpSpreadsheet/composer.json +++ b/src/Bridge/PhpSpreadsheet/composer.json @@ -12,7 +12,7 @@ "require": { "php": "^8.2", "phpoffice/phpspreadsheet": "^1.29", - "sigwin/xezilaires": "^0.6" + "sigwin/xezilaires": "^1.0" }, "require-dev": { "phpunit/phpunit": "^9.6", diff --git a/src/Bridge/Spout/composer.json b/src/Bridge/Spout/composer.json index 9cf7bdf..fc74df4 100644 --- a/src/Bridge/Spout/composer.json +++ b/src/Bridge/Spout/composer.json @@ -12,7 +12,7 @@ "require": { "php": "^8.2", "openspout/openspout": "^4.0", - "sigwin/xezilaires": "^0.6" + "sigwin/xezilaires": "^1.0" }, "require-dev": { "nyholm/nsa": "^1.1", diff --git a/src/Bridge/Symfony/composer.json b/src/Bridge/Symfony/composer.json index 4660357..6caf109 100644 --- a/src/Bridge/Symfony/composer.json +++ b/src/Bridge/Symfony/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": "^8.2", - "sigwin/xezilaires": "^0.6", + "sigwin/xezilaires": "^1.0", "symfony/console": "^6.4 || ^7.0", "symfony/framework-bundle": "^6.4 || ^7.0", "symfony/property-access": "^6.4 || ^7.0", @@ -22,8 +22,8 @@ "matthiasnoback/symfony-dependency-injection-test": "^4.0", "nyholm/symfony-bundle-test": "dev-master", "phpunit/phpunit": "^9.6", - "sigwin/xezilaires-phpspreadsheet": "^0.6", - "sigwin/xezilaires-spout": "^0.6", + "sigwin/xezilaires-phpspreadsheet": "^1.0", + "sigwin/xezilaires-spout": "^1.0", "symfony/phpunit-bridge": "^6.4 || ^7.0" }, "conflict": { From ef4a0f8cc1ec096d9cf5dbe8f6d1c6dd631092e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Mon, 29 Jan 2024 10:14:51 +0100 Subject: [PATCH 3/4] chore: remove Doctrine Annotations --- README.md | 22 ++++++------- .../Metadata/Attribute/AttributeDriver.php | 32 +++++++++---------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 8ad9b6b..f17b8ef 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ An OEM *(Object Excel Manager)*, if you will. ## Example usage -### Without annotations +### Without attributes ```php class Product @@ -54,19 +54,16 @@ $iterator = $iteratorFactory->fromFile( ); ``` -### With annotations +### With attributes ```php -use Xezilaires\Annotation as XLS; +use Xezilaires\Attribute as XLS; -/** - * @XLS\Options(header=1, start=2) - */ + +#[XLS\Options(header=1, start=2)] class Product { - /** - * @XLS\HeaderReference(header="Name") - */ + #[@XLS\HeaderReference(header="Name")] private $name; } @@ -77,12 +74,12 @@ $normalizer = new \Xezilaires\Bridge\Symfony\Serializer\ObjectSerializer($symfon $iteratorFactory = new \Xezilaires\SpreadsheetIteratorFactory($normalizer, [ \Xezilaires\Bridge\PhpSpreadsheet\Spreadsheet::class, ]); -$annotationDriver = new \Xezilaires\Metadata\Annotation\AnnotationDriver(); +$attributeDriver = new \Xezilaires\Metadata\Attribute\AttributeDriver(); $iterator = $iteratorFactory->fromFile( // https://github.com/sigwinhq/xezilaires-dev/raw/master/src/Xezilaires/Test/resources/fixtures/products.xlsx new \SplFileObject(__DIR__.'/../../src/Xezilaires/Test/resources/fixtures/products.xlsx'), - $annotationDriver->getMetadataMapping(Product::class, ['reverse' => true]) + $attributeDriver->getMetadataMapping(Product::class, ['reverse' => true]) ); ``` @@ -107,8 +104,7 @@ Features included: *(using either `phpoffice/PhpSpreadsheet` or `openspout/openspout`)* - **Denormalization / normalization** support *(using `symfony/serializer`, from / to all supported formats)* -- **Annotations** support -*(using `doctrine/annotations`)* +- Attributes support - mapping via **column names** or **header labels** *(saying "Map header label `PrdctEN` to property `product`")* - **A Symfony bundle** diff --git a/src/Xezilaires/Metadata/Attribute/AttributeDriver.php b/src/Xezilaires/Metadata/Attribute/AttributeDriver.php index 29f5b1b..1fda1de 100644 --- a/src/Xezilaires/Metadata/Attribute/AttributeDriver.php +++ b/src/Xezilaires/Metadata/Attribute/AttributeDriver.php @@ -41,40 +41,40 @@ private function getColumns(\ReflectionClass $reflectionClass): array { $columns = []; foreach ($reflectionClass->getProperties() as $reflectionProperty) { - $arrayAnnotation = $this->getPropertyAttribute( + $arrayAttribute = $this->getPropertyAttribute( $reflectionProperty, Attribute\ArrayReference::class ); - $columnAnnotation = $this->getPropertyAttribute( + $columnAttribute = $this->getPropertyAttribute( $reflectionProperty, Attribute\ColumnReference::class ); - $headerAnnotation = $this->getPropertyAttribute( + $headerAttribute = $this->getPropertyAttribute( $reflectionProperty, Attribute\HeaderReference::class ); - if ($arrayAnnotation === null && $columnAnnotation === null && $headerAnnotation === null) { + if ($arrayAttribute === null && $columnAttribute === null && $headerAttribute === null) { // property not managed, skip continue; } - if (($arrayAnnotation xor $columnAnnotation xor $headerAnnotation) === false) { + if (($arrayAttribute xor $columnAttribute xor $headerAttribute) === false) { // if any is set, only one is allowed - throw AttributeException::tooManyReferencesDefined($reflectionProperty, [$arrayAnnotation, $columnAnnotation, $headerAnnotation]); + throw AttributeException::tooManyReferencesDefined($reflectionProperty, [$arrayAttribute, $columnAttribute, $headerAttribute]); } switch (true) { - case $columnAnnotation !== null: - $reference = $this->createReference($columnAnnotation); + case $columnAttribute !== null: + $reference = $this->createReference($columnAttribute); break; - case $headerAnnotation !== null: - $reference = $this->createReference($headerAnnotation); + case $headerAttribute !== null: + $reference = $this->createReference($headerAttribute); break; - case $arrayAnnotation !== null: + case $arrayAttribute !== null: $references = []; - foreach ($arrayAnnotation->references as $annotation) { - $references[] = $this->createReference($annotation); + foreach ($arrayAttribute->references as $attribute) { + $references[] = $this->createReference($attribute); } $reference = new ArrayReference($references); break; @@ -98,11 +98,11 @@ private function getOptions(\ReflectionClass $reflectionClass, ?array $additiona return array_filter($options); } - private function createReference(Attribute\Reference $annotation): ColumnReference|HeaderReference + private function createReference(Attribute\Reference $attribute): ColumnReference|HeaderReference { return match (true) { - $annotation instanceof Attribute\ColumnReference => new ColumnReference($annotation->column), - $annotation instanceof Attribute\HeaderReference => new HeaderReference($annotation->header), + $attribute instanceof Attribute\ColumnReference => new ColumnReference($attribute->column), + $attribute instanceof Attribute\HeaderReference => new HeaderReference($attribute->header), default => throw AttributeException::unsupportedAttribute(), }; } From a0fa0e69acc34a108220f437eb008f648f7c559e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Mon, 29 Jan 2024 10:16:57 +0100 Subject: [PATCH 4/4] chore: remove Doctrine Annotations --- src/Xezilaires/Test/Functional/FunctionalTestCase.php | 2 +- src/Xezilaires/Test/Model/Product.php | 2 +- src/Xezilaires/psalm.xml.dist | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Xezilaires/Test/Functional/FunctionalTestCase.php b/src/Xezilaires/Test/Functional/FunctionalTestCase.php index 38c885c..86d7a7e 100644 --- a/src/Xezilaires/Test/Functional/FunctionalTestCase.php +++ b/src/Xezilaires/Test/Functional/FunctionalTestCase.php @@ -150,7 +150,7 @@ public function testCanLoadSparseFixtureWithHeaderReference(): void *@uses \Xezilaires\Attribute\Options * @uses \Xezilaires\Metadata\Attribute\AttributeDriver */ - public function testCanLoadSparseFixtureWithAnnotations(): void + public function testCanLoadSparseFixtureWithAttributes(): void { $driver = new AttributeDriver(); $mapping = $driver->getMetadataMapping(Product::class); diff --git a/src/Xezilaires/Test/Model/Product.php b/src/Xezilaires/Test/Model/Product.php index 2181612..3014073 100644 --- a/src/Xezilaires/Test/Model/Product.php +++ b/src/Xezilaires/Test/Model/Product.php @@ -13,7 +13,7 @@ namespace Xezilaires\Test\Model; -use Symfony\Component\Serializer\Annotation as Serializer; +use Symfony\Component\Serializer\Attribute as Serializer; use Symfony\Component\Validator\Constraints as Assert; use Xezilaires\Attribute as XLS; diff --git a/src/Xezilaires/psalm.xml.dist b/src/Xezilaires/psalm.xml.dist index e845a10..dddd175 100644 --- a/src/Xezilaires/psalm.xml.dist +++ b/src/Xezilaires/psalm.xml.dist @@ -24,7 +24,7 @@ - + @@ -36,7 +36,7 @@ - +