ConstraintCollection::addCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
namespace Boekkooi\Bundle\JqueryValidationBundle\Validator;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Exception\InvalidArgumentException;
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Symfony\Component\Validator\Constraint;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class ConstraintCollection extends ArrayCollection
12
{
13
    /**
14
     * {@inheritDoc}
15
     *
16
     * @param Constraint $value A Constraint instance
17
     */
18 1
    public function set($key, $value)
19
    {
20 1
        $this->assertConstraintInstance($value);
21
22 1
        parent::set($key, $value);
23 1
    }
24
25
    /**
26
     * {@inheritDoc}
27
     *
28
     * @param Constraint $value A Constraint instance
29
     */
30 7
    public function add($value)
31
    {
32 7
        $this->assertConstraintInstance($value);
33
34 2
        return parent::add($value);
35
    }
36
37
    /**
38
     * Adds a constraint collection at the end of the current set by appending all
39
     * constraint of the added collection.
40
     *
41
     * @param ConstraintCollection $collection A ConstraintCollection instance
42
     */
43 1
    public function addCollection(ConstraintCollection $collection)
44
    {
45 1
        foreach ($collection as $constraint) {
46 1
            $this->add($constraint);
47 1
        }
48 1
    }
49
50
    /**
51
     * @param $value
52
     */
53 8
    private function assertConstraintInstance($value)
54
    {
55 8
        if (!$value instanceof Constraint) {
56 5
            throw new InvalidArgumentException(sprintf(
57 5
                'Expected a "%s" instance',
58
                Constraint::class
59 5
            ));
60
        }
61 3
    }
62
}
63