Passed
Push — master ( 2b6758...acf834 )
by Aleksei
06:59
created

HasLength::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\DataSetInterface;
8
use Yiisoft\Validator\HasValidationErrorMessage;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
12
/**
13
 * StringValidator validates that the attribute value is of certain length.
14
 *
15
 * Note, this validator should only be used with string-typed attributes.
16
 */
17
class HasLength extends Rule
18
{
19
    use HasValidationErrorMessage;
20
21
    /**
22
     * @var int|null maximum length. null means no maximum length limit.
23
     *
24
     * @see tooLongMessage for the customized message for a too long string.
25
     */
26
    private ?int $max = null;
27
    /**
28
     * @var int|null minimum length. null means no minimum length limit.
29
     *
30
     * @see tooShortMessage for the customized message for a too short string.
31
     */
32
    private ?int $min = null;
33
    /**
34
     * @var string user-defined error message used when the value is not a string.
35
     */
36
    private string $message = 'This value must be a string.';
37
    /**
38
     * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
39
     */
40
    private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.';
41
    /**
42
     * @var string user-defined error message used when the length of the value is greater than {@see $max}.
43
     */
44
    private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.';
45
    /**
46
     * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
47
     * If this property is not set, application wide encoding will be used.
48
     */
49
    protected string $encoding = 'UTF-8';
50
51 6
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
52
    {
53 6
        $result = new Result();
54
55 6
        if (!is_string($value)) {
56 2
            $result->addError($this->translateMessage($this->message));
57 2
            return $result;
58
        }
59
60 6
        $length = mb_strlen($value, $this->encoding);
61
62 6
        if ($this->min !== null && $length < $this->min) {
63 3
            $result->addError($this->translateMessage($this->tooShortMessage, ['min' => $this->min]));
64
        }
65 6
        if ($this->max !== null && $length > $this->max) {
66 4
            $result->addError($this->translateMessage($this->tooLongMessage, ['max' => $this->max]));
67
        }
68
69 6
        return $result;
70
    }
71
72 3
    public function min(int $value): self
73
    {
74 3
        $new = clone $this;
75 3
        $new->min = $value;
76 3
        return $new;
77
    }
78
79 9
    public function max(int $value): self
80
    {
81 9
        $new = clone $this;
82 9
        $new->max = $value;
83 9
        return $new;
84
    }
85
86
    public function encoding(string $encoding): self
87
    {
88
        $new = clone $this;
89
        $new->encoding = $encoding;
90
        return $new;
91
    }
92
93 1
    public function tooShortMessage(string $message): self
94
    {
95 1
        $new = clone $this;
96 1
        $new->tooShortMessage = $message;
97 1
        return $new;
98
    }
99
100 1
    public function tooLongMessage(string $message): self
101
    {
102 1
        $new = clone $this;
103 1
        $new->tooLongMessage = $message;
104 1
        return $new;
105
    }
106
107 7
    public function getOptions(): array
108
    {
109 7
        return array_merge(
110 7
            parent::getOptions(),
111
            [
112 7
                'message' => $this->translateMessage($this->message),
113 7
                'min' => $this->min,
114 7
                'tooShortMessage' => $this->translateMessage($this->tooShortMessage, ['min' => $this->min]),
115 7
                'max' => $this->max,
116 7
                'tooLongMessage' => $this->translateMessage($this->tooLongMessage, ['max' => $this->max]),
117 7
                'encoding' => $this->encoding,
118
            ],
119
        );
120
    }
121
}
122