From 5a7b11350ec0b9c3424973e7610344556d30e3c8 Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Mon, 10 Feb 2025 17:44:29 +0000 Subject: [PATCH] feat: Filter node references by their defaultValued node-types (#42) --- src/Field/NodesFieldGenerator.php | 25 +++++++++++++++++-- tests/Mocks/GeneratedNodesSources/NSMock.php | 12 ++++++--- .../NSMock.php | 12 ++++++--- tests/NodeTypeAwareTestTrait.php | 4 +++ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/Field/NodesFieldGenerator.php b/src/Field/NodesFieldGenerator.php index a0f09fd..e6e756a 100644 --- a/src/Field/NodesFieldGenerator.php +++ b/src/Field/NodesFieldGenerator.php @@ -97,6 +97,26 @@ public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): s ->addComment($this->getFieldSourcesName().' NodesSources direct field buffer.') ->addComment('@var '.$this->getRepositoryClass().'[]|null'); + $nodeSourceClasses = []; + if (!empty($this->field->getDefaultValues())) { + $defaultValuesParsed = $this->field->getDefaultValuesAsArray(); + $nodeTypes = array_map( + fn ($nodeTypeName) => $this->nodeTypeResolver->get($nodeTypeName), + $defaultValuesParsed + ); + $nodeSourceClasses = array_map( + fn ($nodeType) => '\\'.$nodeType->getSourceEntityFullQualifiedClassName().'::class', + array_filter($nodeTypes) + ); + } + $repositoryClass = $this->getRepositoryClass().'::class'; + if (1 === count($nodeSourceClasses)) { + $repositoryClass = array_shift($nodeSourceClasses); + $nodeSourceClasses = ''; + } else { + $nodeSourceClasses = implode(', ', $nodeSourceClasses); + } + $this->addFieldAutodoc($property); $this->addFieldAttributes($property, $namespace, $this->isExcludingFieldFromJmsSerialization()); @@ -109,10 +129,11 @@ public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): s if (null === \$this->{$this->getFieldSourcesName()}) { if (null !== \$this->objectManager) { \$this->{$this->getFieldSourcesName()} = \$this->objectManager - ->getRepository({$this->getRepositoryClass()}::class) + ->getRepository({$repositoryClass}) ->findByNodesSourcesAndFieldNameAndTranslation( \$this, - '{$this->field->getName()}' + '{$this->field->getName()}', + [{$nodeSourceClasses}] ); } else { \$this->{$this->getFieldSourcesName()} = []; diff --git a/tests/Mocks/GeneratedNodesSources/NSMock.php b/tests/Mocks/GeneratedNodesSources/NSMock.php index aff380d..1373c75 100644 --- a/tests/Mocks/GeneratedNodesSources/NSMock.php +++ b/tests/Mocks/GeneratedNodesSources/NSMock.php @@ -341,6 +341,9 @@ class NSMock extends NodesSources * @var \mock\Entity\NodesSources[]|null * ForBar hidden nodes field. * Maecenas sed diam eget risus varius blandit sit amet non magna. + * Default values: + * - Mock + * - MockTwo */ #[JMS\Exclude] private ?array $fooBarHiddenSources = null; @@ -792,7 +795,8 @@ public function getFooBarSources(): array ->getRepository(\mock\Entity\NodesSources::class) ->findByNodesSourcesAndFieldNameAndTranslation( $this, - 'foo_bar' + 'foo_bar', + [] ); } else { $this->fooBarSources = []; @@ -827,7 +831,8 @@ public function getFooBarHiddenSources(): array ->getRepository(\mock\Entity\NodesSources::class) ->findByNodesSourcesAndFieldNameAndTranslation( $this, - 'foo_bar_hidden' + 'foo_bar_hidden', + [\tests\mocks\GeneratedNodesSources\NSMock::class, \tests\mocks\GeneratedNodesSources\NSMockTwo::class] ); } else { $this->fooBarHiddenSources = []; @@ -862,7 +867,8 @@ public function getFooBarTypedSources(): array ->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class) ->findByNodesSourcesAndFieldNameAndTranslation( $this, - 'foo_bar_typed' + 'foo_bar_typed', + [] ); } else { $this->fooBarTypedSources = []; diff --git a/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php b/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php index 7291c0d..b307e0b 100644 --- a/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php +++ b/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php @@ -341,6 +341,9 @@ class NSMock extends NodesSources * @var \mock\Entity\NodesSources[]|null * ForBar hidden nodes field. * Maecenas sed diam eget risus varius blandit sit amet non magna. + * Default values: + * - Mock + * - MockTwo */ #[JMS\Exclude] private ?array $fooBarHiddenSources = null; @@ -792,7 +795,8 @@ public function getFooBarSources(): array ->getRepository(\mock\Entity\NodesSources::class) ->findByNodesSourcesAndFieldNameAndTranslation( $this, - 'foo_bar' + 'foo_bar', + [] ); } else { $this->fooBarSources = []; @@ -827,7 +831,8 @@ public function getFooBarHiddenSources(): array ->getRepository(\mock\Entity\NodesSources::class) ->findByNodesSourcesAndFieldNameAndTranslation( $this, - 'foo_bar_hidden' + 'foo_bar_hidden', + [\tests\mocks\GeneratedNodesSources\NSMock::class, \tests\mocks\GeneratedNodesSources\NSMockTwo::class] ); } else { $this->fooBarHiddenSources = []; @@ -862,7 +867,8 @@ public function getFooBarTypedSources(): array ->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class) ->findByNodesSourcesAndFieldNameAndTranslation( $this, - 'foo_bar_typed' + 'foo_bar_typed', + [] ); } else { $this->fooBarTypedSources = []; diff --git a/tests/NodeTypeAwareTestTrait.php b/tests/NodeTypeAwareTestTrait.php index 2256598..07b00da 100644 --- a/tests/NodeTypeAwareTestTrait.php +++ b/tests/NodeTypeAwareTestTrait.php @@ -145,6 +145,10 @@ protected function getMockNodeType(): NodeTypeInterface ->setExcludedFromSerialization(true) ->setLabel('ForBar hidden nodes field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') + ->setDefaultValues(Yaml::dump([ + 'Mock', + 'MockTwo', + ])) ->setIndexed(false), (new SimpleNodeTypeField()) ->setName('foo_bar_typed')