Passed
Pull Request — master (#65)
by Alexander
27:54 queued 12:56
created

HasLength::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
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\HasValidationMessage;
8
use Yiisoft\Validator\Rule;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\DataSetInterface;
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 HasValidationMessage;
20
21
    /**
22
     * @var int|null maximum length. null means no maximum length limit.
23
     * @see tooLongMessage for the customized message for a too long string.
24
     */
25
    private ?int $max = null;
26
    /**
27
     * @var int|null minimum length. null means no minimum length limit.
28
     * @see tooShortMessage for the customized message for a too short string.
29
     */
30
    private ?int $min = null;
31
    /**
32
     * @var string user-defined error message used when the value is not a string.
33
     */
34
    private string $message = 'This value must be a string.';
35
    /**
36
     * @var string user-defined error message used when the length of the value is smaller than {@see $min}.
37
     */
38
    private string $tooShortMessage = 'This value should contain at least {min, number} {min, plural, one{character} other{characters}}.';
39
    /**
40
     * @var string user-defined error message used when the length of the value is greater than {@see $max}.
41
     */
42
    private string $tooLongMessage = 'This value should contain at most {max, number} {max, plural, one{character} other{characters}}.';
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 5
    protected string $encoding = 'UTF-8';
48
49 5
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
50
    {
51 5
        $result = new Result();
52 2
53 2
        if (!is_string($value)) {
54
            $result->addError($this->message);
55
            return $result;
56 5
        }
57
58 5
        $length = mb_strlen($value, $this->encoding);
59 3
60
        if ($this->min !== null && $length < $this->min) {
61 5
            $result->addError($this->translateMessage($this->tooShortMessage, ['min' => $this->min]));
62 3
        }
63
        if ($this->max !== null && $length > $this->max) {
64
            $result->addError($this->translateMessage($this->tooLongMessage, ['min' => $this->max]));
65 5
        }
66
67
        return $result;
68 3
    }
69
70 3
    public function min(int $value): self
71 3
    {
72 3
        $new = clone $this;
73
        $new->min = $value;
74
        return $new;
75 3
    }
76
77 3
    public function max(int $value): self
78 3
    {
79 3
        $new = clone $this;
80
        $new->max = $value;
81
        return $new;
82
    }
83
84
    public function encoding(string $encoding): self
85
    {
86
        $new = clone $this;
87
        $new->encoding = $encoding;
88
        return $new;
89 1
    }
90
91 1
    public function tooShortMessage(string $message): self
92 1
    {
93 1
        $new = clone $this;
94
        $new->tooShortMessage = $message;
95
        return $new;
96 1
    }
97
98 1
    public function tooLongMessage(string $message): self
99 1
    {
100 1
        $new = clone $this;
101
        $new->tooLongMessage = $message;
102
        return $new;
103 1
    }
104
}
105