|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; |
| 15 | + |
| 16 | +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyCustomMutation as DummyCustomMutationDocument; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Dto\OutputDto; |
| 19 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyCustomMutation; |
| 20 | + |
| 21 | +final class DummyCustomMutationDtoDataTransformer implements DataTransformerInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + * |
| 26 | + * @return object |
| 27 | + */ |
| 28 | + public function transform($object, string $to, array $context = []) |
| 29 | + { |
| 30 | + if ($object instanceof \Traversable) { |
| 31 | + foreach ($object as &$value) { |
| 32 | + $value = $this->doTransformation($value); |
| 33 | + } |
| 34 | + |
| 35 | + return $object; |
| 36 | + } |
| 37 | + |
| 38 | + return $this->doTransformation($object); |
| 39 | + } |
| 40 | + |
| 41 | + private function doTransformation($object): OutputDto |
| 42 | + { |
| 43 | + $output = new OutputDto(); |
| 44 | + $output->baz = 98; |
| 45 | + $output->bat = $object->getOperandA(); |
| 46 | + |
| 47 | + return $output; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + public function supportsTransformation($object, string $to, array $context = []): bool |
| 54 | + { |
| 55 | + if ($object instanceof \IteratorAggregate) { |
| 56 | + $iterator = $object->getIterator(); |
| 57 | + if ($iterator instanceof \Iterator) { |
| 58 | + $object = $iterator->current(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return ($object instanceof DummyCustomMutation || $object instanceof DummyCustomMutationDocument) && OutputDto::class === $to; |
| 63 | + } |
| 64 | +} |
0 commit comments