RuleCollection::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\Form;
3
4
use Boekkooi\Bundle\JqueryValidationBundle\Exception\InvalidArgumentException;
5
use Boekkooi\Bundle\JqueryValidationBundle\Exception\LogicException;
6
use Doctrine\Common\Collections\ArrayCollection;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class RuleCollection extends ArrayCollection
12
{
13
    /**
14
     * @param int|string $key
15
     * @param Rule $value
16
     */
17 7
    public function set($key, $value)
18
    {
19 7
        $this->assertRuleInstance($value);
20
21 2
        parent::set($key, $value);
22 2
    }
23
24
    /**
25
     * {@inheritdoc}
26
     * @throw LogicException
27
     */
28 1
    public function add($value)
29
    {
30 1
        throw new LogicException('RuleCollection must be used as a dictionary');
31
    }
32
33
    /**
34
     * Adds a rule collection at the end of the current set by appending all
35
     * rule of the added collection.
36
     *
37
     * @param RuleCollection $collection A RuleCollection instance
38
     */
39 1
    public function addCollection(RuleCollection $collection)
40
    {
41 1
        foreach ($collection as $name => $rule) {
42 1
            $this->set($name, $rule);
43 1
        }
44 1
    }
45
46 7
    private function assertRuleInstance($value)
47
    {
48 7
        if (!$value instanceof Rule) {
49 5
            throw new InvalidArgumentException(sprintf(
50 5
                'Expected a "%s" instance',
51
                Rule::class
52 5
            ));
53
        }
54 2
    }
55
}
56