Completed
Pull Request — master (#257)
by
unknown
01:57
created

RuleSet   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 97
ccs 20
cts 25
cp 0.8
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A add() 0 7 1
A addAliases() 0 8 2
A get() 0 9 2
A getDefault() 0 4 1
A getRules() 0 4 1
A getNameForAlias() 0 8 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo;
12
13
use FeedIo\Rule\OptionalField;
14
15
class RuleSet
16
{
17
    /**
18
     * @var \ArrayIterator
19
     */
20
    protected $rules;
21
22
    /**
23
     * @var array
24
     */
25
    protected $aliases = array();
26
27
    /**
28
     * @var RuleAbstract
29
     */
30
    protected $default;
31
32
    /**
33
     * @param RuleAbstract $default default rule
34
     */
35 2
    public function __construct(RuleAbstract $default = null)
36
    {
37 2
        $this->rules = new \ArrayIterator(array());
38 2
        $this->default = is_null($default) ? new OptionalField() : $default;
39 2
    }
40
41
    /**
42
     * @return RuleAbstract
43
     */
44
    public function getDefault() : RuleAbstract
45
    {
46
        return $this->default;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getRules() : array
53
    {
54
        return $this->rules->getArrayCopy();
55
    }
56
57
    /**
58
     * @param RuleAbstract $rule
59
     * @param array $aliases
60
     * @return RuleSet
61
     */
62 2
    public function add(RuleAbstract $rule, array $aliases = array()) : RuleSet
63
    {
64 2
        $this->rules->offsetSet(strtolower($rule->getNodeName()), $rule);
65 2
        $this->addAliases($rule->getNodeName(), $aliases);
66
67 2
        return $this;
68
    }
69
70
    /**
71
     * @param string $name
72
     * @param array $aliases
73
     * @return RuleSet
74
     */
75 2
    public function addAliases(string $name, array $aliases) : RuleSet
76
    {
77 2
        foreach ($aliases as $alias) {
78 2
            $this->aliases[strtolower($alias)] = strtolower($name);
79
        }
80
81 2
        return $this;
82
    }
83
84
    /**
85
     * @param  string            $name
86
     * @return RuleAbstract
87
     * @throws NotFoundException
88
     */
89 2
    public function get(string $name) : RuleAbstract
90
    {
91 2
        $name = $this->getNameForAlias(strtolower($name));
92 2
        if ($this->rules->offsetExists($name)) {
93 2
            return $this->rules->offsetGet($name);
94
        }
95
96 2
        return $this->default;
97
    }
98
99
    /**
100
     * @param $alias
101
     * @return string
102
     */
103 2
    public function getNameForAlias(string $alias) : string
104
    {
105 2
        if (array_key_exists($alias, $this->aliases)) {
106
            return $this->aliases[$alias];
107
        }
108
109 2
        return $alias;
110
    }
111
}
112