Validator::validTo()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 9.9614

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 23
ccs 5
cts 12
cp 0.4167
crap 9.9614
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen;
6
7
use InShore\Bookwhen\Interfaces\ValidatorInterface;
8
use Respect\Validation\Validator as v;
9
10
class Validator implements ValidatorInterface
11
{
12
    /**
13
     *
14
     * @author Daniel Mullin [email protected]
15
     *
16
     * @access public
17
     *
18
     * @param string $additionalInfo
19
     * @return bool
20
     */
21
    public function validAdditionalInfo(string $additionalInfo): bool
22
    {
23
        return v::stringType()->notEmpty()->alnum()->validate($additionalInfo);
24
    }
25
26
    /**
27
     *
28
     * @author Daniel Mullin [email protected]
29
     *
30
     * @access public
31
     *
32
     * @param string $addressText
33
     * @return bool
34
     */
35
    public function validAddressText(string $addressText): bool
36
    {
37
        return v::stringType()->notEmpty()->alnum()->validate($addressText);
38
    }
39
40
    /**
41
     *
42
     * @author Daniel Mullin [email protected]
43
     *
44
     * @access public
45
     *
46
     * @param string $attachmentId
47
     * @return bool
48
     */
49
    public function validAttachmentId(string $attachmentId): bool
50
    {
51
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($attachmentId);
52
    }
53
54
    /**
55
     *
56
     * @author Daniel Mullin [email protected]
57
     * @author Brandon Lubbehusen [email protected]
58
     *
59
     * @access public
60
     *
61
     * @param string $classPassId
62
     * @return bool
63
     */
64 1
    public function validClassPassId(string $classPassId): bool
65
    {
66 1
        $exploded = explode('-', $classPassId);
67
68 1
        if (2 !== count($exploded)) {
69
            return false;
70
        }
71
72 1
        if ('cp' !== $exploded[0]) {
73
            return false;
74
        }
75
76 1
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($exploded[1]);
77
    }
78
79
    /**
80
     *
81
     * {@inheritDoc}
82
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDate()
83
     */
84 10
    public function validDate(string $date): bool
85
    {
86 10
        if (v::stringType()->notEmpty()->numericVal()->length(8, 8)->date('Ymd')->validate($date)) {
87 8
            return true;
88
        } else {
89 2
            return v::stringType()->notEmpty()->numericVal()->length(14, 14)->dateTime('YmdHis')->validate($date);
90
        }
91
    }
92
93
    /**
94
     *
95
     * {@inheritDoc}
96
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDetails()
97
     */
98
    public function validDetails(string $details): bool
99
    {
100
        return v::stringType()->notEmpty()->validate($details);
101
    }
102
103
    /**
104
     * @author Daniel Mullin [email protected]
105
     * @author Brandon Lubbehusen [email protected]
106
     *
107
     * @access public
108
     *
109
     * @param string $eventId
110
     * @return bool
111
     */
112 1
    public function validEventId(string $eventId): bool
113
    {
114 1
        $exploded = explode('-', $eventId);
115
116 1
        if (3 !== count($exploded)) {
117
            return false;
118
        }
119
120 1
        if ('ev' !== $exploded[0]) {
121
            return false;
122
        }
123
124
        // Syntax.
125 1
        if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
126
            return false;
127
        }
128
129 1
        return $this->validDate($exploded[2]);
130
    }
131
132
    /**
133
     *
134
     * {@inheritDoc}
135
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFileName()
136
     */
137 2
    public function validFileName(string $fileName): bool
138
    {
139 2
        return v::stringType()->notEmpty()->validate($fileName);
140
    }
141
142
    /**
143
     *
144
     * {@inheritDoc}
145
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFileType()
146
     */
147 2
    public function validFileType(string $fileType): bool
148
    {
149 2
        return v::stringType()->notEmpty()->in(['jpg', 'jpeg', 'gif', 'png'])->validate(strtolower($fileType));
150
    }
151
152
    /**
153
     *
154
     * {@inheritDoc}
155
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFrom()
156
     */
157 1
    public function validFrom(string $from, string $to = null): bool
158
    {
159 1
        if (!$this->validDate($from)) {
160 1
            return false;
161
        }
162
163
        $fromDate = new \DateTime($from);
164
165
        if (empty($to)) {
166
            return true;
167
        }
168
169
        if (!$this->validDate($to)) {
170
            return false;
171
        }
172
173
        $toDate = new \DateTime($to);
174
175
        // Compare if actual to date is greater than from.
176
        if ($fromDate > $toDate) {
177
            return false;
178
        }
179
180
        return true;
181
    }
182
183
    /**
184
     *
185
     * {@inheritDoc}
186
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validId()
187
     * @todo
188
     */
