diff --git a/phpunit.integration.xml b/phpunit.integration.xml index 6f446a6b..2b44e4a9 100644 --- a/phpunit.integration.xml +++ b/phpunit.integration.xml @@ -5,10 +5,9 @@ beStrictAboutTodoAnnotatedTests="true" verbose="true"> - - + diff --git a/src/contracts/Output/Generator.php b/src/contracts/Output/Generator.php index ba384afc..990da574 100644 --- a/src/contracts/Output/Generator.php +++ b/src/contracts/Output/Generator.php @@ -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. @@ -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 { @@ -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', diff --git a/src/contracts/Output/VisitorAdapterNormalizer.php b/src/contracts/Output/VisitorAdapterNormalizer.php index 17f1e5e3..b1068988 100644 --- a/src/contracts/Output/VisitorAdapterNormalizer.php +++ b/src/contracts/Output/VisitorAdapterNormalizer.php @@ -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); diff --git a/src/lib/Output/Generator/AbstractFieldTypeHashGenerator.php b/src/lib/Output/Generator/AbstractFieldTypeHashGenerator.php index ceffc72b..660f2ae8 100644 --- a/src/lib/Output/Generator/AbstractFieldTypeHashGenerator.php +++ b/src/lib/Output/Generator/AbstractFieldTypeHashGenerator.php @@ -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; @@ -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 { @@ -57,9 +56,9 @@ public function generateHashValue( * @param array $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 { @@ -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. @@ -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]); @@ -126,9 +125,9 @@ protected function generateObjectValue(JsonObject|ArrayObject|ArrayList $parent, * @param array $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. @@ -136,7 +135,7 @@ abstract protected function generateListArray( * @param array $hashArray */ abstract protected function generateHashArray( - JsonObject|ArrayObject|ArrayList $parent, + DataObjectInterface $parent, array $hashArray, - ): JsonObject|ArrayObject|ArrayList; + ): JsonObject; } diff --git a/src/lib/Output/Generator/Data/ArrayList.php b/src/lib/Output/Generator/Data/ArrayList.php index 50c1cf4c..962f0ebd 100644 --- a/src/lib/Output/Generator/Data/ArrayList.php +++ b/src/lib/Output/Generator/Data/ArrayList.php @@ -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; } diff --git a/src/lib/Output/Generator/Data/DataObjectInterface.php b/src/lib/Output/Generator/Data/DataObjectInterface.php new file mode 100644 index 00000000..be272e88 --- /dev/null +++ b/src/lib/Output/Generator/Data/DataObjectInterface.php @@ -0,0 +1,14 @@ +json; } diff --git a/src/lib/Output/Generator/Json/ArrayObject.php b/src/lib/Output/Generator/Json/ArrayObject.php index 419d4d64..948024e0 100644 --- a/src/lib/Output/Generator/Json/ArrayObject.php +++ b/src/lib/Output/Generator/Json/ArrayObject.php @@ -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. @@ -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; @@ -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; } diff --git a/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php b/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php index 69456be4..77f70214 100644 --- a/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php +++ b/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php @@ -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 @@ -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)); @@ -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); diff --git a/src/lib/Output/Generator/Json/JsonObject.php b/src/lib/Output/Generator/Json/JsonObject.php index d4caad70..ff98cdb5 100644 --- a/src/lib/Output/Generator/Json/JsonObject.php +++ b/src/lib/Output/Generator/Json/JsonObject.php @@ -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. @@ -18,17 +18,17 @@ * 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; } @@ -36,7 +36,7 @@ public function __construct(self|ArrayList|ArrayObject|null $_ref_parent = null) /** * Get parent of the current node. */ - public function getParent(): self|ArrayList|ArrayObject|null + public function getParent(): ?DataObjectInterface { return $this->_ref_parent; } diff --git a/src/lib/Output/Generator/Xml.php b/src/lib/Output/Generator/Xml.php index 7dcd729d..f87d6ce7 100644 --- a/src/lib/Output/Generator/Xml.php +++ b/src/lib/Output/Generator/Xml.php @@ -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; @@ -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; } diff --git a/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php b/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php index 9c12dc01..92bbc9b9 100644 --- a/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php +++ b/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php @@ -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; @@ -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 = []; @@ -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 = []; diff --git a/src/lib/Output/Normalizer/ArrayListNormalizer.php b/src/lib/Output/Normalizer/ArrayListNormalizer.php index 46b829a2..3ead4b91 100644 --- a/src/lib/Output/Normalizer/ArrayListNormalizer.php +++ b/src/lib/Output/Normalizer/ArrayListNormalizer.php @@ -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; @@ -19,8 +20,10 @@ final class ArrayListNormalizer implements NormalizerInterface, NormalizerAwareI /** * @param \Ibexa\Rest\Output\Generator\Data\ArrayList $object * @param array $context + * + * @return array */ - 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) {