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\Entity\Parking; |
12
|
|
|
|
13
|
|
|
use ApiPlatform\Core\Annotation as OA; |
14
|
|
|
use App\Entity\EntityInterface; |
15
|
|
|
use App\Entity\Traits; |
16
|
|
|
use App\Validator\Constraints\NotOverlappedDates; |
17
|
|
|
use Doctrine\ORM\Mapping as ORM; |
18
|
|
|
use PascalDeVink\ShortUuid\ShortUuid; |
19
|
|
|
use Symfony\Component\Serializer\Annotation as Serializer; |
20
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @ORM\Entity() |
24
|
|
|
* @ORM\Table(name="parking_availabilities") |
25
|
|
|
* @OA\ApiResource( |
26
|
|
|
* normalizationContext={"groups"={"get"}}, |
27
|
|
|
* itemOperations={ |
28
|
|
|
* "get"={ |
29
|
|
|
* "access_control"="is_granted('PARKING_AVAILABILITY_READ')" |
30
|
|
|
* }, |
31
|
|
|
* "delete"={ |
32
|
|
|
* "controller"="\App\Controller\Api\Parking\AvailabilityDeleteAction", |
33
|
|
|
* "access_control"="is_granted('PARKING_AVAILABILITY_DELETE', object)" |
34
|
|
|
* }, |
35
|
|
|
* "put"={ |
36
|
|
|
* "denormalization_context"={"groups"={"put"}}, |
37
|
|
|
* "access_control"="is_granted('PARKING_AVAILABILITY_UPDATE', object) and is_granted('PARKING_AVAILABILITY_UPDATE', previous_object)" |
38
|
|
|
* } |
39
|
|
|
* }, |
40
|
|
|
* collectionOperations={ |
41
|
|
|
* "get"={ |
42
|
|
|
* "access_control"="is_granted('PARKING_AVAILABILITY_READ')" |
43
|
|
|
* }, |
44
|
|
|
* "post"={ |
45
|
|
|
* "denormalization_context"={"groups"={"post"}}, |
46
|
|
|
* "access_control"="is_granted('PARKING_AVAILABILITY_CREATE', object)" |
47
|
|
|
* }, |
48
|
|
|
* } |
49
|
|
|
* ) |
50
|
|
|
* @NotOverlappedDates( |
51
|
|
|
* fromDateProperty="fromDate", |
52
|
|
|
* fromDateMethod="getFromDate", |
53
|
|
|
* toDateProperty="toDate", |
54
|
|
|
* toDateMethod="getToDate", |
55
|
|
|
* ) |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
class Availability implements EntityInterface |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
use Traits\PropertyIdGeneratedTrait; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var int |
63
|
|
|
* @ORM\Column(name="places", type="integer", nullable=false) |
64
|
|
|
* |
65
|
|
|
* @Assert\PositiveOrZero() |
66
|
|
|
* |
67
|
|
|
* @Serializer\Groups({"get", "put", "post"}) |
68
|
|
|
* @OA\ApiProperty( |
69
|
|
|
* attributes={ |
70
|
|
|
* "swagger_context"={"type"="integer", "example"=100}, |
71
|
|
|
* } |
72
|
|
|
* ) |
73
|
|
|
* @OA\ApiFilter( |
74
|
|
|
* ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter::class |
75
|
|
|
* ) |
76
|
|
|
* @OA\ApiFilter( |
77
|
|
|
* ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class, |
78
|
|
|
* strategy="exact" |
79
|
|
|
* ) |
80
|
|
|
*/ |
81
|
|
|
private $places; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var \DateTimeImmutable |
85
|
|
|
* @ORM\Column(name="from_date", type="date_immutable", nullable=false) |
86
|
|
|
* |
87
|
|
|
* @Assert\Date() |
88
|
|
|
* |
89
|
|
|
* @Serializer\Groups({"get", "put", "post"}) |
90
|
|
|
* @OA\ApiProperty( |
91
|
|
|
* attributes={ |
92
|
|
|
* "swagger_context"={"type"="string", "format"="date_immutable", "example"="2000-01-01"}, |
93
|
|
|
* "normalization_context"={"date_format"="Y-m-d", "datetime_format"="Y-m-d"}, |
94
|
|
|
* } |
95
|
|
|
* ) |
96
|
|
|
* @OA\ApiFilter( |
97
|
|
|
* ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::class, |
98
|
|
|
* strategy="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::INCLUDE_NULL_BEFORE" |
99
|
|
|
* ) |
100
|
|
|
* @OA\ApiFilter( |
101
|
|
|
* ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class, |
102
|
|
|
* strategy="exact" |
103
|
|
|
* ) |
104
|
|
|
*/ |
105
|
|
|
private $fromDate; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var \DateTimeImmutable |
109
|
|
|
* @ORM\Column(name="to_date", type="date_immutable", nullable=false) |
110
|
|
|
* |
111
|
|
|
* @Assert\Date() |
112
|
|
|
* @Assert\GreaterThan(propertyPath="fromDate") |
113
|
|
|
* |
114
|
|
|
* @Serializer\Groups({"get", "put", "post"}) |
115
|
|
|
* @OA\ApiProperty( |
116
|
|
|
* attributes={ |
117
|
|
|
* "swagger_context"={"type"="string", "format"="date_immutable", "example"="2000-01-01"}, |
118
|
|
|
* "normalization_context"={"date_format"="Y-m-d", "datetime_format"="Y-m-d"}, |
119
|
|
|
* } |
120
|
|
|
* ) |
121
|
|
|
* @OA\ApiFilter( |
122
|
|
|
* ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::class, |
123
|
|
|
* strategy="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::INCLUDE_NULL_BEFORE" |
124
|
|
|
* ) |
125
|
|
|
* @OA\ApiFilter( |
126
|
|
|
* ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class, |
127
|
|
|
* strategy="exact" |
128
|
|
|
* ) |
129
|
|
|
*/ |
130
|
|
|
private $toDate; |
131
|
|
|
|
132
|
|
|
//------------------------------------------------------------------------------------------- |
133
|
|
|
|
134
|
5 |
|
public function __construct( |
135
|
|
|
int $places, |
136
|
|
|
\DateTimeImmutable $fromDate, |
137
|
|
|
\DateTimeImmutable $toDate |
138
|
|
|
) { |
139
|
5 |
|
$this->id = ShortUuid::uuid4(); |
140
|
5 |
|
$this->places = $places; |
141
|
5 |
|
$this->setFromDate($fromDate); |
142
|
5 |
|
$this->setToDate($toDate); |
143
|
5 |
|
} |
144
|
|
|
|
145
|
|
|
//------------------------------------------------------------------------------------------- |
146
|
|
|
|
147
|
5 |
|
public function getPlaces(): int |
148
|
|
|
{ |
149
|
5 |
|
return $this->places; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function setPlaces(int $places): self |
153
|
|
|
{ |
154
|
1 |
|
$this->places = $places; |
155
|
|
|
|
156
|
1 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
4 |
|
public function getFromDate(): \DateTimeImmutable |
160
|
|
|
{ |
161
|
4 |
|
return $this->fromDate; |
162
|
|
|
} |
163
|
|
|
|
164
|
5 |
|
public function setFromDate(\DateTimeImmutable $fromDate): self |
165
|
|
|
{ |
166
|
5 |
|
$this->fromDate = $fromDate; |
167
|
|
|
|
168
|
5 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
public function getToDate(): \DateTimeImmutable |
172
|
|
|
{ |
173
|
4 |
|
return $this->toDate; |
174
|
|
|
} |
175
|
|
|
|
176
|
5 |
|
public function setToDate(\DateTimeImmutable $toDate): self |
177
|
|
|
{ |
178
|
5 |
|
$this->toDate = $toDate; |
179
|
|
|
|
180
|
5 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.