1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toa\Component\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Constraint; |
6
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
7
|
|
|
use Toa\Component\Validator\Exception\ProviderException; |
8
|
|
|
use Toa\Component\Validator\Provider\VideoProviderInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class VideoValidator |
12
|
|
|
* |
13
|
|
|
* @author Enrico Thies <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class VideoValidator extends AudioValidator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param VideoProviderInterface $provider |
19
|
|
|
*/ |
20
|
|
|
public function __construct(VideoProviderInterface $provider) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($provider); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritDoc} |
27
|
|
|
*/ |
28
|
|
|
public function validate($value, Constraint $constraint) |
29
|
|
|
{ |
30
|
|
|
$violations = count($this->context->getViolations()); |
31
|
|
|
|
32
|
|
|
parent::validate($value, $constraint); |
33
|
|
|
|
34
|
|
|
$failed = count($this->context->getViolations()) !== $violations; |
35
|
|
|
|
36
|
|
|
if ($failed || null === $value || '' === $value) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$path = $value instanceof \SplFileInfo ? $value->getPathname() : (string) $value; |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$height = $this->provider->getHeight($path); |
44
|
|
|
} catch (ProviderException $e) { |
45
|
|
|
$this->context->addViolation($constraint->formatNotDetectedMessage); |
46
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($this->validateMaxHeight($height, $constraint)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($this->validateMinHeight($height, $constraint)) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$width = $this->provider->getWidth($path); |
59
|
|
|
|
60
|
|
|
if ($this->validateMaxWidth($width, $constraint)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->validateMinWidth($width, $constraint)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param integer $height |
71
|
|
|
* @param Constraint $constraint |
72
|
|
|
* |
73
|
|
|
* @throws ConstraintDefinitionException |
74
|
|
|
* @return boolean |
75
|
|
|
*/ |
76
|
|
|
protected function validateMaxHeight($height, Constraint $constraint) |
77
|
|
|
{ |
78
|
|
|
if ($constraint->maxHeight) { |
79
|
|
|
if (!ctype_digit((string) $constraint->maxHeight)) { |
80
|
|
|
throw new ConstraintDefinitionException( |
81
|
|
|
sprintf( |
82
|
|
|
'"%s" is not a valid maximum height', |
83
|
|
|
$constraint->maxHeight |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($height > $constraint->maxHeight) { |
89
|
|
|
$this->context->addViolation( |
90
|
|
|
$constraint->maxHeightMessage, |
91
|
|
|
array( |
92
|
|
|
'{{ height }}' => $height, |
93
|
|
|
'{{ max_height }}' => $constraint->maxHeight |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param integer $height |
104
|
|
|
* @param Constraint $constraint |
105
|
|
|
* |
106
|
|
|
* @throws ConstraintDefinitionException |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
|
|
protected function validateMinHeight($height, Constraint $constraint) |
110
|
|
|
{ |
111
|
|
|
if ($constraint->minHeight) { |
112
|
|
|
if (!ctype_digit((string) $constraint->minHeight)) { |
113
|
|
|
throw new ConstraintDefinitionException( |
114
|
|
|
sprintf( |
115
|
|
|
'"%s" is not a valid minimum height', |
116
|
|
|
$constraint->minHeight |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($height < $constraint->minHeight) { |
122
|
|
|
$this->context->addViolation( |
123
|
|
|
$constraint->minHeightMessage, |
124
|
|
|
array( |
125
|
|
|
'{{ height }}' => $height, |
126
|
|
|
'{{ min_height }}' => $constraint->minHeight |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param integer $width |
138
|
|
|
* @param Constraint $constraint |
139
|
|
|
* |
140
|
|
|
* @throws ConstraintDefinitionException |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
|
|
protected function validateMaxWidth($width, Constraint $constraint) |
144
|
|
|
{ |
145
|
|
|
if ($constraint->maxWidth) { |
146
|
|
|
if (!ctype_digit((string) $constraint->maxWidth)) { |
147
|
|
|
throw new ConstraintDefinitionException( |
148
|
|
|
sprintf( |
149
|
|
|
'"%s" is not a valid maximum width', |
150
|
|
|
$constraint->maxWidth |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($width > $constraint->maxWidth) { |
156
|
|
|
$this->context->addViolation( |
157
|
|
|
$constraint->maxWidthMessage, |
158
|
|
|
array( |
159
|
|
|
'{{ width }}' => $width, |
160
|
|
|
'{{ max_width }}' => $constraint->maxWidth |
161
|
|
|
) |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param integer $width |
171
|
|
|
* @param Constraint $constraint |
172
|
|
|
* |
173
|
|
|
* @throws ConstraintDefinitionException |
174
|
|
|
* @return boolean |
175
|
|
|
*/ |
176
|
|
|
protected function validateMinWidth($width, Constraint $constraint) |
177
|
|
|
{ |
178
|
|
|
if ($constraint->minWidth) { |
179
|
|
|
if (!ctype_digit((string) $constraint->minWidth)) { |
180
|
|
|
throw new ConstraintDefinitionException( |
181
|
|
|
sprintf( |
182
|
|
|
'"%s" is not a valid minimum width', |
183
|
|
|
$constraint->minWidth |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ($width < $constraint->minWidth) { |
189
|
|
|
$this->context->addViolation( |
190
|
|
|
$constraint->minWidthMessage, |
191
|
|
|
array( |
192
|
|
|
'{{ width }}' => $width, |
193
|
|
|
'{{ min_width }}' => $constraint->minWidth |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
return true; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|