Skip to content

Commit

Permalink
Introduced normalizers for Generator state
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Oct 8, 2024
1 parent 176ea72 commit 7c5c50a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/contracts/Output/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public function getData(): object
{
throw new \LogicException(sprintf(
'%s does not maintain state',
get_class($this),
static::class,
));
}

Expand Down
23 changes: 20 additions & 3 deletions src/lib/Output/Normalizer/ArrayListNormalizer.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Rest\Output\Normalizer;

use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ArrayListNormalizer implements NormalizerInterface
final class ArrayListNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* @param \Ibexa\Rest\Output\Generator\Data\ArrayList $object
* @param array<mixed> $context
*/
public function normalize($object, ?string $format = null, array $context = [])
{
dump($object);
$data = [];
foreach ($object as $key => $value) {
$data[$key] = $this->normalizer->normalize($value, $format, $context);
}

return $data;
}

public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof ArrayList;
}
}
}
18 changes: 15 additions & 3 deletions src/lib/Output/Normalizer/ArrayObjectNormalizer.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Rest\Output\Normalizer;

use Ibexa\Rest\Output\Generator\Json\ArrayObject;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ArrayObjectNormalizer implements NormalizerInterface
{

/**
* @param array<mixed> $context
*
* @return array<mixed>
*/
public function normalize($object, ?string $format = null, array $context = []): array
{
return get_object_vars($object);
$data = get_object_vars($object);

unset($data['_ref_parent']);

foreach ($data as $key => $value) {
$data[$key] = $this->normalize($value, $format, $context);
}

return $data;
}

public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof ArrayObject;
}
}
}
23 changes: 20 additions & 3 deletions src/lib/Output/Normalizer/JsonObjectNormalizer.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Rest\Output\Normalizer;

use Ibexa\Rest\Output\Generator\Json\JsonObject;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class JsonObjectNormalizer implements NormalizerInterface
final class JsonObjectNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

/**
* @param array<mixed> $context
*
* @return array<mixed>
*/
public function normalize($object, ?string $format = null, array $context = []): array
{
return get_object_vars($object);
$vars = get_object_vars($object);

unset($vars['_ref_parent']);

foreach ($vars as $name => $value) {
$vars[$name] = $this->normalizer->normalize($value, $format, $context);
}

return $vars;
}

public function supportsNormalization($data, ?string $format = null): bool
{
return $data instanceof JsonObject;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ 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 7c5c50a

Please sign in to comment.