Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
- Simplify logic in custom Normalizer/Denormalizer
- Set readonly in constructors
  • Loading branch information
alaugks committed Nov 2, 2024
1 parent f8dfc4a commit 79d3960
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 44 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "Tests"
run-name: Tests [${{ github.ref_name }}]
on:
push:
branches:
- main
- dev
- property-normalizer
workflow_dispatch:
jobs:
tests:
name: Tests
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- name: "Run Tests"
run: |
docker compose up -d
docker exec article_symfony_serializer composer install
docker exec article_symfony_serializer bin/phpunit
3 changes: 2 additions & 1 deletion app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"symfony/runtime": "6.4.*",
"symfony/serializer": "6.4.*",
"symfony/twig-bundle": "6.4.*",
"symfony/yaml": "6.4.*"
"symfony/yaml": "6.4.*",
"webmozart/assert": "^1.11"
},
"config": {
"allow-plugins": {
Expand Down
62 changes: 60 additions & 2 deletions app/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 11 additions & 32 deletions app/src/Normalizer/MappingTableNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use App\Normalizer\Value\BooleanValue;
use App\Normalizer\Value\StringValue;
use App\Normalizer\Value\ValueInterface;
use InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Webmozart\Assert\Assert;

class MappingTableNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
use function get_class;

class MappingTableNormalizer implements NormalizerInterface, DenormalizerInterface
{
public const TABLE = 'mapping_table';

Expand All @@ -22,16 +23,13 @@ class MappingTableNormalizer implements NormalizerInterface, DenormalizerInterfa
public function normalize(mixed $object, string $format = null, array $context = []): ?string
{
if (null === $object?->getValue() || '' === $object?->getValue()) {
return ''; // Reset value in CRM
return ''; // Reset value from property in Emarsys CRM
}

$mappingTable = $this->getMappingTable($context);
Assert::keyExists($context, self::TABLE, sprintf('MappingTable not set (%s)', get_class($object)));

$key = array_search($object->getValue(), $mappingTable);
if($key) {
return (string)$key; // Force string
}
return null;
$key = array_search($object->getValue(), $context[self::TABLE], true);
return $key ? (string)$key : null;
}

public function supportsNormalization(mixed $data, string $format = null): bool
Expand All @@ -41,33 +39,14 @@ public function supportsNormalization(mixed $data, string $format = null): bool

public function denormalize($data, $type, $format = null, array $context = array()): mixed
{
$mappingTable = $this->getMappingTable($context);

foreach ($mappingTable as $key => $value) {
if ((string)$key === $data) {
return new $type($value);
}
}
Assert::keyExists($context, self::TABLE, sprintf('MappingTable not set (%s)', $type));
$mappingTable = $context[self::TABLE];

return new $type(null);
return isset($mappingTable[$data]) ? new $type($mappingTable[$data]) : new $type(null);
}

public function supportsDenormalization($data, $type, $format = null): bool
{
return in_array($type, self::SUPPORTED_TYPES);
}

private function getMappingTable(array $context): array
{
if (!isset($context[self::TABLE]) || !is_array($context[self::TABLE])) {
throw new InvalidArgumentException('mapping_table not defined');
}

return $context[self::TABLE];
}

public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === static::class;
}
}
2 changes: 1 addition & 1 deletion app/src/Normalizer/Value/BooleanValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BooleanValue implements ValueInterface
{
public function __construct(private ?bool $value = null) {}
public function __construct(private readonly ?bool $value = null) {}

public function getValue(): ?bool
{
Expand Down
2 changes: 1 addition & 1 deletion app/src/Normalizer/Value/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class StringValue implements ValueInterface
{
public function __construct(private ?string $value = null) {}
public function __construct(private readonly ?string $value = null) {}

public function getValue(): ?string
{
Expand Down
7 changes: 1 addition & 6 deletions app/src/Service/CrmSerializerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace App\Service;

use App\Dto\ContactDto;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class CrmSerializerService
{
private Serializer $serializer; // Only Symfony Serializer

public function __construct(SerializerInterface $serializer) {
$this->serializer = $serializer;
}
public function __construct(private readonly SerializerInterface $serializer) {}

public function normalize(ContactDto $contactDto): array
{
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.6'
services:
php:
container_name: article_symfony_serializer
Expand Down

0 comments on commit 79d3960

Please sign in to comment.