-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
168 additions
and
155 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/lib/Output/Generator/AbstractFieldTypeHashGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?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\Generator; | ||
|
||
use Ibexa\Rest\Output\Generator\Data\ArrayList; | ||
use Ibexa\Rest\Output\Generator\Json\ArrayObject; | ||
use Ibexa\Rest\Output\Generator\Json\JsonObject; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\Serializer\Exception\ExceptionInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
abstract class AbstractFieldTypeHashGenerator implements LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
private NormalizerInterface $normalizer; | ||
|
||
private bool $strictMode; | ||
|
||
public function __construct( | ||
NormalizerInterface $normalizer, | ||
?LoggerInterface $logger = null, | ||
bool $strictMode = false | ||
) { | ||
$this->normalizer = $normalizer; | ||
$this->logger = $logger ?? new NullLogger(); | ||
$this->strictMode = $strictMode; | ||
} | ||
|
||
/** | ||
* Generates the field type value $hashValue as a child of the given Object | ||
* using $hashElementName as the property name. | ||
*/ | ||
public function generateHashValue( | ||
JsonObject|ArrayObject|ArrayList $parent, | ||
string $hashElementName, | ||
mixed $hashValue | ||
): void { | ||
$parent->$hashElementName = $this->generateValue($parent, $hashValue); | ||
} | ||
|
||
/** | ||
* Generates and returns a JSON structure (array or object) depending on $value type | ||
* with $parent. | ||
* | ||
* If $type only contains numeric keys, the resulting structure will be an | ||
* JSON array, otherwise a JSON object | ||
* | ||
* @param array<mixed> $value | ||
*/ | ||
protected function generateArrayValue( | ||
JsonObject|ArrayObject|ArrayList $parent, | ||
array $value, | ||
): JsonObject|ArrayObject|ArrayList { | ||
if ($this->isNumericArray($value)) { | ||
return $this->generateListArray($parent, $value); | ||
} else { | ||
return $this->generateHashArray($parent, $value); | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
|
||
/** | ||
* Checks if the given $value is a purely numeric array. | ||
* | ||
* @param array<mixed> $value | ||
*/ | ||
protected function isNumericArray(array $value): bool | ||
{ | ||
foreach (array_keys($value) as $key) { | ||
if (is_string($key)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function generateObjectValue(JsonObject|ArrayObject|ArrayList $parent, object $value): mixed | ||
{ | ||
try { | ||
$value = $this->normalizer->normalize($value, 'json', ['parent' => $parent]); | ||
} catch (ExceptionInterface $e) { | ||
if ($this->strictMode) { | ||
throw $e; | ||
} | ||
$message = sprintf( | ||
'Unable to normalize value for type "%s". %s. ' | ||
. 'Ensure that a normalizer is registered with tag: "%s".', | ||
get_class($value), | ||
$e->getMessage(), | ||
'ibexa.rest.serializer.normalizer', | ||
); | ||
|
||
assert($this->logger instanceof LoggerInterface); | ||
$this->logger->error($message, [ | ||
'exception' => $e, | ||
]); | ||
|
||
$value = null; | ||
} | ||
|
||
if (is_array($value)) { | ||
return $this->generateArrayValue($parent, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Generates a JSON array from the given $hashArray with $parent. | ||
* | ||
* @param array<int> $listArray | ||
*/ | ||
abstract protected function generateListArray( | ||
JsonObject|ArrayObject|ArrayList $parent, | ||
array $listArray, | ||
): JsonObject|ArrayObject|ArrayList; | ||
|
||
/** | ||
* Generates a JSON object from the given $hashArray with $parent. | ||
* | ||
* @param array<mixed> $hashArray | ||
*/ | ||
abstract protected function generateHashArray( | ||
JsonObject|ArrayObject|ArrayList $parent, | ||
array $hashArray, | ||
): JsonObject|ArrayObject|ArrayList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters