Passed
Push — develop ( bbfb8c...f742ee )
by Daniel
26:42 queued 11:46
created

Validator::validuseRestrictedForDays()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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