1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2020. |
4
|
|
|
* @author Paweł Antosiak <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Gorynych\Operation; |
10
|
|
|
|
11
|
|
|
use Gorynych\Adapter\SerializerAdapter; |
12
|
|
|
use Gorynych\Adapter\ValidatorAdapter; |
13
|
|
|
use Gorynych\Exception\NotDeserializableException; |
14
|
|
|
use Gorynych\Http\Exception\UnsupportedMediaTypeHttpException; |
15
|
|
|
use Gorynych\Resource\Exception\InvalidEntityException; |
16
|
|
|
use Gorynych\Http\Exception\BadRequestHttpException; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
trait ControllerTrait |
20
|
|
|
{ |
21
|
|
|
protected SerializerAdapter $serializer; |
22
|
|
|
protected ValidatorAdapter $validator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @required |
26
|
|
|
* @param SerializerAdapter $serializer |
27
|
|
|
*/ |
28
|
1 |
|
public function setSerializer(SerializerAdapter $serializer): void |
29
|
|
|
{ |
30
|
1 |
|
$this->serializer = $serializer; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @required |
35
|
|
|
* @param ValidatorAdapter $validator |
36
|
|
|
*/ |
37
|
1 |
|
public function setValidator(ValidatorAdapter $validator): void |
38
|
|
|
{ |
39
|
1 |
|
$this->validator = $validator; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns deserialized request body in form of entity object |
44
|
|
|
* |
45
|
|
|
* @param Request $request |
46
|
|
|
* @param string $entityClass |
47
|
|
|
* @param string|null $definition |
48
|
|
|
* @param mixed[] $context |
49
|
|
|
* @param string $format |
50
|
|
|
* @return object |
51
|
|
|
* |
52
|
|
|
* @throws UnsupportedMediaTypeHttpException |
53
|
|
|
*/ |
54
|
1 |
|
protected function deserializeBody(Request $request, string $entityClass, string $definition = null, array $context = [], string $format = 'json'): object |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
1 |
|
return $this->serializer->setup($definition)->deserialize($request->getContent(), $entityClass, $format, $context); |
58
|
|
|
} catch (NotDeserializableException $e) { |
59
|
|
|
throw new UnsupportedMediaTypeHttpException(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns normalized representation of the resource |
65
|
|
|
* |
66
|
|
|
* @param object|object[] $resource |
67
|
|
|
* @param string|null $definition |
68
|
|
|
* @param mixed[] $context |
69
|
|
|
* @return mixed[] |
70
|
|
|
*/ |
71
|
1 |
|
protected function normalizeResource($resource, string $definition = null, array $context = []): array |
72
|
|
|
{ |
73
|
1 |
|
return $this->serializer->setup($definition)->normalize($resource, $context); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Validates given entity object against specified constraint |
78
|
|
|
* |
79
|
|
|
* @param object $entity |
80
|
|
|
* @param string $constraint |
81
|
|
|
* |
82
|
|
|
* @throws BadRequestHttpException |
83
|
|
|
*/ |
84
|
1 |
|
protected function validate(object $entity, string $constraint): void |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
1 |
|
$this->validator->setup($constraint)->validate($entity); |
88
|
|
|
} catch (InvalidEntityException $e) { |
89
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
} |
93
|
|
|
|