Passed
Push — scrutinizer ( f0febe...f8c545 )
by
unknown
07:27 queued 16s
created

Validator::validid()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7

Importance

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