Passed
Push — develop ( a6f0fe...5308a3 )
by Daniel
24:47 queued 09:44
created

Validator::validTo()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5.9256

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 21
rs 9.6111
ccs 2
cts 3
cp 0.6667
crap 5.9256
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
     * @author Brandon Lubbehusen [email protected]
24
     *
25
     * @access protected
26
     *
27
     * @param string $classPassId
28
     * @return bool
29
     */
30
    protected function validClassPassId($classPassId): bool
31 1
    {
32
        $exploded = explode('-', $classPassId);
33 1
34
        if (count($exploded) !== 2) {
35 1
            return false;
36
        }
37
38
        if ($exploded[0] !== 'cp') {
39 1
            return false;
40
        }
41
42
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($exploded[1]);
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 $eventId
53
     * @return bool
54
     */
55
    protected function validEventId($eventId): bool
56 1
    {
57
        $exploded = explode('-', $eventId);
58 1
59
        if (count($exploded) !== 3) {
60 1
            return false;
61
        }
62
63
        if ($exploded[0] !== 'ev') {
64 1
            return false;
65
        }
66
67
        // Syntax.
68
        if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
69 1
            return false;
70
        }
71
72
        return $this->validDate($exploded[2]);
73 1
    }
74
75
    /**
76
     * @author Daniel Mullin [email protected]
77
     * @author Brandon Lubbehusen [email protected]
78
     *
79
     * @access protected
80
     *
81
     * @param string $ticketId
82
     * @return bool
83
     */
84
    protected function validTicketId($ticketId): bool
85 1
    {
86
87
        $exploded = explode('-', $ticketId);
88 1
89
        if (count($exploded) !== 4) {
90 1
            return false;
91
        }
92
93
        if ($exploded[0] !== 'ti') {
94 1
            return false;
95
        }
96
97
        // Syntax.
98
        if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
99 1
            return false;
100
        }
101
102
        if (!$this->validDate($exploded[2])) {
103 1
            return false;
104
        }
105
106
        return v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[3]);
107 1
    }
108
109
    /**
110
     *
111
     * {@inheritDoc}
112
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDate()
113
     */
114
    public function validDate($date): bool
115 14
    {
116
        if (v::stringType()->notEmpty()->numericVal()->length(8, 8)->date('Ymd')->validate($date)) {
117 14
            return true;
118 8
        } else {
119
            return v::stringType()->notEmpty()->numericVal()->length(14, 14)->dateTime('YmdHis')->validate($date);
120 6
        }
121
    }
122
123
    /**
124
     *
125
     * {@inheritDoc}
126
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDetails()
127
     */
128
    public function validDetails($details): bool
129 4
    {
130
        return v::stringType()->notEmpty()->validate($details);
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($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($from, $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
        $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($id, $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
199 12
            case 'event':
200
                return $this->validEventId($id);
201
202
            case 'ticket':
203
                return $this->validTicketId($id);
204
205
            case 'attachment':
206
            case 'location':
207
            default:
208 4
                return v::alnum()->length(12, 12)->validate($id);
209
        }
210 4
    }
211
212
    /**
213
     *
214
     * {@inheritDoc}
215
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validInclude()
216
     */
217
    public function validInclude($include): bool
218 11
    {
219
        return v::boolType()->validate($include);
220 11
    }
221
222
    /**
223
     *
224
     * {@inheritDoc}
225
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validLocation()
226
     */
227
    public function validLocation($location): bool
228 4
    {
229
        return v::stringType()->notEmpty()->validate($location);
230 4
    }
231
232
    /**
233
     *
234
     * {@inheritDoc}
235
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTag()
236
     */
237
    public function validTag($tag): bool
238 5
    {
239
        return v::stringType()->notEmpty()->alnum()->validate($tag);
240 5
    }
241
242
    /**
243
     *
244 5
     * {@inheritDoc}
245
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTitle()
246 5
     */
247 5
    public function validTitle($title): bool
248
    {
249
        return v::stringType()->notEmpty()->validate($title);
250
    }
251
252
    /**
253
     *
254
     * {@inheritDoc}
255
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTo()
256
     */
257
    public function validTo($to, $from = null): bool
258
    {
259
        if (!$this->validDate($to)) {
260
            return false;
261
        }
262
263
        $toDate = new \DateTime($to);
264
265
        if (empty($from)) {
266 1
            return true;
267
        }
268 1
269
        $fromDate = new \DateTime($from);
270
        if (!$this->validFrom($from)) {
271
            return false;
272
        }
273
        if ($toDate < $fromDate) {
274
            return false;
275
        }
276
277
        return true;
278
    }
279
280
    /**
281
     *
282
     * {@inheritDoc}
283
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
284
     */
285
    public function validToken($token): bool
286
    {
287
        return v::alnum()->validate($token);
288
    }
289
}
290
291
// EOF!
292