-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathText.php
138 lines (117 loc) · 3.09 KB
/
Text.php
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
<?php
namespace Utopia\Http\Validator;
use Utopia\Http\Validator;
/**
* Text
*
* Validate that an variable is a valid text value
*/
class Text extends Validator
{
public const NUMBERS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
public const ALPHABET_UPPER = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
public const ALPHABET_LOWER = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
/**
* @var int
*/
protected int $length;
/**
* @var int
*/
protected int $min;
/**
* @var string[]
*/
protected array $allowList;
/**
* Text constructor.
*
* Validate text with maximum length $length. Use $length = 0 for unlimited length.
* Optionally, provide allowList characters array $allowList to only allow specific character.
*
* @param int $length
* @param int $min
* @param string[] $allowList
*/
public function __construct(int $length, int $min = 1, array $allowList = [])
{
$this->length = $length;
$this->min = $min;
$this->allowList = $allowList;
}
/**
* Get Description
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
$message = 'Value must be a valid string';
if ($this->min === $this->length) {
$message .= ' and exactly '.$this->length.' chars';
} else {
if ($this->min) {
$message .= ' and at least '.$this->min.' chars';
}
if ($this->length) {
$message .= ' and no longer than '.$this->length.' chars';
}
}
if ($this->allowList) {
$message .= ' and only consist of \''.\implode(', ', $this->allowList).'\' chars';
}
return $message;
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_STRING;
}
/**
* Is valid
*
* Validation will pass when $value is text with valid length.
*
* @param mixed $value
* @return bool
*/
public function isValid(mixed $value): bool
{
if (!\is_string($value)) {
return false;
}
if (\mb_strlen($value) < $this->min) {
return false;
}
if (\mb_strlen($value) > $this->length && $this->length !== 0) {
return false;
}
if (\count($this->allowList) > 0) {
foreach (\str_split($value) as $char) {
if (!\in_array($char, $this->allowList)) {
return false;
}
}
}
return true;
}
}