Passed
Push — scrutinizer ( b59259...937a91 )
by
unknown
10:09
created

Validator::validClassPass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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