Skip to content

Commit

Permalink
Fix DataTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvw committed Dec 20, 2021
1 parent a8381bd commit fbc635f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ public function __construct(string $delimiter)
$this->delimiter = $delimiter;
}

public function transform($values): string
public function transform($value): string
{
if (!($values instanceof Collection)) {
if (!($value instanceof Collection)) {
throw new TransformationFailedException(
sprintf(
'Expected "%s", but got "%s"',
Collection::class,
is_object($values) ? get_class($values) : gettype($values)
is_object($value) ? get_class($value) : gettype($value)
)
);
}

if ($values->isEmpty()) {
if ($value->isEmpty()) {
return '';
}

return implode($this->delimiter, $values->toArray());
return implode($this->delimiter, $value->toArray());
}

public function reverseTransform($value): Collection
Expand Down
16 changes: 8 additions & 8 deletions src/Bundle/Form/DataTransformer/RecursiveTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public function __construct(DataTransformerInterface $decoratedTransformer)
$this->decoratedTransformer = $decoratedTransformer;
}

public function transform($values): Collection
public function transform($value): Collection
{
if (null === $values) {
if (null === $value) {
return new ArrayCollection();
}

$this->assertTransformationValueType($values, Collection::class);
$this->assertTransformationValueType($value, Collection::class);

return $values->map(
return $value->map(
/**
* @param mixed $value
*
Expand All @@ -47,15 +47,15 @@ function ($value) {
);
}

public function reverseTransform($values): Collection
public function reverseTransform($value): Collection
{
if (null === $values) {
if (null === $value) {
return new ArrayCollection();
}

$this->assertTransformationValueType($values, Collection::class);
$this->assertTransformationValueType($value, Collection::class);

return $values->map(
return $value->map(
/**
* @param mixed $value
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ final class ResourceToIdentifierTransformer implements DataTransformerInterface

private string $identifier;

/**
* @param string $identifier
*/
public function __construct(RepositoryInterface $repository, ?string $identifier = null)
{
$this->repository = $repository;
$this->identifier = $identifier ?? 'id';
}

/**
* @return string|null
*/
public function transform($value)
{
if (null === $value) {
Expand All @@ -46,6 +46,9 @@ public function transform($value)
return PropertyAccess::createPropertyAccessor()->getValue($value, $this->identifier);
}

/**
* @return object|null
*/
public function reverseTransform($value)
{
if (null === $value) {
Expand Down

0 comments on commit fbc635f

Please sign in to comment.