Passed
Branch master (83eb1b)
by Daniel
02:13
created

Validator::validInclude()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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