PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / Assert / LazyAssertion.php

LazyAssertion.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at lib/packages/Assert/LazyAssertion.php

229 lines 18.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WP_DEFENDER_VENDOR\Assert
5 *
6 * LICENSE
7 *
8 * This source file is subject to the MIT license that is bundled
9 * with this package in the file LICENSE.txt.
10 * If you did not receive a copy of the license and are unable to
11 * obtain it through the world-wide-web, please send an email
12 * to [email protected] so I can send you a copy immediately.
13 */
14
15 namespace WP_DEFENDER_VENDOR\Assert;
16
17 use LogicException;
18
19 /**
20 * Chaining builder for lazy assertions.
21 *
22 * @author Benjamin Eberlei <[email protected]>
23 *
24 * @method LazyAssertion alnum(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is alphanumeric.
25 * @method LazyAssertion base64(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the given string is a valid base64 string.
26 * @method LazyAssertion between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
27 * @method LazyAssertion betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a value is greater than a lower limit, and less than an upper limit.
28 * @method LazyAssertion betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string length is between min and max lengths.
29 * @method LazyAssertion boolean(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is php boolean.
30 * @method LazyAssertion choice(array $choices, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is in array of choices.
31 * @method LazyAssertion choicesNotEmpty(array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content.
32 * @method LazyAssertion classExists(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the class exists.
33 * @method LazyAssertion contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string contains a sequence of chars.
34 * @method LazyAssertion count(int $count, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the count of countable is equal to count.
35 * @method LazyAssertion date(string $format, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that date is valid and corresponds to the given format.
36 * @method LazyAssertion defined(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a constant is defined.
37 * @method LazyAssertion digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit.
38 * @method LazyAssertion directory(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a directory exists.
39 * @method LazyAssertion e164(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the given string is a valid E164 Phone Number.
40 * @method LazyAssertion email(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
41 * @method LazyAssertion endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string ends with a sequence of chars.
42 * @method LazyAssertion eq(mixed $value2, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that two values are equal (using ==).
43 * @method LazyAssertion eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the array contains the subset.
44 * @method LazyAssertion extensionLoaded(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that extension is loaded.
45 * @method LazyAssertion extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that extension is loaded and a specific version is installed.
46 * @method LazyAssertion false(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is boolean False.
47 * @method LazyAssertion file(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a file exists.
48 * @method LazyAssertion float(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is a php float.
49 * @method LazyAssertion greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit.
50 * @method LazyAssertion greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit.
51 * @method LazyAssertion implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the class implements the interface.
52 * @method LazyAssertion inArray(array $choices, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is in array of choices. This is an alias of Assertion::choice().
53 * @method LazyAssertion integer(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is a php integer.
54 * @method LazyAssertion integerish(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is a php integer'ish.
55 * @method LazyAssertion interfaceExists(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the interface exists.
56 * @method LazyAssertion ip(int $flag = null, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an IPv4 or IPv6 address.
57 * @method LazyAssertion ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an IPv4 address.
58 * @method LazyAssertion ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an IPv6 address.
59 * @method LazyAssertion isArray(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an array.
60 * @method LazyAssertion isArrayAccessible(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an array or an array-accessible object.
61 * @method LazyAssertion isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable.
62 * @method LazyAssertion isCountable(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is countable.
63 * @method LazyAssertion isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is instance of given class-name.
64 * @method LazyAssertion isJsonString(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the given string is a valid json string.
65 * @method LazyAssertion isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object.
66 * @method LazyAssertion isResource(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is a resource.
67 * @method LazyAssertion isTraversable(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an array or a traversable object.
68 * @method LazyAssertion keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that key exists in an array.
69 * @method LazyAssertion keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that key exists in an array/array-accessible object using isset().
70 * @method LazyAssertion keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that key does not exist in an array.
71 * @method LazyAssertion length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string has a given length.
72 * @method LazyAssertion lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
73 * @method LazyAssertion lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
74 * @method LazyAssertion max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a number is smaller as a given limit.
75 * @method LazyAssertion maxCount(int $count, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the countable have at most $count elements.
76 * @method LazyAssertion maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string value is not longer than $maxLength chars.
77 * @method LazyAssertion methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
78 * @method LazyAssertion min(mixed $minValue, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that a value is at least as big as a given limit.
79 * @method LazyAssertion minCount(int $count, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the countable have at least $count elements.
80 * @method LazyAssertion minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that a string is at least $minLength chars long.
81 * @method LazyAssertion noContent(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is empty.
82 * @method LazyAssertion notBlank(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is not blank.
83 * @method LazyAssertion notContains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string does not contains a sequence of chars.
84 * @method LazyAssertion notEmpty(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is not empty.
85 * @method LazyAssertion notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that key exists in an array/array-accessible object and its value is not empty.
86 * @method LazyAssertion notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that two values are not equal (using ==).
87 * @method LazyAssertion notInArray(array $choices, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is not in array of choices.
88 * @method LazyAssertion notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is not instance of given class-name.
89 * @method LazyAssertion notNull(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is not null.
90 * @method LazyAssertion notRegex(string $pattern, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value does not match a regex.
91 * @method LazyAssertion notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that two values are not the same (using ===).
92 * @method LazyAssertion null(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is null.
93 * @method LazyAssertion numeric(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is numeric.
94 * @method LazyAssertion objectOrClass(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is an object, or a class that exists.
95 * @method LazyAssertion phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert on PHP version.
96 * @method LazyAssertion propertiesExist(array $properties, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is an object or class, and that the properties all exist.
97 * @method LazyAssertion propertyExists(string $property, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is an object or class, and that the property exists.
98 * @method LazyAssertion range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is in range of numbers.
99 * @method LazyAssertion readable(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is something readable.
100 * @method LazyAssertion regex(string $pattern, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value matches a regex.
101 * @method LazyAssertion same(mixed $value2, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that two values are the same (using ===).
102 * @method LazyAssertion satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the provided value is valid according to a callback.
103 * @method LazyAssertion scalar(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is a PHP scalar.
104 * @method LazyAssertion startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') WP_DEFENDER_VENDOR\Assert that string starts with a sequence of chars.
105 * @method LazyAssertion string(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is a string.
106 * @method LazyAssertion subclassOf(string $className, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is subclass of given class-name.
107 * @method LazyAssertion true(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is boolean True.
108 * @method LazyAssertion uniqueValues(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that values in array are unique (using strict equality).
109 * @method LazyAssertion url(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that value is an URL.
110 * @method LazyAssertion uuid(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the given string is a valid UUID.
111 * @method LazyAssertion version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert comparison of two versions.
112 * @method LazyAssertion writeable(string|callable $message = null, string $propertyPath = null) WP_DEFENDER_VENDOR\Assert that the value is something writeable.
113 * @method LazyAssertion all() Switch chain into validation mode for an array of values.
114 * @method LazyAssertion nullOr() Switch chain into mode allowing nulls, ignoring further assertions.
115 */
116 class LazyAssertion
117 {
118 private $currentChainFailed = false;
119 private $alwaysTryAll = false;
120 private $thisChainTryAll = false;
121 private $currentChain;
122 private $errors = [];
123
124 /** @var string The class to use as AssertionChain factory */
125 private $assertClass = Assert::class;
126
127 /** @var string|LazyAssertionException The class to use for exceptions */
128 private $exceptionClass = LazyAssertionException::class;
129
130 /**
131 * @param mixed $value
132 * @param string|callable|null $defaultMessage
133 *
134 * @return static
135 */
136 public function that($value, ?string $propertyPath = null, $defaultMessage = null)
137 {
138 $this->currentChainFailed = false;
139 $this->thisChainTryAll = false;
140 $assertClass = $this->assertClass;
141 $this->currentChain = $assertClass::that($value, $defaultMessage, $propertyPath);
142
143 return $this;
144 }
145
146 /**
147 * @return static
148 */
149 public function tryAll()
150 {
151 if (!$this->currentChain) {
152 $this->alwaysTryAll = true;
153 }
154
155 $this->thisChainTryAll = true;
156
157 return $this;
158 }
159
160 /**
161 * @param string $method
162 * @param array $args
163 *
164 * @return static
165 */
166 public function __call($method, $args)
167 {
168 if (false === $this->alwaysTryAll
169 && false === $this->thisChainTryAll
170 && true === $this->currentChainFailed
171 ) {
172 return $this;
173 }
174
175 try {
176 \call_user_func_array([$this->currentChain, $method], $args);
177 } catch (AssertionFailedException $e) {
178 $this->errors[] = $e;
179 $this->currentChainFailed = true;
180 }
181
182 return $this;
183 }
184
185 /**
186 * @throws LazyAssertionException
187 */
188 public function verifyNow(): bool
189 {
190 if ($this->errors) {
191 throw \call_user_func([$this->exceptionClass, 'fromErrors'], $this->errors);
192 }
193
194 return true;
195 }
196
197 /**
198 * @param string $className
199 *
200 * @return static
201 */
202 public function setAssertClass(string $className): LazyAssertion
203 {
204 if (Assert::class !== $className && !\is_subclass_of($className, Assert::class)) {
205 throw new LogicException($className.' is not (a subclass of) '.Assert::class);
206 }
207
208 $this->assertClass = $className;
209
210 return $this;
211 }
212
213 /**
214 * @param string $className
215 *
216 * @return static
217 */
218 public function setExceptionClass(string $className): LazyAssertion
219 {
220 if (LazyAssertionException::class !== $className && !\is_subclass_of($className, LazyAssertionException::class)) {
221 throw new LogicException($className.' is not (a subclass of) '.LazyAssertionException::class);
222 }
223
224 $this->exceptionClass = $className;
225
226 return $this;
227 }
228 }
229