Skip to content

Commit

Permalink
Allow null transformation in ModelsToArrayTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 authored and jordisala1991 committed Sep 13, 2020
1 parent 4ae6ec0 commit 98677e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(
}

/**
* @param mixed[]|null $value
* @param mixed $value
*
* @return Collection<int|string, object>|object|null
*
Expand Down
12 changes: 8 additions & 4 deletions src/Form/DataTransformer/ModelsToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,22 @@ public function transform($value)
*
* @phpstan-return Collection<array-key, T>|null
*/
public function reverseTransform($keys)
public function reverseTransform($value)
{
if (!\is_array($keys)) {
throw new UnexpectedTypeException($keys, 'array');
if (null === $value) {
return null;
}

if (!\is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}

/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = new ArrayCollection();
$notFound = [];

// optimize this into a SELECT WHERE IN query
foreach ($keys as $key) {
foreach ($value as $key) {
if ($model = $this->modelManager->find($this->class, $key)) {
$collection->add($model);
} else {
Expand Down

0 comments on commit 98677e1

Please sign in to comment.