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 a78bb4b commit 20b17e6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 70 deletions.
51 changes: 28 additions & 23 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ parameters:
count: 1
path: src/contracts/Output/Visitor.php

-
message: "#^Method Ibexa\\\\Contracts\\\\Rest\\\\Output\\\\VisitorAdapterNormalizer\\:\\:visitValueObject\\(\\) should return array but returns array\\|ArrayObject\\<\\(int\\|string\\), mixed\\>\\|bool\\|float\\|int\\|string\\|null\\.$#"
count: 1
path: src/contracts/Output/VisitorAdapterNormalizer.php

-
message: "#^Method Ibexa\\\\Rest\\\\FieldTypeProcessor\\\\BaseRelationProcessor\\:\\:setLocationService\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -626,27 +631,22 @@ parameters:
path: src/lib/Output/FieldTypeSerializer.php

-
message: "#^Access to an undefined property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject\\:\\:\\$\\#text\\.$#"
message: "#^Access to an undefined property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject\\:\\:\\$\\#\\.$#"
count: 1
path: src/lib/Output/Generator/Json.php
path: src/lib/Output/Generator/InMemory/Xml.php

-
message: "#^Argument of an invalid type Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject supplied for foreach, only iterables are supported\\.$#"
message: "#^Access to an undefined property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject\\:\\:\\$\\#text\\.$#"
count: 1
path: src/lib/Output/Generator/Json.php

-
message: "#^Cannot call method getParent\\(\\) on array\\.$#"
count: 3
path: src/lib/Output/Generator/Json.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:endAttribute\\(\\) has no return type specified\\.$#"
message: "#^Argument of an invalid type Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/lib/Output/Generator/Json.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:endDocument\\(\\) should return string but returns string\\|false\\.$#"
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:endAttribute\\(\\) has no return type specified\\.$#"
count: 1
path: src/lib/Output/Generator/Json.php

Expand Down Expand Up @@ -701,23 +701,13 @@ parameters:
path: src/lib/Output/Generator/Json.php

-
message: "#^Parameter \\#1 \\$parent of method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:generateHashValue\\(\\) expects Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\ArrayObject\\|Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject, array given\\.$#"
count: 1
path: src/lib/Output/Generator/Json.php

-
message: "#^Property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:\\$json \\(array\\) does not accept Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\ArrayObject\\.$#"
message: "#^Parameter \\#1 \\$parent of method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:generateHashValue\\(\\) expects Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\ArrayObject\\|Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject, Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Data\\\\ArrayList\\|Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\ArrayObject\\|Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject given\\.$#"
count: 1
path: src/lib/Output/Generator/Json.php

-
message: "#^Property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:\\$json \\(array\\) does not accept Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject\\.$#"
count: 5
path: src/lib/Output/Generator/Json.php

-
message: "#^Property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:\\$json type has no value type specified in iterable type array\\.$#"
count: 1
message: "#^Property Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\:\\:\\$json \\(Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Data\\\\ArrayList\\|Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\ArrayObject\\|Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\JsonObject\\) does not accept object\\.$#"
count: 3
path: src/lib/Output/Generator/Json.php

-
Expand Down Expand Up @@ -805,6 +795,21 @@ parameters:
count: 1
path: src/lib/Output/Generator/Xml.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\:\\:toArray\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Output/Generator/Xml.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\:\\:transformData\\(\\) has parameter \\$normalizedData with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Output/Generator/Xml.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\:\\:transformData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Output/Generator/Xml.php

-
message: "#^Parameter \\#1 \\$content of method XMLWriter\\:\\:text\\(\\) expects string, bool\\|float\\|int\\|string\\|null given\\.$#"
count: 1
Expand Down
12 changes: 12 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,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;
}

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 20b17e6

Please sign in to comment.