Skip to content

Commit

Permalink
fix: Fixed Attributes import
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed May 7, 2024
1 parent a0e2def commit 09884ce
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 57 deletions.
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ services:
- '@RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\ObjectConstructor'
- [
'@RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\TranslationObjectConstructor',
'@RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\AttributeObjectConstructor',
'@RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\TagObjectConstructor',
'@RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\NodeObjectConstructor',
'@RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\NodeTypeObjectConstructor',
Expand Down
4 changes: 2 additions & 2 deletions src/Model/AttributeGroupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait AttributeGroupTrait
#[
ORM\OneToMany(mappedBy: "group", targetEntity: AttributeInterface::class),
Serializer\Groups(["attribute_group"]),
Serializer\Type("ArrayCollection<RZ\Roadiz\CoreBundle\Model\AttributeInterface>")
Serializer\Type("ArrayCollection<RZ\Roadiz\CoreBundle\Entity\Attribute>")
]
protected Collection $attributes;

Expand All @@ -47,7 +47,7 @@ trait AttributeGroupTrait
orphanRemoval: true
),
Serializer\Groups(["attribute_group", "attribute", "node", "nodes_sources"]),
Serializer\Type("ArrayCollection<RZ\Roadiz\CoreBundle\Model\AttributeGroupTranslationInterface>"),
Serializer\Type("ArrayCollection<RZ\Roadiz\CoreBundle\Entity\AttributeGroupTranslation>"),
Serializer\Accessor(getter: "getAttributeGroupTranslations", setter: "setAttributeGroupTranslations")
]
protected Collection $attributeGroupTranslations;
Expand Down
8 changes: 5 additions & 3 deletions src/Model/AttributeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\Common\Collections\Collection;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\AttributeGroup;
use RZ\Roadiz\CoreBundle\Entity\AttributeTranslation;
use RZ\Roadiz\Utils\StringHandler;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
Expand Down Expand Up @@ -60,12 +62,12 @@ trait AttributeTrait
ORM\JoinColumn(name: "group_id", onDelete: "SET NULL"),
Serializer\Groups(["attribute", "node", "nodes_sources"]),
SymfonySerializer\Groups(["attribute", "node", "nodes_sources"]),
Serializer\Type("RZ\Roadiz\CoreBundle\Model\AttributeGroupInterface")
Serializer\Type(AttributeGroup::class)
]
protected ?AttributeGroupInterface $group = null;

/**
* @var Collection<int, AttributeTranslationInterface>
* @var Collection<int, AttributeTranslation>
*/
#[
ORM\OneToMany(
Expand All @@ -77,7 +79,7 @@ trait AttributeTrait
),
Serializer\Groups(["attribute", "node", "nodes_sources"]),
SymfonySerializer\Groups(["attribute", "node", "nodes_sources"]),
Serializer\Type("ArrayCollection<RZ\Roadiz\CoreBundle\Model\AttributeTranslationInterface>"),
Serializer\Type("ArrayCollection<" . AttributeTranslation::class . ">"),
Serializer\Accessor(getter: "getAttributeTranslations", setter: "setAttributeTranslations")
]
protected Collection $attributeTranslations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@

