1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the zibios/sharep. |
7
|
|
|
* |
8
|
|
|
* (c) Zbigniew Ślązak |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace App\Api\DataTransformer\Parking; |
12
|
|
|
|
13
|
|
|
use ApiPlatform\Core\DataTransformer\DataTransformerInterface; |
14
|
|
|
use ApiPlatform\Core\Serializer\AbstractItemNormalizer; |
15
|
|
|
use ApiPlatform\Core\Validator\ValidatorInterface; |
16
|
|
|
use App\Api\Dto\Parking\ReservationInputDto; |
17
|
|
|
use App\Entity\Parking\Reservation; |
18
|
|
|
use App\Enum\Entity\ReservationTypeEnum; |
19
|
|
|
use App\Service\Parking\ReservationService; |
20
|
|
|
|
21
|
|
|
class ReservationInputTransformer implements DataTransformerInterface |
22
|
|
|
{ |
23
|
|
|
/** @var ValidatorInterface */ |
24
|
|
|
private $validator; |
25
|
|
|
/** @var ReservationService */ |
26
|
|
|
private $reservationService; |
27
|
|
|
|
28
|
1 |
|
public function __construct(ValidatorInterface $validator, ReservationService $reservationService) |
29
|
|
|
{ |
30
|
1 |
|
$this->validator = $validator; |
31
|
1 |
|
$this->reservationService = $reservationService; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ReservationInputDto $data |
36
|
|
|
*/ |
37
|
1 |
|
public function transform($data, string $to, array $context = []): Reservation |
38
|
|
|
{ |
39
|
1 |
|
$this->validator->validate($data); |
40
|
|
|
|
41
|
1 |
|
if (isset($context[AbstractItemNormalizer::OBJECT_TO_POPULATE]) |
42
|
1 |
|
&& $context[AbstractItemNormalizer::OBJECT_TO_POPULATE] instanceof Reservation) { |
43
|
|
|
/** @var Reservation $reservation */ |
44
|
|
|
$reservation = $context[AbstractItemNormalizer::OBJECT_TO_POPULATE]; |
45
|
|
|
$reservation = $this->reservationService |
46
|
|
|
->updateReservation($reservation, $data->places, new ReservationTypeEnum($data->type)); |
47
|
|
|
} else { |
48
|
1 |
|
$reservation = $this->reservationService |
49
|
1 |
|
->createReservation($data->member, $data->date, $data->places, new ReservationTypeEnum($data->type)); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $reservation; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function supportsTransformation($data, string $to, array $context = []): bool |
56
|
|
|
{ |
57
|
1 |
|
if ($data instanceof Reservation) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return Reservation::class === $to && null !== ($context['input']['class'] ?? null); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|