Skip to content

feat(transformer): add ArrayShapeTransformer and support for array shape#336

Open
nlemoine wants to merge 3 commits intojolicode:mainfrom
nlemoine:feat/array-shape
Open

feat(transformer): add ArrayShapeTransformer and support for array shape#336
nlemoine wants to merge 3 commits intojolicode:mainfrom
nlemoine:feat/array-shape

Conversation

@nlemoine
Copy link
Contributor

@nlemoine nlemoine commented Mar 18, 2026

Hi @joelwurtz,

#330 added casting for array but that only works for homogeneous collections (one cast in a loop). Shapes like array{name: string, age: int, score: float} need a different approach since ArrayShapeType::getCollectionValueType() returns a union of all value types and loses the per-key info.

I added an ArrayShapeTransformer that generates per-key code instead of a uniform loop. Optional keys (nickname?) get an array_key_exists guard. Works with nested shapes, objects, DateTime, nullable fields, and collections inside shapes. Also handles object-to-object mapping when source and target shapes have different types.

Let me know if changes are needed.

@nlemoine nlemoine changed the title feat(transformer): add ArrayShapeTransformer and support for array sh… feat(transformer): add ArrayShapeTransformer and support for array shape Mar 18, 2026
}

return new ArrayShapeTransformer($fieldTransformers);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add a new factory instead, this one is mainly for collection, just set the correct priority in factory to be sure that it's executed before (also in the bundle)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

$newSource = $source->withType($fieldSourceType);
$newTarget = $target->withType($fieldTargetType);

$fieldTransformer = $this->chainTransformerFactory->getTransformer($newSource, $newTarget, $mapperMetadata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that, you only change the type but in fact it's a whole other field / sub property, since we mainly guess on type it should be ok, but there may be case of this giving the wrong code IMO, let's keep it for now (no need to change) don't want to over complexify this

@joelwurtz
Copy link
Member

I'm 👍 on this, can you move the factory in its own file and i think we will merge this after.

…taFactory and update transformers configuration
@nlemoine
Copy link
Contributor Author

Thanks for your feedback @joelwurtz. I moved the array shape handling into its own factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants