From 348953b5d2dfb60e676c1b311d6db576d55f09c0 Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Thu, 19 Dec 2024 09:27:12 +0000 Subject: [PATCH] feat: Added new NodeTypeField.php `normalizationContext` to alter normalization groups per field basis (#29) --- composer.json | 1 + src/Field/AbstractFieldGenerator.php | 25 +++++++++++++++++++ src/Field/CustomFormsFieldGenerator.php | 18 +++++++++++++ tests/Mocks/GeneratedNodesSources/NSMock.php | 6 +++++ .../NSMock.php | 6 +++++ 5 files changed, 56 insertions(+) diff --git a/composer.json b/composer.json index 3c3aba4..304bdae 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ }, "require-dev": { "phpstan/phpstan": "^1.5.3", + "phpstan/phpdoc-parser": "<2", "phpunit/phpunit": "^9.5", "api-platform/core": "~3.3.11" }, diff --git a/src/Field/AbstractFieldGenerator.php b/src/Field/AbstractFieldGenerator.php index c27d46b..04f42ee 100644 --- a/src/Field/AbstractFieldGenerator.php +++ b/src/Field/AbstractFieldGenerator.php @@ -93,6 +93,21 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null return null; } + /** + * @return array|null + */ + protected function getNormalizationContext(): ?array + { + if (\method_exists($this->field, 'getNormalizationContext')) { + $normalizationContext = $this->field->getNormalizationContext(); + if (\is_array($normalizationContext) && !empty($normalizationContext['groups'])) { + return $normalizationContext; + } + } + + return null; + } + protected function addFieldAttributes(Property $property, PhpNamespace $namespace, bool $exclude = false): self { if ($exclude) { @@ -134,6 +149,16 @@ protected function addFieldAttributes(Property $property, PhpNamespace $namespac $this->getSerializationMaxDepth(), ]); } + + /* + * Enable different serialization context for this field. + */ + if (null !== $this->getNormalizationContext()) { + $property->addAttribute('Symfony\Component\Serializer\Attribute\Context', [ + 'normalizationContext' => $this->getNormalizationContext(), + 'groups' => $this->getSerializationGroups(), + ]); + } } if ( diff --git a/src/Field/CustomFormsFieldGenerator.php b/src/Field/CustomFormsFieldGenerator.php index 31e2977..8e31f01 100644 --- a/src/Field/CustomFormsFieldGenerator.php +++ b/src/Field/CustomFormsFieldGenerator.php @@ -23,6 +23,24 @@ protected function addSerializationAttributes(Property|Method $property): self return $this; } + protected function addFieldAnnotation(Property $property): AbstractFieldGenerator + { + parent::addFieldAnnotation($property); + + $property->addComment(''); + $property->addComment('@var '.$this->options['custom_form_class'].'[]|null'); + + return $this; + } + + protected function getNormalizationContext(): array + { + return [ + 'groups' => ['nodes_sources', 'urls'], + ...(parent::getNormalizationContext() ?? []), + ]; + } + protected function getDefaultSerializationGroups(): array { $groups = parent::getDefaultSerializationGroups(); diff --git a/tests/Mocks/GeneratedNodesSources/NSMock.php b/tests/Mocks/GeneratedNodesSources/NSMock.php index f851757..c66a0af 100644 --- a/tests/Mocks/GeneratedNodesSources/NSMock.php +++ b/tests/Mocks/GeneratedNodesSources/NSMock.php @@ -309,12 +309,18 @@ class NSMock extends NodesSources /** * Custom forms field. * (Virtual field, this var is a buffer) + * + * @var \mock\Entity\CustomForm[]|null */ #[JMS\Exclude] #[Serializer\SerializedName(serializedName: 'theForms')] #[Serializer\Groups(['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'])] #[ApiProperty(description: 'Custom forms field')] #[Serializer\MaxDepth(2)] + #[Serializer\Context( + normalizationContext: ['groups' => ['nodes_sources', 'urls']], + groups: ['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'], + )] private ?array $theForms = null; /** diff --git a/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php b/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php index 94dbfba..ba7e131 100644 --- a/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php +++ b/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php @@ -309,12 +309,18 @@ class NSMock extends NodesSources /** * Custom forms field. * (Virtual field, this var is a buffer) + * + * @var \mock\Entity\CustomForm[]|null */ #[JMS\Exclude] #[Serializer\SerializedName(serializedName: 'theForms')] #[Serializer\Groups(['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'])] #[ApiProperty(description: 'Custom forms field')] #[Serializer\MaxDepth(2)] + #[Serializer\Context( + normalizationContext: ['groups' => ['nodes_sources', 'urls']], + groups: ['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'], + )] private ?array $theForms = null; /**