Completed
Push — develop ( 082044...a4a532 )
by Daniel
13s queued 10s
created

Validator::validFileName()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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
     * {@inheritDoc}
24
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDate()
25
     */
26 14
    public function validDate($date): bool 
27
    {
28 14
        if (v::stringType()->notEmpty()->numericVal()->length(8, 8)->date('Ymd')->validate($date)) {
29 8
            return true;
30
        } else { 
31 6
            return v::stringType()->notEmpty()->numericVal()->length(14, 14)->dateTime('YmdHis')->validate($date);
32
        }
33
    }
34
    
35
    /**
36
     * 
37
     * {@inheritDoc}
38
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFrom()
39
     */
40 3
    public function validFrom($from, $to = null): bool 
41
    { 
42 3
        if (!$this->validDate($from)) {
43 3
            return false;
44
        }
45
        
46
        $fromDate = new \DateTime($from);
47
        
48
        if (empty($to)) {
49
            return true;
50
        }
51
        
52
        if (!$this->validDate($to)) {
53
            return false;
54
        }
55
        $toDate = new \DateTime($to);
56
        
57
        // Compare if actual to date is greater than from.
58
        if ($fromDate > $toDate) {
59
            return false;
60
        }
61
        
62
        return true;
63
    }
64
    
65
    /**
66
     * 
67
     * {@inheritDoc}
68
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTo()
69
     */
70 5
    public function validTo($to, $from = null): bool 
71
    {        
72 5
        if (!$this->validDate($to)) {
73
            return false;
74
        }
75
76 5
        $toDate = new \DateTime($to);
77
        
78 5
        if (empty($from)) {
79 5
            return true;
80
        }
81
        
82
        $fromDate = new \DateTime($from);
83
        if (!$this->validFrom($from)) {
84
            return false;
85
        }
86
        if ($toDate < $fromDate) {
87
            return false;
88
        }
89
        
90
        return true;
91
    }
92
    
93
    /**
94
     * 
95
     * {@inheritDoc}
96
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTag()
97
     */
98 11
    public function validTag($tag): bool 
99
    {
100 11
        return v::stringType()->notEmpty()->alnum()->validate($tag);
101
    }
102
    
103
    /**
104
     * 
105
     * {@inheritDoc}
106
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
107
     */
108 1
    public function validToken($token): bool
109
    {
110 1
        return v::alnum()->validate($token);
111
    }
112
113
    /**
114
     * 
115
     * {@inheritDoc}
116
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validId()
117
     * @todo
118
     */
119 28
    public function validId($Id, $type = null): bool 
120
    {
121 28
        if (!v::stringType()->notEmpty()->validate($Id)) {
122 13
            return false;
123
        }
124
125 15
        switch ($type) {
126 15
            case 'classPass':
127
128 1
                $exploded = explode('-', $Id);
129
                
130 1
                if (count($exploded) !== 2) {
131
                    return false;
132
                }
133
134 1
                if ($exploded[0] !== 'cp') {
135
                    return false;
136
                }
137
138 1
                return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($exploded[1]);
139
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
140 14
            case 'event':
141
                
142 1
                $exploded = explode('-', $Id);
143
                
144 1
                if (count($exploded) !== 3) {
145
                    return false;
146
                }
147
                
148 1
                if ($exploded[0] !== 'ev') {
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
                return $this->validDate($exploded[2]);
158
                break;
159
            
160 13
            case 'ticket':
161 1
                $exploded = explode('-', $Id);
162
                
163 1
                if (count($exploded) !== 4) {
164
                    return false;
165
                }
166
                
167 1
                if ($exploded[0] !== 'ti') {
168
                    return false;
169
                }
170
                
171
                // Syntax.
172 1
                if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
173
                    return false;
174
                }
175
                
176 1
                if (!$this->validDate($exploded[2])) {
177
                    return false;
178
                }
179
                
180 1
                return v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[3]);
181
                break;
182
            
183 12
            case 'attachment':
184 8
            case 'location':
185
            default:
186 12
                return v::alnum()->length(12, 12)->validate($Id);
187
                break;
188
        }
189
    } 
190
    
191 4
    public function validTitle($title): bool
192
    {
193 4
        return v::stringType()->notEmpty()->validate($title);
194
    }
195
196 2
    public function validFileType($fileType): bool
197
    {
198 2
        return v::stringType()->notEmpty()->in(['jpg', 'jpeg', 'gif', 'png'])->validate(strtolower($fileType));
199
    }
200
201 4
    public function validFileName($fileName): bool
202
    {
203 4
        return v::stringType()->notEmpty()->validate($fileName);
204
    }
205
206 4
    public function validInclude($include): bool
207
    {
208 4
        return v::boolType()->validate($include);
209
    }
210
}
211
212
// EOF!
213