AbstractNumericNode::assertValidValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
nop 3
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\InvalidConfigurationException;
14
use Borobudur\Config\MinMaxValueValidatorTrait;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/10/15
19
 */
20
abstract class AbstractNumericNode extends ScalarNode
21
{
22
    use MinMaxValueValidatorTrait;
23
24
    /**
25
     * @var int|float|double
26
     */
27
    protected $min;
28
29
    /**
30
     * @var int|float|double
31
     */
32
    protected $max;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string                $name
38
     * @param NodeInterface|null    $parent
39
     * @param int|float|double|null $min
40
     * @param int|float|double|null $max
41
     */
42
    public function __construct($name, NodeInterface $parent = null, $min = null, $max = null)
43
    {
44
        parent::__construct($name, $parent);
45
        $this->min = $min;
46
        $this->max = $max;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function finalizeValue($value)
53
    {
54
        $this->assertValidValue('min', 'Minimum', $value);
55
        $this->assertValidValue('max', 'Maximum', $value);
56
57
        return $value;
58
    }
59
60
    /**
61
     * Assert that min or max value is valid.
62
     *
63
     * @param string    $type
64
     * @param string    $label
65
     * @param int|float $value
66
     *
67
     * @throws InvalidConfigurationException
68
     */
69
    private function assertValidValue($type, $label, $value)
70
    {
71
        if (!isset($this->{$type})) {
72
            return;
73
        }
74
75
        if (true === $this->isValid($type, $value)) {
76
            throw InvalidConfigurationException::invalidFinalizeRangeValue(
77
                $label,
78
                $this->getPath(),
79
                $this->{$type},
80
                $value
81
            );
82
        }
83
    }
84
}
85