EnumNode   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 51
wmc 5
lcom 1
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getValue() 0 4 1
A finalizeValue() 0 13 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Config package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
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 Borobudur\Config\Definition;
12
13
use Borobudur\Config\Exception\InvalidArgumentException;
14
use Borobudur\Config\Exception\InvalidConfigurationException;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/10/15
19
 */
20
class EnumNode extends ScalarNode
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $values = array();
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param string $name
31
     * @param NodeInterface|null $parent
32
     * @param array              $values
33
     */
34
    public function __construct($name, NodeInterface $parent = null, array $values = array())
35
    {
36
        $values = array_unique($values);
37
38
        if (count($values) < 2) {
39
            throw new InvalidArgumentException('Enum values should contain at least two elements.');
40
        }
41
42
        parent::__construct($name, $parent);
43
        $this->values = $values;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getValue()
50
    {
51
        return $this->values;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function finalizeValue($value)
58
    {
59
        if (!in_array($value, $this->values)) {
60
            throw new InvalidConfigurationException(sprintf(
61
                'Value "%s" is not accepted for path "%s". Acceptable values: "%s"',
62
                $value,
63
                $this->getPath(),
64
                implode(', ', $this->values)
65
            ));
66
        }
67
68
        return $value;
69
    }
70
}
71