Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
8 / 10 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| StringableCompositeValueObjectNormalizer | |
80.00% |
8 / 10 |
|
0.00% |
0 / 2 |
8.51 | |
0.00% |
0 / 1 |
| supportsNormalization | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
7.07 | |||
| normalize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\ContextConstants; |
| 5 | use Apie\Core\Lists\ItemHashmap; |
| 6 | use Apie\Core\Lists\ItemList; |
| 7 | use Apie\Core\ValueObjects\CompositeValueObject; |
| 8 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 9 | use Apie\Serializer\Context\ApieSerializerContext; |
| 10 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 11 | use ReflectionClass; |
| 12 | use Stringable; |
| 13 | |
| 14 | class StringableCompositeValueObjectNormalizer implements NormalizerInterface |
| 15 | { |
| 16 | public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool |
| 17 | { |
| 18 | $context = $apieSerializerContext->getContext(); |
| 19 | $displayAsString = ($context->hasContext(ContextConstants::GET_ALL_OBJECTS) && $context->hasContext(ContextConstants::CMS)) |
| 20 | || $context->hasContext(ContextConstants::SHOW_PROFILE); |
| 21 | if ($displayAsString && $object instanceof Stringable) { |
| 22 | if ($object instanceof ValueObjectInterface) { |
| 23 | $refl = new ReflectionClass($object); |
| 24 | return in_array(CompositeValueObject::class, $refl->getTraitNames()); |
| 25 | } |
| 26 | return $object instanceof ItemList || $object instanceof ItemHashmap; |
| 27 | } |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param Stringable&ValueObjectInterface $object |
| 33 | */ |
| 34 | public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): string |
| 35 | { |
| 36 | return (string) $object; |
| 37 | } |
| 38 | } |