-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
roadiz-ci
committed
May 7, 2024
1 parent
a0e2def
commit 09884ce
Showing
14 changed files
with
95 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Serializer/ObjectConstructor/AttributeObjectConstructor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters