Skip to content

Commit

Permalink
Applied review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Nov 4, 2024
1 parent 8db052a commit c0a09bf
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 54 deletions.
3 changes: 1 addition & 2 deletions phpunit.integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<php>
<env name="setupFactory" value="\Ibexa\Contracts\Core\Test\Repository\SetupFactory\Legacy" />
<env name="KERNEL_CLASS" value="Ibexa\Tests\Integration\Rest\IbexaTestKernel" />
<env name="SEARCH_ENGINE" value="legacy" />
<env name="DATABASE_URL" value="sqlite://i@i/test.db" />
<env name="DATABASE_URL" value="sqlite://i@i/var/test.db" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=4&amp;max[direct]=0&amp;verbose=0"/>
</php>
<testsuites>
Expand Down
8 changes: 4 additions & 4 deletions src/contracts/Output/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Ibexa\Contracts\Rest\Output;

use Ibexa\Rest\Output\Generator\AbstractFieldTypeHashGenerator;
use Ibexa\Rest\Output\Generator\Data;
use Ibexa\Rest\Output\Generator\Json;
use Ibexa\Rest\Output\Generator\Xml;

/**
* Output generator.
Expand Down Expand Up @@ -43,12 +43,12 @@ abstract class Generator
/**
* Generator for field type hash values.
*/
protected Json\FieldTypeHashGenerator|Xml\FieldTypeHashGenerator $fieldTypeHashGenerator;
protected AbstractFieldTypeHashGenerator $fieldTypeHashGenerator;

/**
* Data structure which is build during visiting.
*/
protected Json\JsonObject|Json\ArrayObject|Data\ArrayList $json;
protected Data\DataObjectInterface $json;

public function setFormatOutput(bool $formatOutput): void
{
Expand Down Expand Up @@ -458,7 +458,7 @@ protected function checkEnd(string $type, mixed $data): void
*/
abstract public function serializeBool(mixed $boolValue): bool|string;

public function getData(): Json\JsonObject|Json\ArrayObject|Data\ArrayList
public function getData(): Data\DataObjectInterface
{
throw new \LogicException(sprintf(
'%s does not maintain state',
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/Output/VisitorAdapterNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private function createGenerator(string $format): BaseGenerator

private function createVisitor(?string $format): Visitor
{
$format = $format ?: 'json';
$format ??= 'json';

$generator = $this->createGenerator($format);

Expand Down
21 changes: 10 additions & 11 deletions src/lib/Output/Generator/AbstractFieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

namespace Ibexa\Rest\Output\Generator;

use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Json\ArrayObject;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;
use Ibexa\Rest\Output\Generator\Json\JsonObject;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function __construct(
* using $hashElementName as the property name.
*/
public function generateHashValue(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
string $hashElementName,
mixed $hashValue
): void {
Expand All @@ -57,9 +56,9 @@ public function generateHashValue(
* @param array<mixed> $value
*/
protected function generateArrayValue(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $value,
): JsonObject|ArrayObject|ArrayList {
): DataObjectInterface {
if ($this->isNumericArray($value)) {
return $this->generateListArray($parent, $value);
} else {
Expand All @@ -71,7 +70,7 @@ protected function generateArrayValue(
* Generates and returns a value based on $hashValue type, with $parent (
* if the type of $hashValue supports it).
*/
abstract protected function generateValue(JsonObject|ArrayObject|ArrayList $parent, mixed $value): mixed;
abstract protected function generateValue(DataObjectInterface $parent, mixed $value): mixed;

/**
* Checks if the given $value is a purely numeric array.
Expand All @@ -89,7 +88,7 @@ protected function isNumericArray(array $value): bool
return true;
}

protected function generateObjectValue(JsonObject|ArrayObject|ArrayList $parent, object $value): mixed
protected function generateObjectValue(DataObjectInterface $parent, object $value): mixed
{
try {
$value = $this->normalizer->normalize($value, 'json', ['parent' => $parent]);
Expand Down Expand Up @@ -126,17 +125,17 @@ protected function generateObjectValue(JsonObject|ArrayObject|ArrayList $parent,
* @param array<int> $listArray
*/
abstract protected function generateListArray(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $listArray,
): JsonObject|ArrayObject|ArrayList;
): DataObjectInterface;

/**
* Generates a JSON object from the given $hashArray with $parent.
*
* @param array<mixed> $hashArray
*/
abstract protected function generateHashArray(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $hashArray,
): JsonObject|ArrayObject|ArrayList;
): JsonObject;
}
16 changes: 6 additions & 10 deletions src/lib/Output/Generator/Data/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Output\Generator\Data;

use Ibexa\Rest\Output\Generator\Json\ArrayObject;
use Ibexa\Rest\Output\Generator\Json\JsonObject;

final class ArrayList extends \ArrayObject
final class ArrayList extends \ArrayObject implements DataObjectInterface
{
private self|JsonObject|ArrayObject|null $parent;
private ?DataObjectInterface $parent;

private string $name;

public function __construct(
string $name,
self|JsonObject|ArrayObject|null $parent,
) {
public function __construct(string $name, ?DataObjectInterface $parent)
{
$this->name = $name;
$this->parent = $parent;

parent::__construct();
}

public function getParent(): self|JsonObject|ArrayObject|null
public function getParent(): ?DataObjectInterface
{
return $this->parent;
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib/Output/Generator/Data/DataObjectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

namespace Ibexa\Rest\Output\Generator\Data;

interface DataObjectInterface
{
public function getParent(): ?self;
}
3 changes: 2 additions & 1 deletion src/lib/Output/Generator/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\Rest\Output\Generator;

use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;

class Json extends Generator
{
Expand Down Expand Up @@ -89,7 +90,7 @@ public function serializeBool(mixed $boolValue): bool
}

#[\Override]
public function getData(): Json\JsonObject|Json\ArrayObject|Data\ArrayList
public function getData(): DataObjectInterface
{
return $this->json;
}
Expand Down
11 changes: 6 additions & 5 deletions src/lib/Output/Generator/Json/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Output\Generator\Json;

use AllowDynamicProperties;
use ArrayObject as NativeArrayObject;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;

/**
* Json array object.
Expand All @@ -18,17 +19,17 @@
* parent object it is assigned to again.
*/
#[AllowDynamicProperties]
class ArrayObject extends NativeArrayObject
class ArrayObject extends NativeArrayObject implements DataObjectInterface
{
/**
* Reference to the parent node.
*/
protected self|JsonObject|ArrayList|null $_ref_parent;
protected ?DataObjectInterface $_ref_parent;

/**
* Construct from optional parent node.
*/
public function __construct(self|JsonObject|ArrayList|null $_ref_parent)
public function __construct(?DataObjectInterface $_ref_parent)
{
$this->_ref_parent = $_ref_parent;

Expand All @@ -38,7 +39,7 @@ public function __construct(self|JsonObject|ArrayList|null $_ref_parent)
/**
* Get Parent of current node.
*/
public function getParent(): self|JsonObject|ArrayList|null
public function getParent(): ?DataObjectInterface
{
return $this->_ref_parent;
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/Output/Generator/Json/FieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
namespace Ibexa\Rest\Output\Generator\Json;

use Ibexa\Rest\Output\Generator\AbstractFieldTypeHashGenerator;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;

class FieldTypeHashGenerator extends AbstractFieldTypeHashGenerator
{
protected function generateValue(JsonObject|ArrayObject|ArrayList $parent, mixed $value): mixed
protected function generateValue(DataObjectInterface $parent, mixed $value): mixed
{
if ($value === null || is_scalar($value)) {
// Will be handled accordingly on serialization
Expand All @@ -32,9 +32,9 @@ protected function generateValue(JsonObject|ArrayObject|ArrayList $parent, mixed
}

protected function generateListArray(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $listArray,
): JsonObject|ArrayObject|ArrayList {
): DataObjectInterface {
$arrayObject = new ArrayObject($parent);
foreach ($listArray as $listItem) {
$arrayObject->append($this->generateValue($arrayObject, $listItem));
Expand All @@ -44,9 +44,9 @@ protected function generateListArray(
}

protected function generateHashArray(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $hashArray,
): JsonObject|ArrayObject|ArrayList {
): JsonObject {
$object = new JsonObject($parent);
foreach ($hashArray as $hashKey => $hashItem) {
$object->$hashKey = $this->generateValue($object, $hashItem);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/Output/Generator/Json/JsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Ibexa\Rest\Output\Generator\Json;

use AllowDynamicProperties;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;

/**
* Json object.
Expand All @@ -18,25 +18,25 @@
* parent object it is assigned to again.
*/
#[AllowDynamicProperties]
class JsonObject
class JsonObject implements DataObjectInterface
{
/**
* Reference to the parent node.
*/
protected self|ArrayList|ArrayObject|null $_ref_parent;
protected ?DataObjectInterface $_ref_parent;

/**
* Construct from optional parent node.
*/
public function __construct(self|ArrayList|ArrayObject|null $_ref_parent = null)
public function __construct(?DataObjectInterface $_ref_parent = null)
{
$this->_ref_parent = $_ref_parent;
}

/**
* Get parent of the current node.
*/
public function getParent(): self|ArrayList|ArrayObject|null
public function getParent(): ?DataObjectInterface
{
return $this->_ref_parent;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Output/Generator/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\Rest\Output\Generator;

use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;
use Ibexa\Rest\Output\Normalizer\ArrayListNormalizer;
use Ibexa\Rest\Output\Normalizer\ArrayObjectNormalizer;
use Ibexa\Rest\Output\Normalizer\JsonObjectNormalizer;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function endDocument(mixed $data): string
}

#[\Override]
public function getData(): Json\JsonObject|Json\ArrayObject|Data\ArrayList
public function getData(): DataObjectInterface
{
return $this->json;
}
Expand Down
13 changes: 6 additions & 7 deletions src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
namespace Ibexa\Rest\Output\Generator\Xml;

use Ibexa\Rest\Output\Generator\AbstractFieldTypeHashGenerator;
use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Json\ArrayObject;
use Ibexa\Rest\Output\Generator\Data\DataObjectInterface;
use Ibexa\Rest\Output\Generator\Json\JsonObject;

final class FieldTypeHashGenerator extends AbstractFieldTypeHashGenerator
{
protected function generateValue(JsonObject|ArrayObject|ArrayList $parent, mixed $value): mixed
protected function generateValue(DataObjectInterface $parent, mixed $value): mixed
{
if ($value === null) {
return null;
Expand All @@ -33,9 +32,9 @@ protected function generateValue(JsonObject|ArrayObject|ArrayList $parent, mixed
}

protected function generateListArray(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $listArray,
): JsonObject|ArrayObject|ArrayList {
): DataObjectInterface {
$object = new JsonObject($parent);
$object->value = [];

Expand All @@ -49,9 +48,9 @@ protected function generateListArray(
}

protected function generateHashArray(
JsonObject|ArrayObject|ArrayList $parent,
DataObjectInterface $parent,
array $hashArray,
): JsonObject|ArrayObject|ArrayList {
): JsonObject {
$object = new JsonObject($parent);
$object->value = [];

Expand Down
5 changes: 4 additions & 1 deletion src/lib/Output/Normalizer/ArrayListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Output\Normalizer;

Expand All @@ -19,8 +20,10 @@ final class ArrayListNormalizer implements NormalizerInterface, NormalizerAwareI
/**
* @param \Ibexa\Rest\Output\Generator\Data\ArrayList $object
* @param array<mixed> $context
*
* @return array<mixed>
*/
public function normalize($object, ?string $format = null, array $context = [])
public function normalize($object, ?string $format = null, array $context = []): array
{
$data = [];
foreach ($object as $key => $value) {
Expand Down

0 comments on commit c0a09bf

Please sign in to comment.