Passed
Pull Request — develop (#95)
by
unknown
29:42 queued 14:41
created

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