Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Feb 5, 2025
1 parent 348953b commit 25d06cb
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": ">=8.2",
"ext-json": "*",
"nette/php-generator": "^4.1",
"roadiz/nodetype-contracts": "~1.1.2",
"roadiz/nodetype-contracts": "^2.0.0",
"symfony/string": "6.4.*",
"symfony/yaml": "6.4.*",
"symfony/serializer": "6.4.*",
Expand Down
18 changes: 14 additions & 4 deletions src/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use RZ\Roadiz\EntityGenerator\Field\YamlFieldGenerator;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\String\UnicodeString;
use Symfony\Component\Yaml\Yaml;

final class EntityGenerator implements EntityGeneratorInterface
{
Expand Down Expand Up @@ -122,10 +121,9 @@ private function getFieldGenerator(NodeTypeFieldInterface $field): ?AbstractFiel
return new ManyToOneFieldGenerator($field, $this->defaultValuesResolver, $this->options);
}
if ($field->isManyToMany()) {
$configuration = Yaml::parse($field->getDefaultValues() ?? '');
$configuration = $field->getDefaultValuesAsArray();
if (
is_array($configuration)
&& isset($configuration['proxy'])
isset($configuration['proxy'])
&& !empty($configuration['proxy']['classname'])
) {
/*
Expand Down Expand Up @@ -292,6 +290,18 @@ private function addClassMethods(ClassType $classType): self
->setBody('return \''.$this->nodeType->getName().'\';')
;

$classType->addMethod('getNodeTypeColor')
->setReturnType('string')
->addAttribute('JMS\Serializer\Annotation\VirtualProperty')
->addAttribute('JMS\Serializer\Annotation\Groups', [['node_type']])
->addAttribute('JMS\Serializer\Annotation\SerializedName', ['nodeTypeColor'])
->addAttribute('Symfony\Component\Serializer\Attribute\Groups', [['node_type']])
->addAttribute('Symfony\Component\Serializer\Attribute\SerializedName', [
'serializedName' => 'nodeTypeColor',
])
->setBody('return \''.$this->nodeType->getColor().'\';')
;

$classType->addMethod('isReachable')
->addComment('$this->nodeType->isReachable() proxy.')
->addComment('@return bool Does this nodeSource is reachable over network?')
Expand Down
6 changes: 3 additions & 3 deletions src/Field/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ protected function addFieldAttributes(Property $property, PhpNamespace $namespac
if (!empty($this->field->getDescription())) {
$description .= ': '.$this->field->getDescription();
}
if ($this->field->isEnum() && null !== $defaultValues = $this->field->getDefaultValues()) {
$enumValues = explode(',', $defaultValues);
$enumValues = array_filter(array_map('trim', $enumValues));
$defaultValues = $this->field->getDefaultValuesAsArray();
if ($this->field->isEnum() && count($defaultValues) > 0) {
$enumValues = array_filter(array_map('trim', $defaultValues));
$openapiContext = array_filter([
'type' => 'string',
'enum' => $enumValues,
Expand Down
9 changes: 6 additions & 3 deletions src/Field/NodesFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ protected function getFieldSourcesName(): string
protected function hasOnlyOneNodeType(): bool
{
if (!empty($this->field->getDefaultValues())) {
return 1 === count(explode(',', $this->field->getDefaultValues()));
$defaultValuesParsed = $this->field->getDefaultValuesAsArray();

return 1 === count(array_unique($defaultValuesParsed));
}

return false;
}

protected function getRepositoryClass(): string
{
if (!empty($this->field->getDefaultValues()) && true === $this->hasOnlyOneNodeType()) {
$nodeTypeName = trim(explode(',', $this->field->getDefaultValues())[0]);
$defaultValuesParsed = $this->field->getDefaultValuesAsArray();
if (count($defaultValuesParsed) > 0 && true === $this->hasOnlyOneNodeType()) {
$nodeTypeName = trim(array_values($defaultValuesParsed)[0]);

$nodeType = $this->nodeTypeResolver->get($nodeTypeName);
if (null !== $nodeType) {
Expand Down
2 changes: 1 addition & 1 deletion tests/JoinedTableDefaultValuesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class JoinedTableDefaultValuesResolver implements DefaultValuesResolverInterface
{
public function getDefaultValuesAmongAllFields(NodeTypeFieldInterface $field): array
{
return array_map('trim', explode(',', $field->getDefaultValues()));
return array_map('trim', $field->getDefaultValuesAsArray());
}

public function getMaxDefaultValuesLengthAmongAllFields(NodeTypeFieldInterface $field): int
Expand Down
18 changes: 16 additions & 2 deletions tests/Mocks/GeneratedNodesSources/NSMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class NSMock extends NodesSources
* @var \tests\mocks\GeneratedNodesSources\NSMockTwo[]|null
* ForBar nodes typed field.
* Default values:
* MockTwo
* - MockTwo
*/
#[JMS\Exclude]
#[Serializer\SerializedName(serializedName: 'fooBarTyped')]
Expand All @@ -362,7 +362,11 @@ class NSMock extends NodesSources
/**
* ForBar layout enum.
* Default values:
* layout_odd, layout_odd_big_title, layout_even, layout_even_big_title, layout_media_grid
* - layout_odd
* - layout_odd_big_title
* - layout_even
* - layout_even_big_title
* - layout_media_grid
*/
#[Serializer\SerializedName(serializedName: 'layout')]
#[Serializer\Groups(['nodes_sources', 'nodes_sources_default'])]
Expand Down Expand Up @@ -1033,6 +1037,16 @@ public function getNodeTypeName(): string
return 'Mock';
}

#[JMS\VirtualProperty]
#[JMS\Groups(['node_type'])]
#[JMS\SerializedName('nodeTypeColor')]
#[Serializer\Groups(['node_type'])]
#[Serializer\SerializedName(serializedName: 'nodeTypeColor')]
public function getNodeTypeColor(): string
{
return '';
}

/**
* $this->nodeType->isReachable() proxy.
* @return bool Does this nodeSource is reachable over network?
Expand Down
18 changes: 16 additions & 2 deletions tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class NSMock extends NodesSources
* @var \tests\mocks\GeneratedNodesSources\NSMockTwo[]|null
* ForBar nodes typed field.
* Default values:
* MockTwo
* - MockTwo
*/
#[JMS\Exclude]
#[Serializer\SerializedName(serializedName: 'fooBarTyped')]
Expand All @@ -362,7 +362,11 @@ class NSMock extends NodesSources
/**
* ForBar layout enum.
* Default values:
* layout_odd, layout_odd_big_title, layout_even, layout_even_big_title, layout_media_grid
* - layout_odd
* - layout_odd_big_title
* - layout_even
* - layout_even_big_title
* - layout_media_grid
*/
#[Serializer\SerializedName(serializedName: 'layout')]
#[Serializer\Groups(['nodes_sources', 'nodes_sources_default'])]
Expand Down Expand Up @@ -1033,6 +1037,16 @@ public function getNodeTypeName(): string
return 'Mock';
}

