LengthBetween::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
13
/**
14
 * This rule is for validating that the length of the value is within predefined boundaries.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class LengthBetween extends Between
19
{
20
    /**
21
     * A constant that is used when the value is too long.
22
     */
23
    const TOO_LONG = 'LengthBetween::TOO_LONG';
24
25
    /**
26
     * A constant that is used when the value is too short.
27
     */
28
    const TOO_SHORT = 'LengthBetween::TOO_SHORT';
29
30
    /**
31
     * The message templates which can be returned by this validator.
32
     *
33
     * @var array
34
     */
35
    protected $messageTemplates = [
36
        self::TOO_LONG => '{{ name }} must be {{ max }} characters or shorter',
37
        self::TOO_SHORT => '{{ name }} must be {{ min }} characters or longer'
38
    ];
39
40
    /**
41
     * The upper boundary for the length of the value.
42
     *
43
     * @var int
44
     */
45
    protected $max;
46
47
    /**
48
     * The lower boundary for the length of the value.
49
     *
50
     * @var int
51
     */
52
    protected $min;
53
54
    /**
55
     * @param int $min
56
     * @param int|null $max
57
     */
58 9
    public function __construct($min, $max)
59
    {
60 9
        $this->min = $min;
61 9
        $this->max = $max;
62 9
    }
63
64
    /**
65
     * Validates that the length of the value is between min and max.
66
     *
67
     * @param mixed $value
68
     * @return bool
69
     */
70 9
    public function validate($value)
71
    {
72 9
        $length = strlen($value);
73
74 9
        return !$this->tooSmall($length, self::TOO_SHORT) && !$this->tooLarge($length, self::TOO_LONG);
75
    }
76
77
    /**
78
     * Returns whether or not the value is too long, and logs an error if it is.
79
     *
80
     * @param mixed $value
81
     * @param string $error
82
     * @return bool
83
     */
84 9
    protected function tooLarge($value, $error)
85
    {
86 9
        if ($this->max !== null) {
87 8
            return parent::tooLarge($value, $error);
88
        }
89 1
        return false;
90
    }
91
92
    /**
93
     * Returns the parameters that may be used in a validation message.
94
     *
95
     * @return array
96
     */
97 3
    protected function getMessageParameters()
98
    {
99 3
        return array_merge(parent::getMessageParameters(), [
100 3
            'min' => $this->min,
101 3
            'max' => $this->max
102 3
        ]);
103
    }
104
}
105