Skip to content

Commit

Permalink
outer_element context item
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Oct 10, 2024
1 parent b08f735 commit d781e88
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 45 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,6 @@ parameters:
count: 1
path: src/lib/Output/Generator/InMemory/Xml.php

-
message: "#^Parameter \\#1 \\$data of method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\InMemory\\\\Xml\\:\\:transformDataForEncoder\\(\\) expects array, array\\|ArrayObject\\<\\(int\\|string\\), mixed\\>\\|bool\\|float\\|int\\|string\\|null given\\.$#"
count: 1
path: src/lib/Output/Generator/InMemory/Xml.php

-
message: "#^Access to an undefined property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject\\:\\:\\$\\#text\\.$#"
count: 1
Expand Down
7 changes: 0 additions & 7 deletions src/contracts/Output/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,4 @@ public function getData(): object
* @return array<mixed>
*/
abstract public function getEncoderContext(array $data): array;

/**
* @param array<mixed> $data
*
* @return array<mixed>
*/
abstract public function transformDataForEncoder(array $data): array;
}
27 changes: 23 additions & 4 deletions src/contracts/Output/VisitorAdapterNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class VisitorAdapterNormalizer implements NormalizerInterface, NormalizerA

public const string ENCODER_CONTEXT = 'ENCODER_CONTEXT';

public const string OUTER_ELEMENT = 'outer_element';

public function __construct(
private readonly ValueObjectVisitorResolverInterface $valueObjectVisitorResolver,
) {
Expand Down Expand Up @@ -104,11 +106,28 @@ private function visitValueObject(

$data = $generator->getData();

$normalizedData = $this->normalizer->normalize($data, $format, $context + [
self::CALLED_CONTEXT => true,
]);
$normalizedData = $generator->transformDataForEncoder($normalizedData);
$normalizedData = $this->normalizer->normalize(
$data,
$format,
$this->buildContext($context, $format),
);

return $normalizedData + [self::ENCODER_CONTEXT => $generator->getEncoderContext(get_object_vars($data))];
}

/**
* @param array<mixed> $context
*
* @return array<mixed>
*/
private function buildContext(array $context, ?string $format): array
{
$context += [self::CALLED_CONTEXT => true];

if ($format === 'xml') {
$context += [self::OUTER_ELEMENT => true, 'as_collection' => true];
}

return $context;
}
}
21 changes: 3 additions & 18 deletions src/lib/Output/Generator/InMemory/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ibexa\Rest\Output\Generator\InMemory;

use Ibexa\Contracts\Rest\Output\VisitorAdapterNormalizer;
use Ibexa\Rest\Output\Generator\Data;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Json;
Expand Down Expand Up @@ -91,6 +92,7 @@ public function endDocument(mixed $data): string
$vars = get_object_vars($data);
$encoderContext = $this->getEncoderContext($vars);
$encoderContext['as_collection'] = true;
$encoderContext[VisitorAdapterNormalizer::OUTER_ELEMENT] = true;

$normalizers = [
new ArrayListNormalizer(),
Expand All @@ -101,10 +103,7 @@ public function endDocument(mixed $data): string
$encoders = [new XmlEncoder()];
$serializer = new Serializer($normalizers, $encoders);

$normalizedData = $serializer->normalize($data, 'xml');
$normalizedData = $this->transformDataForEncoder($normalizedData);

return $serializer->encode($normalizedData, 'xml', $encoderContext);
return $serializer->serialize($data, 'xml', $encoderContext);
}

public function getEncoderContext(array $data): array
Expand All @@ -113,18 +112,4 @@ public function getEncoderContext(array $data): array
XmlEncoder::ROOT_NODE_NAME => array_key_first($data),
];
}

public function transformDataForEncoder(array $data): array
{
$firstKey = array_key_first($data);

if ($firstKey === null) {
return $data;
}

$data['#'] = $data[$firstKey];
unset($data[$firstKey]);

return $data;
}
}
5 changes: 0 additions & 5 deletions src/lib/Output/Generator/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,4 @@ public function getEncoderContext(array $data): array
{
return [];
}

public function transformDataForEncoder(array $data): array
{
return $data;
}
}
5 changes: 0 additions & 5 deletions src/lib/Output/Generator/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,4 @@ public function getEncoderContext(array $data): array
{
return [];
}

public function transformDataForEncoder(array $data): array
{
return $data;
}
}
7 changes: 6 additions & 1 deletion src/lib/Output/Normalizer/JsonObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Ibexa\Rest\Output\Normalizer;

use Ibexa\Contracts\Rest\Output\VisitorAdapterNormalizer;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Json\JsonObject;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
Expand All @@ -26,6 +27,9 @@ public function normalize($object, ?string $format = null, array $context = []):
{
$vars = get_object_vars($object);

$isOuterElement = $context[VisitorAdapterNormalizer::OUTER_ELEMENT] ?? false;
unset($context[VisitorAdapterNormalizer::OUTER_ELEMENT]);

$data = [];
foreach ($vars as $key => $value) {
if ($value instanceof ArrayList) {
Expand All @@ -35,7 +39,8 @@ public function normalize($object, ?string $format = null, array $context = []):
}
$data[$name] = $this->normalizer->normalize($value, $format, $context);
} else {
$data[$key] = $this->normalizer->normalize($value, $format, $context);
$modifiedKey = $isOuterElement && count($vars) === 1 ? '#' : $key;
$data[$modifiedKey] = $this->normalizer->normalize($value, $format, $context);
}
}

Expand Down

0 comments on commit d781e88

Please sign in to comment.