#[JMS\VirtualProperty]
#[JMS\Groups(['node_type'])]
#[JMS\SerializedName('nodeTypeColor')]
#[Serializer\Groups(['node_type'])]
#[Serializer\SerializedName(serializedName: 'nodeTypeColor')]
public function getNodeTypeColor(): string
{
return '';
}

/**
* $this->nodeType->isReachable() proxy.
* @return bool Does this nodeSource is reachable over network?
Expand Down
5 changes: 3 additions & 2 deletions tests/NodeTypeAwareTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use RZ\Roadiz\Contracts\NodeType\NodeTypeResolverInterface;
use Symfony\Component\Yaml\Yaml;

trait NodeTypeAwareTestTrait
{
Expand Down Expand Up @@ -151,13 +152,13 @@ protected function getMockNodeType(): NodeTypeInterface
->setVirtual(true)
->setLabel('ForBar nodes typed field')
->setIndexed(false)
->setDefaultValues('MockTwo'),
->setDefaultValues(Yaml::dump(['MockTwo'])),
(new SimpleNodeTypeField())
->setName('layout')
->setTypeName('enum')
->setLabel('ForBar layout enum')
->setIndexed(true)
->setDefaultValues('layout_odd, layout_odd_big_title, layout_even, layout_even_big_title, layout_media_grid'),
->setDefaultValues(Yaml::dump(['layout_odd', 'layout_odd_big_title', 'layout_even', 'layout_even_big_title', 'layout_media_grid'])),
(new SimpleNodeTypeField())
->setName('foo_many_to_one')
->setTypeName('many_to_one')
Expand Down
8 changes: 8 additions & 0 deletions tests/SimpleNodeTypeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Contracts\NodeType\SerializableInterface;
use Symfony\Component\String\UnicodeString;
use Symfony\Component\Yaml\Yaml;

final class SimpleNodeTypeField implements NodeTypeFieldInterface, SerializableInterface
{
Expand Down Expand Up @@ -92,6 +93,13 @@ public function setDefaultValues(?string $defaultValues): SimpleNodeTypeField
return $this;
}

public function getDefaultValuesAsArray(): array
{
$defaultValues = Yaml::parse($this->defaultValues ?? '') ?? '';

return is_array($defaultValues) ? $defaultValues : [];
}

public function getGroupName(): ?string
{
return $this->groupName;
Expand Down

0 comments on commit 25d06cb

Please sign in to comment.