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

RuleSet::getNameForAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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