Skip to content

Commit

Permalink
feat: Added new NodeTypeField.php normalizationContext to alter nor…
Browse files Browse the repository at this point in the history
…malization groups per field basis (#29)
  • Loading branch information
roadiz-ci committed Dec 19, 2024
1 parent 2111c37 commit 348953b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
25 changes: 25 additions & 0 deletions src/Field/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null
return null;
}

/**
* @return array<string, mixed>|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) {
Expand Down Expand Up @@ -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 (
Expand Down
18 changes: 18 additions & 0 deletions src/Field/CustomFormsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions tests/Mocks/GeneratedNodesSources/NSMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

0 comments on commit 348953b

Please sign in to comment.