summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/validator/src/StringLengthValidator.php
blob: 57df1ebd06fc931c324b335e69754d227cc0f4ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

namespace ipl\Validator;

use InvalidArgumentException;
use ipl\I18n\Translation;
use LogicException;

/**
 * Validates string length with given options
 */
class StringLengthValidator extends BaseValidator
{
    use Translation;

    /** @var mixed Minimum required length */
    protected $min;

    /** @var mixed Maximum required length */
    protected $max;

    /** @var ?string Encoding to use */
    protected $encoding;

    /**
     * Create a new StringLengthValidator
     *
     * Optional options:
     * - min: (scalar) Minimum required string length, default 0
     * - max: (scalar) Maximum required string length, default null
     * - encoding: (string) Encoding type, default null
     */
    public function __construct(array $options = [])
    {
        $this
            ->setMin($options['min'] ?? 0)
            ->setMax($options['max'] ?? null)
            ->setEncoding($options['encoding'] ?? null);
    }

    /**
     * Get the minimum required string length
     *
     * @return mixed
     */
    public function getMin()
    {
        return $this->min;
    }

    /**
     * Set the minimum required string length
     *
     * @param mixed $min
     *
     * @return $this
     *
     * @throws LogicException When the $min is greater than the $max value
     */
    public function setMin($min): self
    {
        if ($this->getMax() !== null && $min > $this->getMax()) {
            throw new LogicException(
                sprintf(
                    'The min must be less than or equal to the max length, but min: %d and max: %d given.',
                    $min,
                    $this->getMax()
                )
            );
        }

        $this->min = $min;

        return $this;
    }

    /**
     * Get the maximum required string length
     *
     * @return mixed
     */
    public function getMax()
    {
        return $this->max;
    }

    /**
     * Set the minimum required string length
     *
     * @param mixed $max
     *
     * @return $this
     *
     * @throws LogicException When the $min is greater than the $max value
     */
    public function setMax($max): self
    {
        if ($max !== null && $this->getMin() > $max) {
            throw new LogicException(
                sprintf(
                    'The min must be less than or equal to the max length, but min: %d and max: %d given.',
                    $this->getMin(),
                    $max
                )
            );
        }

        $this->max = $max;

        return $this;
    }

    /**
     * Get the encoding type to use
     *
     * @return ?string
     */
    public function getEncoding(): ?string
    {
        return $this->encoding;
    }

    /**
     * Set the encoding type to use
     *
     * @param ?string $encoding
     *
     * @return $this
     */
    public function setEncoding(?string $encoding): self
    {
        if ($encoding !== null) {
            $availableEncodings = array_map('strtolower', mb_list_encodings());
            if (! in_array(strtolower($encoding), $availableEncodings, true)) {
                throw new InvalidArgumentException(
                    sprintf('Given encoding "%s" is not supported on this OS!', $encoding)
                );
            }
        }

        $this->encoding = $encoding;

        return  $this;
    }

    public function isValid($value)
    {
        // Multiple isValid() calls must not stack validation messages
        $this->clearMessages();

        if ($encoding = $this->getEncoding()) { // because encoding is only nullable in php >= 8.0
            $length = mb_strlen($value, $encoding);
        } else {
            $length = mb_strlen($value);
        }

        if ($length < $this->getMin()) {
            $this->addMessage(sprintf(
                $this->translate('String should be %d characters long, %d given'),
                $this->getMin(),
                $length
            ));

            return false;
        }

        if ($this->getMax() && $this->getMax() < $length) {
            $this->addMessage(sprintf(
                $this->translate('String should be %d characters long, %d given'),
                $this->getMax(),
                $length
            ));

            return false;
        }

        return true;
    }
}