Passed
Pull Request — master (#99)
by Def
02:39
created

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