Passed
Push — develop ( 5308a3...2390ef )
by Daniel
21:15 queued 06:16
created

Validator::validDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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