Completed
Pull Request — master (#430)
by Anton
04:06
created

LengthRule::extractLength()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator\Rule;
12
13
use Bluz\Validator\Exception\ComponentException;
14
use Countable;
15
16
/**
17
 * Check for length in range between minimum and maximum values
18
 *
19
 * @package Bluz\Validator\Rule
20
 */
21
class LengthRule extends AbstractCompareRule
22
{
23
    /**
24
     * @var integer minimum value
25
     */
26
    protected $minValue;
27
28
    /**
29
     * @var integer maximum value
30
     */
31
    protected $maxValue;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $inclusive = true;
37
38
    /**
39
     * Setup validation rule
40
     *
41
     * @param  integer|null $min
42
     * @param  integer|null $max
43
     *
44
     * @throws \Bluz\Validator\Exception\ComponentException
45
     */
46
    public function __construct($min = null, $max = null)
47
    {
48
        $this->minValue = $min;
49
        $this->maxValue = $max;
50
51
        if (!is_numeric($min) && !is_null($min)) {
52
            throw new ComponentException(
53
                __('"%s" is not a valid numeric length', $min)
54
            );
55
        }
56
57
        if (!is_numeric($max) && !is_null($max)) {
58
            throw new ComponentException(
59
                __('"%s" is not a valid numeric length', $max)
60
            );
61
        }
62
63
        if (!is_null($min) && !is_null($max) && $min > $max) {
64
            throw new ComponentException(
65
                __('"%s" cannot be less than "%s" for validation', $min, $max)
66
            );
67
        }
68
    }
69
70
    /**
71
     * Check input data
72
     *
73
     * @param  string $input
74
     *
75
     * @return bool
76
     */
77
    public function validate($input): bool
78
    {
79
        if (!$length = $this->extractLength($input)) {
80
            return false;
81
        }
82
83
        return (is_null($this->minValue) || $this->less($this->minValue, $length))
84
            && (is_null($this->maxValue) || $this->less($length, $this->maxValue));
85
    }
86
87
    /**
88
     * Extract length
89
     *
90
     * @param  string|object $input
91
     *
92
     * @return integer|false
93
     */
94
    protected function extractLength($input)
95
    {
96
        if (is_string($input)) {
97
            return mb_strlen($input, mb_detect_encoding($input));
98
        } elseif (is_array($input) || $input instanceof Countable) {
99
            return count($input);
100
        } elseif (is_object($input)) {
101
            return count(get_object_vars($input));
102
        }
103
        return false;
104
    }
105
106
    /**
107
     * Get error template
108
     *
109
     * @return string
110
     */
111
    public function getDescription() : string
112
    {
113
        if (!$this->minValue) {
114
            return __('must have a length lower than %d', $this->maxValue);
115
        } elseif (!$this->maxValue) {
116
            return __('must have a length greater than %d', $this->minValue);
117
        }
118
        return __('must have a length between %d and %d', $this->minValue, $this->maxValue);
119
    }
120
}
121