-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathUpdateOrderDataTransformer.php
More file actions
50 lines (38 loc) · 1.6 KB
/
UpdateOrderDataTransformer.php
File metadata and controls
50 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace Acme\Application\UseCase\Command\Order\UpdateOrder;
use Acme\Application\UseCase\Command\Order\CreateOrder\CreateOrderInput;
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\Validator\ValidatorInterface;
use Ramsey\Uuid\UuidInterface;
class UpdateOrderDataTransformer implements DataTransformerInterface
{
private ValidatorInterface $validator;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function transform($object, string $to, array $context = [])
{
if (!$object instanceof UpdateOrderInput) {
throw new \InvalidArgumentException(\sprintf('Object is not an instance of %s', UpdateOrderInput::class));
}
if (!isset($context['uuid'])) {
throw new \RuntimeException(\sprintf('Missing uuid value in context'));
}
if (($uuid = $context['uuid']) && !$uuid instanceof UuidInterface) {
throw new \InvalidArgumentException(\sprintf('Given uuid must be an instance of %s', UuidInterface::class));
}
$this->validator->validate($object, $context);
$command = new UpdateOrderCommand();
$command->setUuid($uuid);
$command->setNumber($object->number);
$command->setState($object->state);
$command->setTotal($object->total);
return $command;
}
public function supportsTransformation($data, string $to, array $context = []): bool
{
return UpdateOrderInput::class === ($context['input']['class'] ?? null);
}
}