Skip to content

Commit

Permalink
Update Generator normalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Oct 10, 2024
1 parent bb2b396 commit 1243dd6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 47 deletions.
12 changes: 12 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,18 @@ services:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -1000 }

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

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

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

Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolverInterface: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitor'

Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver: ~
Expand Down
47 changes: 3 additions & 44 deletions src/lib/Output/Generator/InMemory/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\Rest\Output\Generator\InMemory;

use Ibexa\Rest\Output\Generator\Data;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Json;
use Ibexa\Rest\Output\Normalizer\ArrayListNormalizer;
use Ibexa\Rest\Output\Normalizer\ArrayObjectNormalizer;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function startValueElement(string $name, $value, array $attributes = []):
$jsonValue->{'#'} = $value;

Check failure on line 66 in src/lib/Output/Generator/InMemory/Xml.php

View workflow job for this annotation

GitHub Actions / Unit & integration tests (8.3)

Access to an undefined property Ibexa\Rest\Output\Generator\Json\JsonObject::$#.
}

if ($this->json instanceof Json\ArrayObject) {
if ($this->json instanceof Json\ArrayObject || $this->json instanceof ArrayList) {
$this->json->append($jsonValue);
} else {
$this->json->$name = $jsonValue;
Expand All @@ -90,6 +91,7 @@ public function endDocument(mixed $data): string
$vars = get_object_vars($data);
$encoderContext = $this->getEncoderContext($vars);
$encoderContext['as_collection'] = true;
$encoderContext['outer_element'] = true;

$normalizers = [
new ArrayListNormalizer(),
Expand All @@ -103,49 +105,6 @@ public function endDocument(mixed $data): string
return $serializer->serialize($data, 'xml', $encoderContext);
}

/**
* @param array<mixed> $normalizedData
*
* @return array<mixed>
*/
private function transformData(array $normalizedData): array
{
$topNodeName = array_key_first($normalizedData);
$data = array_filter(
$normalizedData[$topNodeName] ?? [],
static fn (string $key): bool => str_starts_with($key, '@'),
ARRAY_FILTER_USE_KEY,
);

if ($topNodeName !== null) {
$data['#'] = $normalizedData[$topNodeName];
}

return $this->clearEmptyArrays($data);
}

/**
* @param array<mixed> $array
*
* @return array<mixed>
*/
private function clearEmptyArrays(array &$array): array
{
foreach ($array as $key => &$value) {
if (is_array($value)) {
// Recursively apply the function to the nested array
$this->clearEmptyArrays($value);

// Remove the field if it's an empty array after recursion
if (empty($value)) {
unset($array[$key]);
}
}
}

return $array;
}

protected function getEncoderContext(array $data): array
{
return [
Expand Down
5 changes: 4 additions & 1 deletion src/lib/Output/Generator/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ public function startHashElement($name)

$object = new Json\JsonObject($this->json);

if ($this->json instanceof Json\ArrayObject) {
if ($this->json instanceof Json\ArrayObject || $this->json instanceof Data\ArrayList) {
$this->json->append($object);
if ($this->json instanceof Data\ArrayList) {
$this->json->setName($name);
}
$this->json = $object;
} else {
$this->json->$name = $object;
Expand Down
5 changes: 4 additions & 1 deletion src/lib/Output/Normalizer/JsonObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ public function normalize($object, ?string $format = null, array $context = []):
{
$vars = get_object_vars($object);

$isOuterElement = $context['outer_element'] ?? false;
unset($context['outer_element']);

$data = [];
foreach ($vars as $key => $value) {
if ($value instanceof ArrayList) {
$name = $value->getName();
$data[$name] = $this->normalizer->normalize($value, $format, $context);
} else {
$data[$key] = $this->normalizer->normalize($value, $format, $context);
$data[$isOuterElement && count($vars) === 1 ? '#' : $key] = $this->normalizer->normalize($value, $format, $context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function testResultContainsBookmarkElement(string $result): void
$document->loadXML($result);
$xpath = new DOMXPath($document);

dump($result);
self::assertEquals(count($this->data->items), $xpath->query($query)->length);
}

Expand Down

0 comments on commit 1243dd6

Please sign in to comment.