189 14
    public function validId(string $id, string $type): bool
190
    {
191 14
        if (!v::stringType()->notEmpty()->validate($id)) {
192 3
            return false;
193
        }
194 11
        if (!v::stringType()->regex('/\b(attachment|classPass|event|location|\ticket|)\b/')->validate($type)) {
195
            return false;
196
        }
197 11
        $validationMethod = 'valid' . ucFirst($type) . 'Id';
198 11
        return $this->$validationMethod($id);
199
    }
200
201
    /**
202
     *
203
     * @author Brandon Lubbehusen [email protected]
204
     *
205
     * {@inheritDoc}
206
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validInclude()
207
     */
208 1
    public function validInclude(bool $include): bool
209
    {
210 1
        return v::boolType()->validate($include);
211
    }
212
213
    /**
214
     *
215
     * @author Brandon Lubbehusen [email protected]
216
     *
217
     * {@inheritDoc}
218
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validLocation()
219
     */
220
    public function validLocation(string $location): bool
221
    {
222
        return v::stringType()->notEmpty()->validate($location);
223
    }
224
225
    /**
226
     *
227
     * @author Daniel Mullin [email protected]
228
     *
229
     * @access public
230
     *
231
     * @param string $locationId
232
     * @return bool
233
     */
234
    public function validLocationId(string $locationId): bool
235
    {
236
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($locationId);
237
    }
238
239
    /**
240
     *
241
     * {@inheritDoc}
242
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTag()
243
     */
244 9
    public function validTag(string $tag): bool
245
    {
246 9
        return v::stringType()->notEmpty()->alnum()->validate($tag);
247
    }
248
249
    /**
250
     * @author Daniel Mullin [email protected]
251
     * @author Brandon Lubbehusen [email protected]
252
     *
253
     * @access public
254
     *
255
     * @param string $ticketId
256
     * @return bool
257
     */
258 1
    public function validTicketId(string $ticketId): bool
259
    {
260
261 1
        $exploded = explode('-', $ticketId);
262
263 1
        if (4 !== count($exploded)) {
264
            return false;
265
        }
266
267 1
        if ('ti' !== $exploded[0]) {
268
            return false;
269
        }
270
271
        // Syntax.
272 1
        if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
273
            return false;
274
        }
275
276 1
        if (!$this->validDate($exploded[2])) {
277
            return false;
278
        }
279
280 1
        return v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[3]);
281
    }
282
283
    /**
284
     *
285
     * {@inheritDoc}
286
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTitle()
287
     */
288 2
    public function validTitle(string $title): bool
289
    {
290 2
        return v::stringType()->notEmpty()->validate($title);
291
    }
292
293
    /**
294
     *
295
     * {@inheritDoc}
296
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTo()
297
     */
298 5
    public function validTo(string $to, null | string $from = null): bool
299
    {
300 5
        if (!$this->validDate($to)) {
301
            return false;
302
        }
303
304 5
        $toDate = new \DateTime($to);
305
306
307 5
        if (empty($from)) {
308 5
            return true;
309
        }
310
311
        $fromDate = new \DateTime($from);
312
313
        if (!$this->validFrom($from)) {
314
            return false;
315
        }
316
        if ($toDate < $fromDate) {
317
            return false;
318
        }
319
320
        return true;
321
    }
322
323
    /**
324
     *
325
     * {@inheritDoc}
326
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
327
     */
328 1
    public function validToken(string $token): bool
329
    {
330 1
        return v::alnum()->validate($token);
331
    }
332
333
    /**
334
     *
335
     * @author Brandon Lubbehusen [email protected]
336
     *
337
     * @access public
338
     *
339
     * @param int $usageAllowance
340
     * @return bool
341
     */
342
    public function validUsageAllowance(int $usageAllowance): bool
343
    {
344
        return v::intType()->notEmpty()->alnum()->validate($usageAllowance);
345
    }
346
347
    /**
348
     *
349
     * @author Brandon Lubbehusen [email protected]
350
     *
351
     * @access public
352
     *
353
     * @param string $usageType
354
     * @return bool
355
     */
356
    public function validUsageType(string $usageType): bool
357
    {
358
        return v::regex('/\b(personal|any)\b/')->validate($usageType);
359
    }
360
361
    /**
362
     *
363
     * @author Brandon Lubbehusen [email protected]
364
     *
365
     * @access public
366
     *
367
     * @param int $useRestrictedForDays
368
     * @return bool
369
     */
370
    public function validuseRestrictedForDays(int $useRestrictedForDays): bool
371
    {
372
        return v::intType()->notEmpty()->alnum()->validate($useRestrictedForDays);
373
    }
374
}
375
376
// EOF!
377