-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCreateOrderDataTransformer.php
More file actions
38 lines (29 loc) · 1.06 KB
/
CreateOrderDataTransformer.php
File metadata and controls
38 lines (29 loc) · 1.06 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
<?php
declare(strict_types=1);
namespace Acme\Application\UseCase\Command\Order\CreateOrder;
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\Validator\ValidatorInterface;
class CreateOrderDataTransformer implements DataTransformerInterface
{
private ValidatorInterface $validator;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function transform($object, string $to, array $context = [])
{
if (!$object instanceof CreateOrderInput) {
throw new \InvalidArgumentException(\sprintf('Object is not an instance of %s', CreateOrderInput::class));
}
$this->validator->validate($object, $context);
return new CreateOrderCommand(
$object->number,
$object->state,
$object->total
);
}
public function supportsTransformation($data, string $to, array $context = []): bool
{
return CreateOrderInput::class === ($context['input']['class'] ?? null);
}
}