Passed
Pull Request — develop (#88)
by
unknown
13:49 queued 01:56
created

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

294
        if (!$this->validFrom(/** @scrutinizer ignore-type */ $from)) {
Loading history...
295
            return false;
296
        }
297
        if ($toDate < $fromDate) {
298
            return false;
299
        }
300
301
        return true;
302
    }
303
304
    /**
305
     *
306
     * {@inheritDoc}
307
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
308
     */
309
    public function validToken(string $token): bool
310
    {
311
        return v::alnum()->validate($token);
312
    }
313
}
314
315
// EOF!
316