-
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
6 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
namespace Ibexa\Contracts\Rest\Output; | ||
|
||
use Ibexa\Rest\Output\Generator\InMemory\Xml as InMemoryXml; | ||
use Ibexa\Rest\Output\Generator\Json; | ||
use LogicException; | ||
use Symfony\Component\Serializer\Encoder\EncoderInterface; | ||
|
@@ -38,7 +39,7 @@ public function normalize(mixed $object, ?string $format = null, array $context | |
: null; | ||
|
||
if ($eligibleVisitor instanceof ValueObjectVisitor) { | ||
return $this->visitValueObject($object, $eligibleVisitor); | ||
return $this->visitValueObject($object, $eligibleVisitor, $format); | ||
Check failure on line 42 in src/contracts/Output/VisitorAdapterNormalizer.php GitHub Actions / Unit & integration tests (8.3)
|
||
} | ||
|
||
return $this->normalizer->normalize($object, $format, $context); | ||
|
@@ -79,11 +80,14 @@ public function supportsNormalization(mixed $data, ?string $format = null, array | |
} | ||
|
||
/** | ||
* @return array<mixed> | ||
* @return array<array<mixed>, array<mixed>> | ||
*/ | ||
private function visitValueObject(object $object, ValueObjectVisitor $valueObjectVisitor): array | ||
{ | ||
$visitor = $this->createVisitor(); | ||
private function visitValueObject( | ||
Check failure on line 85 in src/contracts/Output/VisitorAdapterNormalizer.php GitHub Actions / Unit & integration tests (8.3)
|
||
object $object, | ||
ValueObjectVisitor $valueObjectVisitor, | ||
string $format | ||
): array { | ||
$visitor = $this->createVisitor($format); | ||
$generator = $visitor->getGenerator(); | ||
|
||
$generator->reset(); | ||
|
@@ -93,21 +97,26 @@ private function visitValueObject(object $object, ValueObjectVisitor $valueObjec | |
|
||
$generator->endDocument($object); | ||
|
||
return $generator->toArray(); | ||
$normalizedData = $generator->toArray(); | ||
$encoderContext = $generator->getEncoderContext($normalizedData); | ||
|
||
return [$generator->transformData($normalizedData), $encoderContext]; | ||
} | ||
|
||
private function createVisitor(): Visitor | ||
private function createVisitor(string $format): Visitor | ||
{ | ||
$fieldTypeHashGenerator = new Json\FieldTypeHashGenerator($this->normalizer); | ||
|
||
$generator = new Json($fieldTypeHashGenerator); | ||
$generator = $format === 'xml' | ||
? new InMemoryXml($fieldTypeHashGenerator) | ||
: new Json($fieldTypeHashGenerator); | ||
|
||
return new Visitor( | ||
$generator, | ||
$this->normalizer, | ||
$this->encoder, | ||
$this->valueObjectVisitorResolver, | ||
'json', | ||
$format, | ||
); | ||
} | ||
} |
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,51 @@ | ||
<?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\InMemory; | ||
|
||
use Ibexa\Rest\Output\Generator\Json; | ||
use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
|
||
final class Xml extends Json | ||
{ | ||
public function getMediaType($name): string | ||
{ | ||
return $this->generateMediaTypeWithVendor($name, 'xml', $this->vendor); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param string $value | ||
*/ | ||
public function startAttribute($name, $value): void | ||
{ | ||
$this->checkStartAttribute($name); | ||
|
||
$this->json->{'@' . $name} = $value; | ||
} | ||
|
||
public 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, | ||
); | ||
$data['#'] = $normalizedData[$topNodeName]; | ||
|
||
return $data; | ||
} | ||
|
||
public function getEncoderContext(array $data): array | ||
{ | ||
return [ | ||
XmlEncoder::ROOT_NODE_NAME => array_key_first($data), | ||
]; | ||
} | ||
} |
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