|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Assert |
|
4
|
|
|
* |
|
5
|
|
|
* LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this package in the file LICENSE.txt. |
|
9
|
|
|
* If you did not receive a copy of the license and are unable to |
|
10
|
|
|
* obtain it through the world-wide-web, please send an email |
|
11
|
|
|
* to [email protected] so I can send you a copy immediately. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Assert; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Chaining builder for lazy assertions |
|
18
|
|
|
* |
|
19
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @method LazyAssertion alnum($message = null, $propertyPath = null) Assert that value is alphanumeric. |
|
22
|
|
|
* @method LazyAssertion between($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
|
23
|
|
|
* @method LazyAssertion betweenExclusive($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit. |
|
24
|
|
|
* @method LazyAssertion betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths. |
|
25
|
|
|
* @method LazyAssertion boolean($message = null, $propertyPath = null) Assert that value is php boolean. |
|
26
|
|
|
* @method LazyAssertion choice($choices, $message = null, $propertyPath = null) Assert that value is in array of choices. |
|
27
|
|
|
* @method LazyAssertion choicesNotEmpty($choices, $message = null, $propertyPath = null) Determines if the values array has every choice as key and that this choice has content. |
|
28
|
|
|
* @method LazyAssertion classExists($message = null, $propertyPath = null) Assert that the class exists. |
|
29
|
|
|
* @method LazyAssertion contains($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars. |
|
30
|
|
|
* @method LazyAssertion count($count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count. |
|
31
|
|
|
* @method LazyAssertion date($format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format. |
|
32
|
|
|
* @method LazyAssertion digit($message = null, $propertyPath = null) Validates if an integer or integerish is a digit. |
|
33
|
|
|
* @method LazyAssertion directory($message = null, $propertyPath = null) Assert that a directory exists. |
|
34
|
|
|
* @method LazyAssertion e164($message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number. |
|
35
|
|
|
* @method LazyAssertion email($message = null, $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
|
36
|
|
|
* @method LazyAssertion endsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars. |
|
37
|
|
|
* @method LazyAssertion eq($value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ). |
|
38
|
|
|
* @method LazyAssertion false($message = null, $propertyPath = null) Assert that the value is boolean False. |
|
39
|
|
|
* @method LazyAssertion file($message = null, $propertyPath = null) Assert that a file exists. |
|
40
|
|
|
* @method LazyAssertion float($message = null, $propertyPath = null) Assert that value is a php float. |
|
41
|
|
|
* @method LazyAssertion greaterOrEqualThan($limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit. |
|
42
|
|
|
* @method LazyAssertion greaterThan($limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit. |
|
43
|
|
|
* @method LazyAssertion implementsInterface($interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface. |
|
44
|
|
|
* @method LazyAssertion inArray($choices, $message = null, $propertyPath = null) Alias of {@see choice()}. |
|
45
|
|
|
* @method LazyAssertion integer($message = null, $propertyPath = null) Assert that value is a php integer. |
|
46
|
|
|
* @method LazyAssertion integerish($message = null, $propertyPath = null) Assert that value is a php integer'ish. |
|
47
|
|
|
* @method LazyAssertion interfaceExists($message = null, $propertyPath = null) Assert that the interface exists. |
|
48
|
|
|
* @method LazyAssertion ip($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 or IPv6 address. |
|
49
|
|
|
* @method LazyAssertion ipv4($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 address. |
|
50
|
|
|
* @method LazyAssertion ipv6($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv6 address. |
|
51
|
|
|
* @method LazyAssertion isArray($message = null, $propertyPath = null) Assert that value is an array. |
|
52
|
|
|
* @method LazyAssertion isArrayAccessible($message = null, $propertyPath = null) Assert that value is an array or an array-accessible object. |
|
53
|
|
|
* @method LazyAssertion isCallable($message = null, $propertyPath = null) Determines that the provided value is callable. |
|
54
|
|
|
* @method LazyAssertion isInstanceOf($className, $message = null, $propertyPath = null) Assert that value is instance of given class-name. |
|
55
|
|
|
* @method LazyAssertion isJsonString($message = null, $propertyPath = null) Assert that the given string is a valid json string. |
|
56
|
|
|
* @method LazyAssertion isObject($message = null, $propertyPath = null) Determines that the provided value is an object. |
|
57
|
|
|
* @method LazyAssertion isTraversable($message = null, $propertyPath = null) Assert that value is an array or a traversable object. |
|
58
|
|
|
* @method LazyAssertion keyExists($key, $message = null, $propertyPath = null) Assert that key exists in an array. |
|
59
|
|
|
* @method LazyAssertion keyIsset($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object using isset(). |
|
60
|
|
|
* @method LazyAssertion keyNotExists($key, $message = null, $propertyPath = null) Assert that key does not exist in an array. |
|
61
|
|
|
* @method LazyAssertion length($length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length. |
|
62
|
|
|
* @method LazyAssertion lessOrEqualThan($limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit. |
|
63
|
|
|
* @method LazyAssertion lessThan($limit, $message = null, $propertyPath = null) Determines if the value is less than given limit. |
|
64
|
|
|
* @method LazyAssertion max($maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit. |
|
65
|
|
|
* @method LazyAssertion maxLength($maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string value is not longer than $maxLength chars. |
|
66
|
|
|
* @method LazyAssertion methodExists($object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object. |
|
67
|
|
|
* @method LazyAssertion min($minValue, $message = null, $propertyPath = null) Assert that a value is at least as big as a given limit. |
|
68
|
|
|
* @method LazyAssertion minLength($minLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that a string is at least $minLength chars long. |
|
69
|
|
|
* @method LazyAssertion noContent($message = null, $propertyPath = null) Assert that value is empty. |
|
70
|
|
|
* @method LazyAssertion notBlank($message = null, $propertyPath = null) Assert that value is not blank. |
|
71
|
|
|
* @method LazyAssertion notEmpty($message = null, $propertyPath = null) Assert that value is not empty. |
|
72
|
|
|
* @method LazyAssertion notEmptyKey($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty. |
|
73
|
|
|
* @method LazyAssertion notEq($value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ). |
|
74
|
|
|
* @method LazyAssertion notInArray($choices, $message = null, $propertyPath = null) Assert that value is not in array of choices. |
|
75
|
|
|
* @method LazyAssertion notIsInstanceOf($className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name. |
|
76
|
|
|
* @method LazyAssertion notNull($message = null, $propertyPath = null) Assert that value is not null. |
|
77
|
|
|
* @method LazyAssertion notSame($value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ). |
|
78
|
|
|
* @method LazyAssertion null($message = null, $propertyPath = null) Assert that value is null. |
|
79
|
|
|
* @method LazyAssertion numeric($message = null, $propertyPath = null) Assert that value is numeric. |
|
80
|
|
|
* @method LazyAssertion range($minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers. |
|
81
|
|
|
* @method LazyAssertion readable($message = null, $propertyPath = null) Assert that the value is something readable. |
|
82
|
|
|
* @method LazyAssertion regex($pattern, $message = null, $propertyPath = null) Assert that value matches a regex. |
|
83
|
|
|
* @method LazyAssertion same($value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===). |
|
84
|
|
|
* @method LazyAssertion satisfy($callback, $message = null, $propertyPath = null) Assert that the provided value is valid according to a callback. |
|
85
|
|
|
* @method LazyAssertion scalar($message = null, $propertyPath = null) Assert that value is a PHP scalar. |
|
86
|
|
|
* @method LazyAssertion startsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars. |
|
87
|
|
|
* @method LazyAssertion string($message = null, $propertyPath = null) Assert that value is a string. |
|
88
|
|
|
* @method LazyAssertion subclassOf($className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name. |
|
89
|
|
|
* @method LazyAssertion true($message = null, $propertyPath = null) Assert that the value is boolean True. |
|
90
|
|
|
* @method LazyAssertion url($message = null, $propertyPath = null) Assert that value is an URL. |
|
91
|
|
|
* @method LazyAssertion uuid($message = null, $propertyPath = null) Assert that the given string is a valid UUID. |
|
92
|
|
|
* @method LazyAssertion writeable($message = null, $propertyPath = null) Assert that the value is something writeable. |
|
93
|
|
|
* @method LazyAssertion all() Switch chain into validation mode for an array of values. |
|
94
|
|
|
* @method LazyAssertion nullOr() Switch chain into mode allowing nulls, ignoring further assertions. |
|
95
|
|
|
*/ |
|
96
|
|
|
class LazyAssertion |
|
97
|
|
|
{ |
|
98
|
|
|
private $currentChainFailed = false; |
|
99
|
|
|
private $shouldTryAll = false; |
|
100
|
|
|
private $currentChain; |
|
101
|
|
|
private $errors = array(); |
|
102
|
|
|
|
|
103
|
|
|
public function that($value, $propertyPath, $defaultMessage = null) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->currentChainFailed = false; |
|
106
|
|
|
$this->shouldTryAll = false; |
|
107
|
|
|
$this->currentChain = \Assert\that($value, $defaultMessage, $propertyPath); |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function tryAll() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->shouldTryAll = true; |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function __call($method, $args) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($this->shouldTryAll === false && $this->currentChainFailed === true) { |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
try { |
|
125
|
|
|
call_user_func_array(array($this->currentChain, $method), $args); |
|
126
|
|
|
} catch (AssertionFailedException $e) { |
|
127
|
|
|
$this->errors[] = $e; |
|
128
|
|
|
$this->currentChainFailed = true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @throws \Assert\LazyAssertionException |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
public function verifyNow() |
|
139
|
|
|
{ |
|
140
|
|
|
if ($this->errors) { |
|
|
|
|
|
|
141
|
|
|
throw LazyAssertionException::fromErrors($this->errors); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.