abstract class AbstractTypedObjectConstructor implements TypedObjectConstructorInterface
{
protected ObjectManager $entityManager;
protected ObjectConstructorInterface $fallbackConstructor;

/**
* @param ObjectManager $entityManager
* @param ObjectConstructorInterface $fallbackConstructor
*/
public function __construct(ObjectManager $entityManager, ObjectConstructorInterface $fallbackConstructor)
{
$this->entityManager = $entityManager;
$this->fallbackConstructor = $fallbackConstructor;
public function __construct(
protected readonly ObjectManager $entityManager,
protected readonly ObjectConstructorInterface $fallbackConstructor
) {
}

/**
Expand All @@ -32,7 +25,7 @@ public function __construct(ObjectManager $entityManager, ObjectConstructorInter
*
* @return object|null
*/
abstract protected function findObject($data, DeserializationContext $context): ?object;
abstract protected function findObject(mixed $data, DeserializationContext $context): ?object;

/**
* @param object $object
Expand Down
55 changes: 55 additions & 0 deletions src/Serializer/ObjectConstructor/AttributeObjectConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor;

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\CoreBundle\Exception\EntityAlreadyExistsException;
use RZ\Roadiz\CoreBundle\Model\AttributeInterface;

final class AttributeObjectConstructor extends AbstractTypedObjectConstructor
{
public const EXCEPTION_ON_EXISTING = 'exception_on_existing_attribute';

/**
* @inheritDoc
*/
public function supports(string $className, array $data): bool
{
return (
\is_subclass_of($className, AttributeInterface::class)
) && \array_key_exists('code', $data);
}

/**
* @inheritDoc
*/
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (empty($data['code'])) {
throw new ObjectConstructionException('Attribute code can not be empty');
}
$tag = $this->entityManager
->getRepository(AttributeInterface::class)
->findOneByCode($data['code']);

if (
null !== $tag &&
$context->hasAttribute(self::EXCEPTION_ON_EXISTING) &&
true === $context->hasAttribute(self::EXCEPTION_ON_EXISTING)
) {
throw new EntityAlreadyExistsException('Attribute already exists in database.');
}

return $tag;
}

protected function fillIdentifier(object $object, array $data): void
{
if ($object instanceof AttributeInterface) {
$object->setCode($data['code']);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,13 @@
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;

class ChainDoctrineObjectConstructor implements ObjectConstructorInterface
final class ChainDoctrineObjectConstructor implements ObjectConstructorInterface
{
protected ?ObjectManager $entityManager;
/**
* @var array<TypedObjectConstructorInterface>
*/
protected array $typedObjectConstructors;
protected ObjectConstructorInterface $fallbackConstructor;

/**
* @param ObjectManager|null $entityManager
* @param ObjectConstructorInterface $fallbackConstructor
* @param array $typedObjectConstructors
*/
public function __construct(
?ObjectManager $entityManager,
ObjectConstructorInterface $fallbackConstructor,
array $typedObjectConstructors
private readonly ?ObjectManager $entityManager,
private readonly ObjectConstructorInterface $fallbackConstructor,
private readonly array $typedObjectConstructors
) {
$this->entityManager = $entityManager;
$this->typedObjectConstructors = $typedObjectConstructors;
$this->fallbackConstructor = $fallbackConstructor;
}

/**
Expand Down Expand Up @@ -103,7 +88,6 @@ public function construct(

foreach ($classMetadata->getIdentifierFieldNames() as $name) {
if (
isset($metadata->propertyMetadata[$name]) &&
isset($metadata->propertyMetadata[$name]->serializedName)
) {
$dataName = $metadata->propertyMetadata[$name]->serializedName;
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/ObjectConstructor/GroupObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\CoreBundle\Entity\Group;

class GroupObjectConstructor extends AbstractTypedObjectConstructor
final class GroupObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
Expand All @@ -21,7 +21,7 @@ public function supports(string $className, array $data): bool
/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (null === $data['name'] || $data['name'] === '') {
throw new ObjectConstructionException('Group name can not be empty');
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/ObjectConstructor/NodeObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Repository\NodeRepository;

class NodeObjectConstructor extends AbstractTypedObjectConstructor
final class NodeObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
Expand All @@ -22,7 +22,7 @@ public function supports(string $className, array $data): bool
/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (empty($data['nodeName']) && empty($data['node_name'])) {
throw new ObjectConstructionException('Node name can not be empty');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use RZ\Roadiz\CoreBundle\Entity\NodeType;

class NodeTypeObjectConstructor extends AbstractTypedObjectConstructor
final class NodeTypeObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
*/
public function supports(string $className, array $data): bool
{
return $className === NodeType::class && array_key_exists('name', $data);
return \is_subclass_of($className, NodeTypeInterface::class) &&
array_key_exists('name', $data);
}

/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (null === $data['name'] || $data['name'] === '') {
throw new ObjectConstructionException('NodeType name can not be empty');
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/ObjectConstructor/RoleObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\CoreBundle\Entity\Role;

class RoleObjectConstructor extends AbstractTypedObjectConstructor
final class RoleObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
Expand All @@ -21,7 +21,7 @@ public function supports(string $className, array $data): bool
/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (null === $data['name'] || $data['name'] === '') {
throw new ObjectConstructionException('Role name can not be empty');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\CoreBundle\Entity\SettingGroup;

class SettingGroupObjectConstructor extends AbstractTypedObjectConstructor
final class SettingGroupObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
Expand All @@ -21,7 +21,7 @@ public function supports(string $className, array $data): bool
/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (null === $data['name'] || $data['name'] === '') {
throw new ObjectConstructionException('SettingGroup name can not be empty');
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/ObjectConstructor/SettingObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\CoreBundle\Entity\Setting;

class SettingObjectConstructor extends AbstractTypedObjectConstructor
final class SettingObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
Expand All @@ -21,7 +21,7 @@ public function supports(string $className, array $data): bool
/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (null === $data['name'] || $data['name'] === '') {
throw new ObjectConstructionException('Setting name can not be empty');
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/ObjectConstructor/TagObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Exception\EntityAlreadyExistsException;

class TagObjectConstructor extends AbstractTypedObjectConstructor
final class TagObjectConstructor extends AbstractTypedObjectConstructor
{
public const EXCEPTION_ON_EXISTING_TAG = 'exception_on_existing_tag';

Expand All @@ -27,7 +27,7 @@ public function supports(string $className, array $data): bool
/**
* @inheritDoc
*/
protected function findObject($data, DeserializationContext $context): ?object
protected function findObject(mixed $data, DeserializationContext $context): ?object
{
if (empty($data['tagName']) && empty($data['tag_name'])) {
throw new ObjectConstructionException('Tag name can not be empty');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Exception\ObjectConstructionException;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;

class TranslationObjectConstructor extends AbstractTypedObjectConstructor
final class TranslationObjectConstructor extends AbstractTypedObjectConstructor
{
/**
* @inheritDoc
*/
public function supports(string $className, array $data): bool
{
return $className === Translation::class && array_key_exists('locale', $data);
return \is_subclass_of($className, TranslationInterface::class) &&
array_key_exists('locale', $data);
}

/**
Expand All @@ -28,13 +29,13 @@ protected function findObject($data, DeserializationContext $context): ?object
}

return $this->entityManager
->getRepository(Translation::class)
->getRepository(TranslationInterface::class)
->findOneByLocale($data['locale']);
}

protected function fillIdentifier(object $object, array $data): void
{
if ($object instanceof Translation) {
if ($object instanceof TranslationInterface) {
$object->setLocale($data['locale']);
$object->setName($data['locale']);
}
Expand Down

0 comments on commit 09884ce

Please sign in to comment.