1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Helper\ExtensionGuesserFactoryInterface; |
6
|
|
|
use Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface; |
7
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
11
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class hasGuessableExtensionValidator |
15
|
|
|
*/ |
16
|
|
|
class HasGuessableExtensionValidator extends ConstraintValidator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ExtensionGuesserInterface |
20
|
|
|
*/ |
21
|
|
|
private $extensionGuesser; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var MimeTypeGuesserInterface |
25
|
|
|
*/ |
26
|
|
|
private $mimeTypeGuesser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $value |
30
|
|
|
* @param Constraint $constraint |
31
|
|
|
* |
32
|
|
|
* @throws ConstraintDefinitionException |
33
|
|
|
* @throws UnexpectedTypeException |
34
|
|
|
*/ |
35
|
|
|
public function validate($value, Constraint $constraint) |
36
|
|
|
{ |
37
|
|
|
if (!$constraint instanceof HasGuessableExtension) { |
38
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\HasGuessableExtension'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!$value instanceof UploadedFile) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$contentType = $this->mimeTypeGuesser->guess($value->getPathname()); |
46
|
|
|
$pathInfo = pathinfo($value->getClientOriginalName()); |
47
|
|
|
if (!array_key_exists('extension', $pathInfo)) { |
48
|
|
|
$pathInfo['extension'] = $this->extensionGuesser->guess($contentType); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($pathInfo['extension'] === null) { |
52
|
|
|
$this->context->buildViolation($constraint->notGuessableErrorMessage) |
53
|
|
|
->setCode(HasGuessableExtension::NOT_GUESSABLE_ERROR) |
54
|
|
|
->addViolation(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ExtensionGuesserFactoryInterface $extensionGuesserFactory |
60
|
|
|
*/ |
61
|
|
|
public function setExtensionGuesser(ExtensionGuesserFactoryInterface $extensionGuesserFactory) |
62
|
|
|
{ |
63
|
|
|
$this->extensionGuesser = $extensionGuesserFactory->get(); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory |
68
|
|
|
*/ |
69
|
|
|
public function setMimeTypeGuesser(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory) |
70
|
|
|
{ |
71
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesserFactory->get(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..