Completed
Pull Request — develop (#63)
by
unknown
07:04
created

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