MaxRule::resolve()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

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