|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Rest/RestResource.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Rest; |
|
10
|
|
|
|
|
11
|
|
|
use App\DTO\RestDtoInterface; |
|
12
|
|
|
use App\Repository\Interfaces\BaseRepositoryInterface; |
|
13
|
|
|
use App\Rest\Interfaces\RestResourceInterface; |
|
14
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
15
|
|
|
use Symfony\Contracts\Service\Attribute\Required; |
|
16
|
|
|
use UnexpectedValueException; |
|
17
|
|
|
use function array_keys; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class RestResource |
|
22
|
|
|
* |
|
23
|
|
|
* @package App\Rest |
|
24
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class RestResource implements RestResourceInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use Traits\RestResourceBaseMethods; |
|
29
|
|
|
|
|
30
|
|
|
private ValidatorInterface $validator; |
|
31
|
|
|
private string $dtoClass = ''; |
|
32
|
|
|
|
|
33
|
770 |
|
public function __construct( |
|
34
|
|
|
protected readonly BaseRepositoryInterface $repository, |
|
35
|
|
|
) { |
|
36
|
770 |
|
} |
|
37
|
|
|
|
|
38
|
91 |
|
public function getSerializerContext(): array |
|
39
|
|
|
{ |
|
40
|
91 |
|
return []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
663 |
|
public function getRepository(): BaseRepositoryInterface |
|
44
|
|
|
{ |
|
45
|
663 |
|
return $this->repository; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
64 |
|
public function getValidator(): ValidatorInterface |
|
49
|
|
|
{ |
|
50
|
64 |
|
return $this->validator; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
767 |
|
#[Required] |
|
54
|
|
|
public function setValidator(ValidatorInterface $validator): self |
|
55
|
|
|
{ |
|
56
|
767 |
|
$this->validator = $validator; |
|
57
|
|
|
|
|
58
|
767 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function getDtoClass(): string |
|
62
|
|
|
{ |
|
63
|
2 |
|
if ($this->dtoClass === '') { |
|
64
|
1 |
|
$message = sprintf( |
|
65
|
1 |
|
'DTO class not specified for \'%s\' resource', |
|
66
|
1 |
|
static::class |
|
67
|
1 |
|
); |
|
68
|
|
|
|
|
69
|
1 |
|
throw new UnexpectedValueException($message); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $this->dtoClass; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function setDtoClass(string $dtoClass): RestResourceInterface |
|
76
|
|
|
{ |
|
77
|
2 |
|
$this->dtoClass = $dtoClass; |
|
78
|
|
|
|
|
79
|
2 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
176 |
|
public function getEntityName(): string |
|
83
|
|
|
{ |
|
84
|
176 |
|
return $this->getRepository()->getEntityName(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
262 |
|
public function getReference(string $id): ?object |
|
88
|
|
|
{ |
|
89
|
262 |
|
return $this->getRepository()->getReference($id); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function getAssociations(): array |
|
93
|
|
|
{ |
|
94
|
1 |
|
return array_keys($this->getRepository()->getAssociations()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
28 |
|
public function getDtoForEntity( |
|
98
|
|
|
string $id, |
|
99
|
|
|
string $dtoClass, |
|
100
|
|
|
RestDtoInterface $dto, |
|
101
|
|
|
?bool $patch = null |
|
102
|
|
|
): RestDtoInterface { |
|
103
|
28 |
|
$patch ??= false; |
|
104
|
|
|
|
|
105
|
|
|
// Fetch entity |
|
106
|
28 |
|
$entity = $this->getEntity($id); |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Create new instance of DTO and load entity to that. |
|
110
|
|
|
* |
|
111
|
|
|
* @var RestDtoInterface $restDto |
|
112
|
|
|
* @var class-string<RestDtoInterface> $dtoClass |
|
113
|
|
|
*/ |
|
114
|
27 |
|
$restDto = (new $dtoClass()) |
|
115
|
27 |
|
->setId($id); |
|
116
|
|
|
|
|
117
|
27 |
|
if ($patch === true) { |
|
118
|
12 |
|
$restDto->load($entity); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
27 |
|
$restDto->patch($dto); |
|
122
|
|
|
|
|
123
|
27 |
|
return $restDto; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|