forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxRule.php
75 lines (69 loc) · 2.66 KB
/
MaxRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping;
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintMapperInterface;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\LessThan;
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
use Symfony\Component\Validator\Constraints\Range;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class MaxRule implements ConstraintMapperInterface
{
const RULE_NAME = 'max';
/**
* {@inheritdoc}
* @param
*/
public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
{
/** @var \Symfony\Component\Validator\Constraints\AbstractComparison $constraint */
$constraintClass = get_class($constraint);
if ($constraintClass === LessThan::class) {
$rule = new ConstraintRule(
self::RULE_NAME,
$constraint->value - 1, // TODO support floats
new RuleMessage($constraint->message, array('{{ compared_value }}' => $constraint->value)),
$constraint->groups
);
} elseif ($constraintClass === LessThanOrEqual::class) {
$rule = new ConstraintRule(
self::RULE_NAME,
$constraint->value,
new RuleMessage($constraint->message, array('{{ compared_value }}' => $constraint->value)),
$constraint->groups
);
}
/** @var Range $constraint */
elseif ($constraintClass === Range::class && $constraint->max !== null) {
$rule = new ConstraintRule(
self::RULE_NAME,
$constraint->max,
new RuleMessage($constraint->maxMessage, array('{{ limit }}' => $constraint->max)),
$constraint->groups
);
} else {
throw new LogicException();
}
$collection->set(
self::RULE_NAME,
$rule
);
}
public function supports(Constraint $constraint, FormInterface $form)
{
$constraintClass = get_class($constraint);
return
in_array($constraintClass, array(
LessThan::class,
LessThanOrEqual::class,
), true) ||
$constraintClass === Range::class && $constraint->max !== null
;
}
}