|
1
|
|
|
<?php |
|
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Validator\Constraint; |
|
5
|
|
|
use Symfony\Component\Form\FormInterface; |
|
6
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; |
|
7
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException; |
|
8
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule; |
|
9
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage; |
|
10
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\File; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Warnar Boekkooi <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class FileRule implements ConstraintMapperInterface |
|
17
|
|
|
{ |
|
18
|
|
|
const RULE_NAME = 'accept'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
private $active; |
|
24
|
|
|
|
|
25
|
13 |
|
public function __construct($active) |
|
26
|
|
|
{ |
|
27
|
13 |
|
$this->active = $active; |
|
28
|
13 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection) |
|
34
|
|
|
{ |
|
35
|
6 |
|
if (!$this->supports($constraint, $form)) { |
|
36
|
4 |
|
throw new LogicException(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** @var File $constraint */ |
|
40
|
2 |
|
$collection->set( |
|
41
|
2 |
|
self::RULE_NAME, |
|
42
|
2 |
|
new ConstraintRule( |
|
43
|
2 |
|
self::RULE_NAME, |
|
44
|
2 |
|
implode(',', $constraint->mimeTypes), |
|
45
|
2 |
|
new RuleMessage($constraint->mimeTypesMessage, array( |
|
46
|
2 |
|
'{{ types }}' => implode(', ', $constraint->mimeTypes), |
|
47
|
2 |
|
)), |
|
48
|
2 |
|
$constraint->groups |
|
49
|
2 |
|
) |
|
50
|
2 |
|
); |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
13 |
|
public function supports(Constraint $constraint, FormInterface $form) |
|
54
|
|
|
{ |
|
55
|
13 |
|
return $this->active && |
|
56
|
13 |
|
get_class($constraint) === File::class && |
|
57
|
13 |
|
!empty($constraint->mimeTypes); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|