Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dictionary enum normalizer #49

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Enum/DictionaryEnumInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Enum;

/**
* DictionaryEnumInterface.
*/
interface DictionaryEnumInterface extends \BackedEnum
{
/**
* @return string|null
*/
public function getPrefix(): ?string;

/**
* @return string
*/
public static function getTranslatorDomain(): string;
}
78 changes: 78 additions & 0 deletions Serializer/Normalizer/DictionaryEnumNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Serializer\Normalizer;

use StfalconStudio\ApiBundle\Enum\DictionaryEnumInterface;
use StfalconStudio\ApiBundle\Traits\TranslatorTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* DictionaryEnumNormalizer.
*/
class DictionaryEnumNormalizer implements NormalizerInterface
{
use TranslatorTrait;

/**
* @param mixed $data
* @param string|null $format
*
* @return bool
*/
public function supportsNormalization(mixed $data, string $format = null): bool
{
return $data instanceof DictionaryEnumInterface;
}

/**
* @param DictionaryEnumInterface $object
* @param string|null $format
* @param array $context
*
* @return array
*/
public function normalize(mixed $object, string $format = null, array $context = []): array
{
$data = [];

$this->normalizeId($object, $data);
$this->normalizeValue($object, $data);

return $data;
}

/**
* @param DictionaryEnumInterface $object
* @param array $data
*
* @return void
*/
protected function normalizeId(DictionaryEnumInterface $object, array &$data): void
{
$data['id'] = $object->value;
}

/**
* @param DictionaryEnumInterface $object
* @param array $data
*
* @return void
*/
protected function normalizeValue(DictionaryEnumInterface $object, array &$data): void
{
$data['value'] = $this->translator->trans(
id: null === $object->getPrefix() ? (string) $object->value : sprintf('%s.%s', $object->getPrefix(), $object->value),
domain: $object::getTranslatorDomain(),
);
}
}
Loading