-
Notifications
You must be signed in to change notification settings - Fork 6
/
_DataValidation.php
184 lines (163 loc) · 5.16 KB
/
_DataValidation.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
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
180
181
182
183
184
<?php
declare(strict_types=1);
namespace Momento\Utilities;
use Momento\Cache\Errors\InvalidArgumentError;
use Momento\Cache\Errors\InvalidArgumentException;
if (!function_exists('validateTtl')) {
function validateTtl(int $ttlSeconds): void
{
if (!is_int($ttlSeconds) || $ttlSeconds < 0) {
throw new InvalidArgumentError("TTL Seconds must be a non-negative integer");
}
}
}
if (!function_exists('isNullOrEmpty')) {
function isNullOrEmpty(string $str = null): bool
{
return (is_null($str) || $str === "");
}
}
if (!function_exists('validateCacheName')) {
function validateCacheName(string $cacheName): void
{
if (isNullOrEmpty($cacheName)) {
throw new InvalidArgumentError("Cache name must be a non-empty string");
}
}
}
if (!function_exists('validateListName')) {
function validateListName(string $listName): void
{
if (isNullOrEmpty($listName)) {
throw new InvalidArgumentError("List name must be a non-empty string");
}
}
}
if (!function_exists('validateDictionaryName')) {
function validateDictionaryName(string $dictionaryName): void
{
if (isNullOrEmpty($dictionaryName)) {
throw new InvalidArgumentError("Dictionary name must be a non-empty string");
}
}
}
if (!function_exists('validateFieldName')) {
function validateFieldName(string $fieldName): void
{
if (isNullOrEmpty($fieldName)) {
throw new InvalidArgumentError("Field name must be a non-empty string");
}
}
}
if (!function_exists('validateFields')) {
function validateFields(array $fieldNames): void
{
if (empty($fieldNames)) {
throw new InvalidArgumentError("Field names must be a non-empty array");
}
foreach ($fieldNames as $fieldName) {
validateFieldName($fieldName);
}
}
}
if (!function_exists('validateValueName')) {
function validateValueName(string $valueName): void
{
if (isNullOrEmpty($valueName)) {
throw new InvalidArgumentError("Value name must be a non-empty string");
}
}
}
if (!function_exists('validateItems')) {
function validateItems(array $items): void
{
if (empty($items)) {
throw new InvalidArgumentError("Items must be a non-empty array");
}
foreach ($items as $item) {
if (empty($item)) {
throw new InvalidArgumentError("Items must be a non-empty array");
}
}
}
}
if (!function_exists('validateFieldsKeys')) {
function validateFieldsKeys(array $items): void
{
if (empty($items)) {
throw new InvalidArgumentError("Items must be a non-empty array");
}
foreach ($items as $field => $value) {
if (isNullOrEmpty($field) || isNullOrEmpty($value)) {
throw new InvalidArgumentError("Each key and value must be a non-empty string");
}
}
}
}
if (!function_exists('validateOperationTimeout')) {
function validateOperationTimeout(?int $operationTimeout = null)
{
if ($operationTimeout === null) {
return;
}
if ($operationTimeout <= 0) {
throw new InvalidArgumentError("Request timeout must be greater than zero.");
}
}
}
if (!function_exists('validateTruncateSize')) {
function validateTruncateSize(?int $truncateSize = null)
{
if ($truncateSize === null) {
return;
}
if ($truncateSize <= 0) {
throw new InvalidArgumentError("Truncate size must be greater than zero.");
}
}
}
if (!function_exists('validateRange')) {
function validateRange(?int $beginIndex, ?int $count)
{
if (is_null($beginIndex) && is_null($count)) {
return;
}
if (!is_null($beginIndex) xor !is_null($count)) {
throw new InvalidArgumentError("Beginning index and count must be supplied together.");
}
if ($beginIndex < 0) {
throw new InvalidArgumentError("Beginning index and count must be a positive integer.");
}
if ($count <= 0) {
throw new InvalidArgumentError("Count must be greater than zero.");
}
}
}
if (!function_exists('validateSetName')) {
function validateSetName(string $setName): void
{
if (isNullOrEmpty($setName)) {
throw new InvalidArgumentError("Set name must be a non-empty string");
}
}
}
if (!function_exists('validateElement')) {
function validateElement(string $element): void
{
if (isNullOrEmpty($element)) {
throw new InvalidArgumentError("Element must be a non-empty string");
}
}
}
if (!function_exists('validatePsr16Key')) {
function validatePsr16Key(string $key): void
{
$reserved = '/\{|\}|\(|\)|\/|\\\\|\@|\:/u';
if (isNullOrEmpty($key)) {
throw new InvalidArgumentException("Key must be a non-empty string");
}
if (preg_match($reserved, $key, $match) === 1) {
throw new InvalidArgumentException("Key must not contain the character {$match[0]}");
}
}
}