Completed
Push — master ( 17ecb7...725fdf )
by Alexander
15:14
created

HasLength::validateValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 19
cc 6
rs 9.2222
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace Yiisoft\Validator\Rule;
9
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule;
12
13
/**
14
 * StringValidator validates that the attribute value is of certain length.
15
 *
16
 * Note, this validator should only be used with string-typed attributes.
17
 */
18
class HasLength extends Rule
19
{
20
    /**
21
     * @var int maximum length. If not set, it means no maximum length limit.
22
     * @see tooLongMessage for the customized message for a too long string.
23
     */
24
    private $max;
25
    /**
26
     * @var int minimum length. If not set, it means no minimum length limit.
27
     * @see tooShortMessage for the customized message for a too short string.
28
     */
29
    private $min;
30
    /**
31
     * @var string user-defined error message used when the value is not a string.
32
     */
33
    private $message = '{attribute} must be a string.';
34
    /**
35
     * @var string user-defined error message used when the length of the value is smaller than [[min]].
36
     */
37
    private $tooShortMessage = '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.';
38
    /**
39
     * @var string user-defined error message used when the length of the value is greater than [[max]].
40
     */
41
    private $tooLongMessage = '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.';
42
43
    /**
44
     * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
45
     * If this property is not set, application wide encoding will be used.
46
     */
47
    protected $encoding = 'UTF-8';
48
49
    public function min(int $value): self
50
    {
51
        $this->min = $value;
52
        return $this;
53
    }
54
55
    public function max(int $value): self
56
    {
57
        $this->max = $value;
58
        return $this;
59
    }
60
61
    public function encoding(string $encoding): self
62
    {
63
        $this->encoding = $encoding;
64
        return $this;
65
    }
66
67
    public function validateValue($value): Result
68
    {
69
        $result = new Result();
70
71
        if (!is_string($value)) {
72
            $result->addError($this->formatMessage($this->message));
73
            return $result;
74
        }
75
76
        $length = mb_strlen($value, $this->encoding);
77
78
        if ($this->min !== null && $length < $this->min) {
79
            $result->addError($this->formatMessage($this->tooShortMessage, ['min' => $this->min]));
80
        }
81
        if ($this->max !== null && $length > $this->max) {
82
            $result->addError($this->formatMessage($this->tooLongMessage, ['min' => $this->max]));
83
        }
84
85
        return $result;
86
    }
87
}
88