|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Validator\Constraints; |
|
4
|
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Entity\Media as MediaObject; |
|
6
|
|
|
use Symfony\Component\Validator\Constraint; |
|
7
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
8
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
9
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
10
|
|
|
|
|
11
|
|
|
class MediaValidator extends ConstraintValidator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param MediaObject $value |
|
15
|
|
|
* @param Constraint $constraint |
|
16
|
|
|
* |
|
17
|
|
|
* @throws ConstraintDefinitionException |
|
18
|
|
|
*/ |
|
19
|
17 |
|
public function validate($value, Constraint $constraint) |
|
20
|
|
|
{ |
|
21
|
17 |
|
if (!$constraint instanceof Media) { |
|
22
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Media'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
17 |
|
if (!$value instanceof MediaObject) { |
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
17 |
|
$mimeType = $value->getContentType(); |
|
30
|
|
|
|
|
31
|
17 |
|
if ($constraint->mimeTypes) { |
|
|
|
|
|
|
32
|
7 |
|
$mimeTypes = (array) $constraint->mimeTypes; |
|
33
|
|
|
|
|
34
|
7 |
|
if (false === $this->validateMimeType($value, $mimeTypes)) { |
|
35
|
2 |
|
$this->context->buildViolation($constraint->mimeTypesMessage) |
|
36
|
2 |
|
->setParameter('{{ type }}', $this->formatValue($mimeType)) |
|
37
|
2 |
|
->setParameter('{{ types }}', $this->formatValues($mimeTypes)) |
|
38
|
2 |
|
->setCode(Media::INVALID_MIME_TYPE_ERROR) |
|
39
|
2 |
|
->addViolation(); |
|
40
|
|
|
|
|
41
|
2 |
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
15 |
|
if (!preg_match('^image\/*^', $mimeType) || $mimeType === 'image/svg+xml') { |
|
46
|
2 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
13 |
|
$this->validateDimensions($value, $constraint); |
|
50
|
13 |
|
} |
|
51
|
|
|
|
|
52
|
7 |
|
private function validateMimeType(MediaObject $value, $allowedMimeTypes) |
|
53
|
|
|
{ |
|
54
|
7 |
|
$mimeType = strtolower($value->getContentType()); |
|
55
|
|
|
|
|
56
|
7 |
|
foreach ($allowedMimeTypes as $type) { |
|
57
|
7 |
|
$type = strtolower($type); |
|
58
|
7 |
|
if ($type === $mimeType) { |
|
59
|
4 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
if ($discrete = strstr($type, '/*', true)) { |
|
63
|
2 |
|
if (strstr($mimeType, '/', true) === $discrete) { |
|
64
|
1 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
13 |
|
private function validateDimensions(MediaObject $value, Media $constraint) |
|
73
|
|
|
{ |
|
74
|
13 |
|
$height = $value->getMetadataValue('original_height'); |
|
75
|
13 |
|
$width = $value->getMetadataValue('original_width'); |
|
76
|
|
|
|
|
77
|
13 |
View Code Duplication |
if ($constraint->minHeight) { |
|
|
|
|
|
|
78
|
2 |
|
if (!ctype_digit((string) $constraint->minHeight)) { |
|
79
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
if ($height < $constraint->minHeight) { |
|
83
|
1 |
|
$this->context->buildViolation($constraint->minHeightMessage) |
|
84
|
1 |
|
->setParameter('{{ height }}', $height) |
|
85
|
1 |
|
->setParameter('{{ min_height }}', $constraint->minHeight) |
|
86
|
1 |
|
->setCode(Media::TOO_LOW_ERROR) |
|
87
|
1 |
|
->addViolation(); |
|
88
|
|
|
|
|
89
|
1 |
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
12 |
View Code Duplication |
if ($constraint->maxHeight) { |
|
|
|
|
|
|
94
|
2 |
|
if (!ctype_digit((string) $constraint->maxHeight)) { |
|
95
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
if ($height > $constraint->maxHeight) { |
|
99
|
1 |
|
$this->context->buildViolation($constraint->maxHeightMessage) |
|
100
|
1 |
|
->setParameter('{{ height }}', $height) |
|
101
|
1 |
|
->setParameter('{{ max_height }}', $constraint->maxHeight) |
|
102
|
1 |
|
->setCode(Media::TOO_HIGH_ERROR) |
|
103
|
1 |
|
->addViolation(); |
|
104
|
|
|
|
|
105
|
1 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
11 |
View Code Duplication |
if ($constraint->minWidth) { |
|
|
|
|
|
|
110
|
2 |
|
if (!ctype_digit((string) $constraint->minWidth)) { |
|
111
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
if ($width < $constraint->minWidth) { |
|
115
|
1 |
|
$this->context->buildViolation($constraint->minWidthMessage) |
|
116
|
1 |
|
->setParameter('{{ width }}', $width) |
|
117
|
1 |
|
->setParameter('{{ min_width }}', $constraint->minWidth) |
|
118
|
1 |
|
->setCode(Media::TOO_NARROW_ERROR) |
|
119
|
1 |
|
->addViolation(); |
|
120
|
|
|
|
|
121
|
1 |
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
10 |
View Code Duplication |
if ($constraint->maxWidth) { |
|
|
|
|
|
|
126
|
2 |
|
if (!ctype_digit((string) $constraint->maxWidth)) { |
|
127
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
if ($width > $constraint->maxWidth) { |
|
131
|
1 |
|
$this->context->buildViolation($constraint->maxWidthMessage) |
|
132
|
1 |
|
->setParameter('{{ width }}', $width) |
|
133
|
1 |
|
->setParameter('{{ max_width }}', $constraint->maxWidth) |
|
134
|
1 |
|
->setCode(Media::TOO_WIDE_ERROR) |
|
135
|
1 |
|
->addViolation(); |
|
136
|
|
|
|
|
137
|
1 |
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
9 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.