Passed
Pull Request — develop (#59)
by
unknown
09:35
created

Validator   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 71
dl 0
loc 199
rs 9.6
c 2
b 0
f 0
wmc 35

11 Methods

Rating   Name   Duplication   Size   Complexity  
A validDate() 0 6 2
A validTo() 0 21 5
A __construct() 0 2 1
A validFrom() 0 23 5
A validToken() 0 3 1
A validTag() 0 3 1
C validId() 0 69 16
A validTitle() 0 3 1
A validFileType() 0 3 1
A validFileName() 0 3 1
A validInclude() 0 3 1
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
    public function __construct()
17
    {
18
        
19
    }
20
    
21
    /**
22
     * 
23
     * {@inheritDoc}
24
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDate()
25
     */
26
    public function validDate($date): bool 
27
    {
28
        if (v::stringType()->notEmpty()->numericVal()->length(8, 8)->date('Ymd')->validate($date)) {
29
            return true;
30
        } else { 
31
            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
    public function validFrom($from, $to = null): bool 
41
    { 
42
        if (!$this->validDate($from)) {
43
            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
    public function validTo($to, $from = null): bool 
71
    {        
72
        if (!$this->validDate($to)) {
73
            return false;
74
        }
75
76
        $toDate = new \DateTime($to);
77
        
78
        if (empty($from)) {
79
            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
    public function validTag($tag): bool 
99
    {
100
        return v::stringType()->notEmpty()->alnum()->validate($tag);
101
    }
102
    
103
    /**
104
     * 
105
     * {@inheritDoc}
106
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
107
     */
108
    public function validToken($token): bool
109
    {
110
        return v::alnum()->validate($token);
111
    }
112
113
    /**
114
     * 
115
     * {@inheritDoc}
116
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validId()
117
     * @todo
118
     */
119
    public function validId($Id, $type = null): bool 
120
    {
121
        if (!v::stringType()->notEmpty()->validate($Id)) {
122
            return false;
123
        }
124
125
        switch ($type) {
126
            case 'classPass':
127
128
                $exploded = explode('-', $Id);
129
                
130
                if (count($exploded) !== 2) {
131
                    return false;
132
                }
133
134
                if ($exploded[0] !== 'cp') {
135
                    return false;
136
                }
137
138
                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
            case 'event':
141
                
142
                $exploded = explode('-', $Id);
143
                
144
                if (count($exploded) !== 3) {
145
                    return false;
146
                }
147
                
148
                if ($exploded[0] !== 'ev') {
149
                    return false;
150
                }
151
                
152
                // Syntax.
153
                if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
154
                    return false;
155
                }
156
 
157
                return $this->validDate($exploded[2]);
158
                break;
159
            
160
            case 'ticket':
161
                $exploded = explode('-', $Id);
162
                
163
                if (count($exploded) !== 4) {
164
                    return false;
165
                }
166
                
167
                if ($exploded[0] !== 'ti') {
168
                    return false;
169
                }
170
                
171
                // Syntax.
172
                if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
173
                    return false;
174
                }
175
                
176
                if (!$this->validDate($exploded[2])) {
177
                    return false;
178
                }
179
                
180
                return v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[3]);
181
                break;
182
            
183
            case 'attachment':
184
            case 'location':
185
            default:
186
                return v::alnum()->length(12, 12)->validate($Id);
187
                break;
188
        }
189
    } 
190
    
191
    public function validTitle($title): bool
192
    {
193
        return v::stringType()->notEmpty()->validate($title);
194
    }
195
196
    public function validFileType($fileType): bool
197
    {
198
        return v::stringType()->notEmpty()->in(['jpg', 'jpeg', 'gif', 'png'])->validate(strtolower($fileType));
199
    }
200
201
    public function validFileName($fileName): bool
202
    {
203
        return v::stringType()->notEmpty()->validate($fileName);
204
    }
205
206
    public function validInclude($include)
207
    {
208
        return v::boolType()->validate($include);
209
    }
210
}
211
212
// EOF!
213