RequiredRule::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 1
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\NotBlank;
12
use Symfony\Component\Validator\Constraints\NotNull;
13
14
/**
15
 * @author Warnar Boekkooi <[email protected]>
16
 */
17
class RequiredRule implements ConstraintMapperInterface
18
{
19
    const RULE_NAME = 'required';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 4
    public function resolve(Constraint $constraint, FormInterface $form, RuleCollection $collection)
25
    {
26 4
        if (!$this->supports($constraint, $form)) {
27 2
            throw new LogicException();
28
        }
29
30
        /** @var \Symfony\Component\Validator\Constraints\NotBlank | \Symfony\Component\Validator\Constraints\NotNull $constraint */
31 2
        $collection->set(
32 2
            self::RULE_NAME,
33 2
            new ConstraintRule(
34 2
                self::RULE_NAME,
35 2
                true,
36 2
                new RuleMessage($constraint->message),
37 2
                $constraint->groups
38 2
            )
39 2
        );
40 2
    }
41
42 8
    public function supports(Constraint $constraint, FormInterface $form)
43
    {
44 8
        return in_array(
45 8
            get_class($constraint),
46
            array(
47 8
                NotBlank::class,
48 8
                NotNull::class,
49 8
            ),
50
            true
51 8
        );
52
    }
53
}
54