Skip to content

Commit

Permalink
Don't-preserve-keys mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Exter-N committed Jun 10, 2024
1 parent c691e23 commit 96c35c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Attribute/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
class Collection
{
/**
* @param string|null $indexBy property of the elements to use as keys instead of their position, can be a property merged by inline
* @param bool $preserveKeys whether to preserve keys of the collection
* @param string|null $indexBy property of the elements to use as keys instead of their position, can be a property merged by inline
*/
public function __construct(
public bool $preserveKeys = true,
public ?string $indexBy = null,
) {
}
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/SpecializedNormalizerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ protected function compileNormalizerClass(StreamWriter $fd, ResolvedClassInfo $c
$fd
->printfln('\'inline_property\' => %s,', \var_export($meta->inlineSubProperty, true))
->printfln('\'index_by_property\' => %s,', \var_export($meta->indexBySubProperty, true))
->printfln('\'preserve_keys\' => %s,', \var_export($meta->preserveKeys, true))
->printfln('\'skip_property\' => %s,', \var_export(null !== $inverseMeta && null !== $inverseMeta->type && $inverseMeta->type->isCollection() ? null : $meta->inverseSubProperty, true))
->printfln('\'inbound_property\' => %s,', \var_export($property, true))
->outdent()
Expand Down Expand Up @@ -369,7 +370,6 @@ protected function compileNormalizerClass(StreamWriter $fd, ResolvedClassInfo $c
/* ----- Breadth-first Normalization ----- */

$fd
->printfln('/** @param T $object */')
->printfln('private function normalizeBreadthFirst(T $object, ?string $format = null, array $context = []): void')
->printfln('{')
->indent()
Expand Down Expand Up @@ -492,6 +492,7 @@ protected function compileNormalizerClass(StreamWriter $fd, ResolvedClassInfo $c
$fd
->printfln('\'inline_property\' => %s,', \var_export($meta->inlineSubProperty, true))
->printfln('\'index_by_property\' => %s,', \var_export($meta->indexBySubProperty, true))
->printfln('\'preserve_keys\' => %s,', \var_export($meta->preserveKeys, true))
->printfln('\'skip_property\' => %s,', \var_export(null !== $inverseMeta && null !== $inverseMeta->type && $inverseMeta->type->isCollection() ? null : $meta->inverseSubProperty, true))
->printfln('\'inbound_property\' => %s,', \var_export($property, true))
->printfln('\'continuation\' => $inline ? ($context[\'continuation\'] ?? null) : null,', \var_export($property, true))
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/AutoNormalizableMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public function getNormalizableProperties(string $className): array
}
}
if (null !== $collectionAttribute) {
$property->preserveKeys = $collectionAttribute->preserveKeys;
$property->indexBySubProperty = $collectionAttribute->indexBy;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/NormalizableProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NormalizableProperty

public bool $alwaysNormalize = false;
public ?Type $type = null;
public bool $preserveKeys = true;
public ?string $indexBySubProperty = null;
public ?string $inlineSubProperty = null;
public ?string $inverseSubProperty = null;
Expand Down
20 changes: 16 additions & 4 deletions src/Normalizer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ public function normalize($object, $format = null, array $context = []): array|\
}

$normalized = [];
foreach ($object as $key => $element) {
$normalized[$key] = $this->normalizer->normalize($element, $format, $context);
if ($context['preserve_keys'] ?? true) {
foreach ($object as $key => $element) {
$normalized[$key] = $this->normalizer->normalize($element, $format, $context);
}
} else {
foreach ($object as $element) {
$normalized[] = $this->normalizer->normalize($element, $format, $context);
}
}

if (isset($context['index_by_property'])) {
Expand All @@ -179,7 +185,7 @@ public function normalize($object, $format = null, array $context = []): array|\
return $normalized;
}

private function normalizeBreadthFirst($object, ?string $format, array $context): void
private function normalizeBreadthFirst(iterable $object, ?string $format, array $context): void
{
$helper = $context['breadth_first_helper'];
$normalized = &$helper->getCurrentBindPoint();
Expand Down Expand Up @@ -227,12 +233,18 @@ private function normalizeBreadthFirst($object, ?string $format, array $context)
if ($empty) {
$normalized = new \stdClass();
}
} else {
} elseif ($context['preserve_keys'] ?? true) {
foreach ($object as $key => $element) {
$helper->bind($normalized[$key], $this->normalizer, $element, $format, [
'continuation' => null,
] + $context);
}
} else {
foreach ($object as $element) {
$helper->bind($normalized[], $this->normalizer, $element, $format, [
'continuation' => null,
] + $context);
}
}

if (isset($context['continuation'])) {
Expand Down

0 comments on commit 96c35c8

Please sign in to comment.