SituationFilterParameters::unsetSources()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\Melvin;
6
7
use Swis\Melvin\Enums\ActivityType;
8
use Swis\Melvin\Enums\RestrictionType;
9
use Swis\Melvin\Enums\SituationStatus;
10
use Swis\Melvin\Enums\Source;
11
use Swis\Melvin\Exceptions\InvalidArgumentException;
12
13
class SituationFilterParameters implements \JsonSerializable
14
{
15
    protected ?\DateTimeInterface $startPeriod = null;
16
17
    protected ?\DateTimeInterface $endPeriod = null;
18
19
    /**
20
     * @var \Swis\Melvin\Enums\Source[]|null
21
     */
22
    protected ?array $sources = null;
23
24
    /**
25
     * @var int[]|null
26
     */
27
    protected ?array $areaIds = null;
28
29
    protected ?int $areaBuffer = null;
30
31
    /**
32
     * @var \Swis\Melvin\Enums\SituationStatus[]|null
33
     */
34
    protected ?array $statuses = null;
35
36
    protected ?bool $published = null;
37
38
    protected ?bool $rvmConflict = null;
39
40
    /**
41
     * @var \Swis\Melvin\Enums\RestrictionType[]|null
42
     */
43
    protected ?array $restrictionTypes = null;
44
45
    /**
46
     * @var \Swis\Melvin\Enums\ActivityType[]|null
47
     */
48
    protected ?array $activityTypes = null;
49
50
    protected ?bool $includeDetours = null;
51
52
    /**
53
     * @param \DateTimeInterface|null $start
54
     * @param \DateTimeInterface|null $end
55
     *
56
     * @return $this
57
     */
58
    public function setPeriod(?\DateTimeInterface $start, ?\DateTimeInterface $end): self
59
    {
60
        $this->startPeriod = $start;
61
        $this->endPeriod = $end;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return $this
68
     */
69
    public function unsetPeriod(): self
70
    {
71
        $this->startPeriod = $this->endPeriod = null;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param \Swis\Melvin\Enums\Source[] $sources
78
     *
79
     * @return $this
80
     */
81
    public function setSources(array $sources): self
82
    {
83
        $this->validateEnums($sources, Source::class, 'sources');
84
85
        $this->sources = $sources;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @return $this
92
     */
93
    public function unsetSources(): self
94
    {
95
        $this->sources = null;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param int[] $areaIds
102
     *
103
     * @return $this
104
     */
105
    public function setAreaIds(array $areaIds): self
106
    {
107
        $this->validateTypes($areaIds, 'integer', 'areaIds');
108
109
        $this->areaIds = $areaIds;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117
    public function unsetAreaIds(): self
118
    {
119
        $this->areaIds = null;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param int $areaBuffer
126
     *
127
     * @return $this
128
     */
129
    public function setAreaBuffer(int $areaBuffer): self
130
    {
131
        $this->areaBuffer = $areaBuffer;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return $this
138
     */
139
    public function unsetAreaBuffer(): self
140
    {
141
        $this->areaBuffer = null;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param \Swis\Melvin\Enums\SituationStatus[] $statuses
148
     *
149
     * @return $this
150
     */
151
    public function setStatuses(array $statuses): self
152
    {
153
        $this->validateEnums($statuses, SituationStatus::class, 'statuses');
154
155
        $this->statuses = $statuses;
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return $this
162
     */
163
    public function unsetStatuses(): self
164
    {
165
        $this->statuses = null;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @param bool $published
172
     *
173
     * @return $this
174
     */
175
    public function setPublished(bool $published): self
176
    {
177
        $this->published = $published;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return $this
184
     */
185
    public function unsetPublished(): self
186
    {
187
        $this->published = null;
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param bool $rvmConflict
194
     *
195
     * @return $this
196
     */
197
    public function setRvmConflict(bool $rvmConflict): self
198
    {
199
        $this->rvmConflict = $rvmConflict;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return $this
206
     */
207
    public function unsetRvmConflict(): self
208
    {
209
        $this->rvmConflict = null;
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param \Swis\Melvin\Enums\RestrictionType[] $restrictionTypes
216
     *
217
     * @return $this
218
     */
219
    public function setRestrictionTypes(array $restrictionTypes): self
220
    {
221
        $this->validateEnums($restrictionTypes, RestrictionType::class, 'restrictionTypes');
222
223
        $this->restrictionTypes = $restrictionTypes;
224
225
        return $this;
226
    }
227
228
    /**
229
     * @return $this
230
     */
231
    public function unsetRestrictionTypes(): self
232
    {
233
        $this->restrictionTypes = null;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @param \Swis\Melvin\Enums\ActivityType[] $activityTypes
240
     *
241
     * @return $this
242
     */
243
    public function setActivityTypes(array $activityTypes): self
244
    {
245
        $this->validateEnums($activityTypes, ActivityType::class, 'activityTypes');
246
247
        $this->activityTypes = $activityTypes;
248
249
        return $this;
250
    }
251
252
    /**
253
     * @return $this
254
     */
255
    public function unsetActivityTypes(): self
256
    {
257
        $this->activityTypes = null;
258
259
        return $this;
260
    }
261
262
    /**
263
     * @param bool $includeDetours
264
     *
265
     * @return $this
266
     */
267
    public function setIncludeDetours(bool $includeDetours): self
268
    {
269
        $this->includeDetours = $includeDetours;
270
271
        return $this;
272
    }
273
274
    /**
275
     * @return $this
276
     */
277
    public function unsetIncludeDetours(): self
278
    {
279
        $this->includeDetours = null;
280
281
        return $this;
282
    }
283
284
    public function jsonSerialize(): array
285
    {
286
        return array_filter(
287
            [
288
                'startPeriod' => $this->startPeriod ? $this->startPeriod->format('Y-m-d\TH:i:s\Z') : null,
289
                'endPeriod' => $this->endPeriod ? $this->endPeriod->format('Y-m-d\TH:i:s\Z') : null,
290
                'sources' => $this->sources,
291
                'areaIds' => $this->areaIds,
292
                'areaBuffer' => $this->areaBuffer,
293
                'statuses' => $this->statuses,
294
                'published' => $this->published,
295
                'rvmConflict' => $this->rvmConflict,
296
                'restrictionTypes' => $this->restrictionTypes,
297
                'activityTypes' => $this->activityTypes,
298
                'includeDetours' => $this->includeDetours,
299
            ],
300
            fn ($value) => $value !== null
301
        );
302
    }
303
304
    protected function validateEnums(array $values, string $expectedEnum, string $name): void
305
    {
306
        foreach ($values as $value) {
307
            if (!$value instanceof $expectedEnum) {
308
                throw new InvalidArgumentException(
309
                    sprintf(
310
                        'All %s must be an instance of %s, %s provided.',
311
                        $name,
312
                        $expectedEnum,
313
                        is_object($value) ? get_class($value) : gettype($value)
314
                    )
315
                );
316
            }
317
        }
318
    }
319
320
    protected function validateTypes(array $values, string $expectedType, string $name): void
321
    {
322
        foreach ($values as $value) {
323
            if (gettype($value) !== $expectedType) {
324
                throw new InvalidArgumentException(
325
                    sprintf(
326
                        'All %s must be an %s, %s (%s) provided.',
327
                        $name,
328
                        $expectedType,
329
                        $value,
330
                        gettype($value)
331
                    )
332
                );
333
            }
334
        }
335
    }
336
}
337