RequiredRule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 37
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 2
A supports() 0 11 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