-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
4 changed files
with
83 additions
and
1 deletion.
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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |