MinMaxValueValidatorTrait::isValid()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 4
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;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/13/15
16
 */
17
trait MinMaxValueValidatorTrait
18
{
19
    /**
20
     * Check if min or max value is valid.
21
     *
22
     * @param string    $type (min|max)
23
     * @param int|float $value
24
     * @param string    $operator
25
     *
26
     * @return bool
27
     */
28
    protected function isValid($type, $value, $operator = null)
29
    {
30
        $operator = $operator ?: $type;
31
32
        if (isset($this->{$type})) {
33
            return $this->compute($operator, $type, $value);
34
        }
35
36
        return true;
37
    }
38
39
    /**
40
     * @param string    $type (min|max)
41
     * @param int|float $value
42
     * @param string    $operator
43
     *
44
     * @return bool
45
     */
46
    private function compute($operator, $type, $value)
47
    {
48
        switch ($operator) {
49
            case 'min':
50
                return $value < $this->{$type};
51
            case 'max':
52
                return $value > $this->{$type};
53
        }
54
55
        return true;
56
    }
57
}
58