Skip to content

Commit

Permalink
Add required normalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Jan 17, 2025
1 parent 9bf9cc3 commit 34f7c5b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/serializer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ services:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -400 }

Ibexa\Rest\Output\Normalizer\NativeArrayObjectNormalizer:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -500 }

Ibexa\Rest\Output\Normalizer\JsonSerializableNormalizer:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -500 }

ibexa.rest.serializer.object_normalizer:
class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
tags:
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Output/Generator/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public function getData(): DataObjectInterface
#[\Override]
public function getEncoderContext(array $data): array
{
return [];
return [
'json_encode_options' => JSON_PRETTY_PRINT,
];
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/lib/Output/Normalizer/JsonSerializableNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Output\Normalizer;

use JsonSerializable;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class JsonSerializableNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* @param array<mixed> $context
*
* {@inheritDoc}
*/
public function normalize($object, ?string $format = null, array $context = []): string
{
assert($object instanceof JsonSerializable);

return $object->jsonSerialize();
}

public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof JsonSerializable;
}
}
36 changes: 36 additions & 0 deletions src/lib/Output/Normalizer/NativeArrayObjectNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Output\Normalizer;

use ArrayObject as NativeArrayObject;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class NativeArrayObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* @param array<mixed> $context
*
* {@inheritDoc}
*/
public function normalize($object, ?string $format = null, array $context = []): mixed
{
assert($object instanceof NativeArrayObject);

return $object->count() === 0 ? $object : $this->normalizer->normalize($object, $format, $context);
}

public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof NativeArrayObject;
}
}

0 comments on commit 34f7c5b

Please # to comment.