Completed
Push — master ( ef8414...a67d49 )
by Richard
10s
created

LazyAssertion::setExceptionClass()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
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
use LogicException;
17
18
/**
19
 * Chaining builder for lazy assertions
20
 *
21
 * @author Benjamin Eberlei <[email protected]>
22
 *
23
 * @method LazyAssertion alnum($message = null, $propertyPath = null) Assert that value is alphanumeric.
24
 * @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.
25
 * @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.
26
 * @method LazyAssertion betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths.
27
 * @method LazyAssertion boolean($message = null, $propertyPath = null) Assert that value is php boolean.
28
 * @method LazyAssertion choice($choices, $message = null, $propertyPath = null) Assert that value is in array of choices.
29
 * @method LazyAssertion choicesNotEmpty($choices, $message = null, $propertyPath = null) Determines if the values array has every choice as key and that this choice has content.
30
 * @method LazyAssertion classExists($message = null, $propertyPath = null) Assert that the class exists.
31
 * @method LazyAssertion contains($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars.
32
 * @method LazyAssertion count($count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count.
33
 * @method LazyAssertion date($format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format.
34
 * @method LazyAssertion digit($message = null, $propertyPath = null) Validates if an integer or integerish is a digit.
35
 * @method LazyAssertion directory($message = null, $propertyPath = null) Assert that a directory exists.
36
 * @method LazyAssertion e164($message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number.
37
 * @method LazyAssertion email($message = null, $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL).
38
 * @method LazyAssertion endsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars.
39
 * @method LazyAssertion eq($value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ).
40
 * @method LazyAssertion false($message = null, $propertyPath = null) Assert that the value is boolean False.
41
 * @method LazyAssertion file($message = null, $propertyPath = null) Assert that a file exists.
42
 * @method LazyAssertion float($message = null, $propertyPath = null) Assert that value is a php float.
43
 * @method LazyAssertion greaterOrEqualThan($limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit.
44
 * @method LazyAssertion greaterThan($limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit.
45
 * @method LazyAssertion implementsInterface($interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface.
46
 * @method LazyAssertion inArray($choices, $message = null, $propertyPath = null) Alias of {@see choice()}.
47
 * @method LazyAssertion integer($message = null, $propertyPath = null) Assert that value is a php integer.
48
 * @method LazyAssertion integerish($message = null, $propertyPath = null) Assert that value is a php integer'ish.
49
 * @method LazyAssertion interfaceExists($message = null, $propertyPath = null) Assert that the interface exists.
50
 * @method LazyAssertion ip($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 or IPv6 address.
51
 * @method LazyAssertion ipv4($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 address.
52
 * @method LazyAssertion ipv6($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv6 address.
53
 * @method LazyAssertion isArray($message = null, $propertyPath = null) Assert that value is an array.
54
 * @method LazyAssertion isArrayAccessible($message = null, $propertyPath = null) Assert that value is an array or an array-accessible object.
55
 * @method LazyAssertion isCallable($message = null, $propertyPath = null) Determines that the provided value is callable.
56
 * @method LazyAssertion isInstanceOf($className, $message = null, $propertyPath = null) Assert that value is instance of given class-name.
57
 * @method LazyAssertion isJsonString($message = null, $propertyPath = null) Assert that the given string is a valid json string.
58
 * @method LazyAssertion isObject($message = null, $propertyPath = null) Determines that the provided value is an object.
59
 * @method LazyAssertion isTraversable($message = null, $propertyPath = null) Assert that value is an array or a traversable object.
60
 * @method LazyAssertion keyExists($key, $message = null, $propertyPath = null) Assert that key exists in an array.
61
 * @method LazyAssertion keyIsset($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object using isset().
62
 * @method LazyAssertion keyNotExists($key, $message = null, $propertyPath = null) Assert that key does not exist in an array.
63
 * @method LazyAssertion length($length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length.
64
 * @method LazyAssertion lessOrEqualThan($limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit.
65
 * @method LazyAssertion lessThan($limit, $message = null, $propertyPath = null) Determines if the value is less than given limit.
66
 * @method LazyAssertion max($maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit.
67
 * @method LazyAssertion maxLength($maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string value is not longer than $maxLength chars.
68
 * @method LazyAssertion methodExists($object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object.
69
 * @method LazyAssertion min($minValue, $message = null, $propertyPath = null) Assert that a value is at least as big as a given limit.
70
 * @method LazyAssertion minLength($minLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that a string is at least $minLength chars long.
71
 * @method LazyAssertion noContent($message = null, $propertyPath = null) Assert that value is empty.
72
 * @method LazyAssertion notBlank($message = null, $propertyPath = null) Assert that value is not blank.
73
 * @method LazyAssertion notEmpty($message = null, $propertyPath = null) Assert that value is not empty.
74
 * @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.
75
 * @method LazyAssertion notEq($value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ).
76
 * @method LazyAssertion notInArray($choices, $message = null, $propertyPath = null) Assert that value is not in array of choices.
77
 * @method LazyAssertion notIsInstanceOf($className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name.
78
 * @method LazyAssertion notNull($message = null, $propertyPath = null) Assert that value is not null.
79
 * @method LazyAssertion notSame($value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ).
80
 * @method LazyAssertion null($message = null, $propertyPath = null) Assert that value is null.
81
 * @method LazyAssertion numeric($message = null, $propertyPath = null) Assert that value is numeric.
82
 * @method LazyAssertion range($minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers.
83
 * @method LazyAssertion readable($message = null, $propertyPath = null) Assert that the value is something readable.
84
 * @method LazyAssertion regex($pattern, $message = null, $propertyPath = null) Assert that value matches a regex.
85
 * @method LazyAssertion same($value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===).
86
 * @method LazyAssertion satisfy($callback, $message = null, $propertyPath = null) Assert that the provided value is valid according to a callback.
87
 * @method LazyAssertion scalar($message = null, $propertyPath = null) Assert that value is a PHP scalar.
88
 * @method LazyAssertion startsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars.
89
 * @method LazyAssertion string($message = null, $propertyPath = null) Assert that value is a string.
90
 * @method LazyAssertion subclassOf($className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name.
91
 * @method LazyAssertion true($message = null, $propertyPath = null) Assert that the value is boolean True.
92
 * @method LazyAssertion url($message = null, $propertyPath = null) Assert that value is an URL.
93
 * @method LazyAssertion uuid($message = null, $propertyPath = null) Assert that the given string is a valid UUID.
94
 * @method LazyAssertion writeable($message = null, $propertyPath = null) Assert that the value is something writeable.
95
 * @method LazyAssertion all() Switch chain into validation mode for an array of values.
96
 * @method LazyAssertion nullOr() Switch chain into mode allowing nulls, ignoring further assertions.
97
 */
98
class LazyAssertion
99
{
100
    private $currentChainFailed = false;
101
    private $alwaysTryAll = false;
102
    private $thisChainTryAll = false;
103
    private $currentChain;
104
    private $errors = array();
105
106
    /** @var string|LazyAssertionException The class to use for exceptions */
107
    private $exceptionClass = 'Assert\LazyAssertionException';
108
109
    public function that($value, $propertyPath, $defaultMessage = null)
110
    {
111
        $this->currentChainFailed = false;
112
        $this->thisChainTryAll = false;
113
        $this->currentChain = Assert::that($value, $defaultMessage, $propertyPath);
114
115
        return $this;
116
    }
117
118
    public function tryAll()
119
    {
120
        if (!$this->currentChain) {
121
            $this->alwaysTryAll = true;
122
        }
123
124
        $this->thisChainTryAll = true;
125
126
        return $this;
127
    }
128
129
    public function __call($method, $args)
130
    {
131
        if ($this->alwaysTryAll === false
132
            && $this->thisChainTryAll === false
133
            && $this->currentChainFailed === true
134
        ) {
135
            return $this;
136
        }
137
138
        try {
139
            call_user_func_array(array($this->currentChain, $method), $args);
140
        } catch (AssertionFailedException $e) {
141
            $this->errors[] = $e;
142
            $this->currentChainFailed = true;
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * @throws \Assert\LazyAssertionException
150
     * @return bool
151
     */
152
    public function verifyNow()
153
    {
154
        if ($this->errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
155
            throw call_user_func(array($this->exceptionClass, 'fromErrors'), $this->errors);
156
        }
157
158
        return true;
159
    }
160
161
    /**
162
     * @param string $className
163
     * @return $this
164
     */
165
    public function setExceptionClass($className)
166
    {
167
        if (!is_string($className)) {
168
            throw new LogicException('Exception class name must be passed as a string');
169
        }
170
171
        if ($className !== 'Assert\LazyAssertionException' && !is_subclass_of($className, 'Assert\LazyAssertionException')) {
172
            throw new LogicException($className . ' is not (a subclass of) Assert\LazyAssertionException');
173
        }
174
175
        $this->exceptionClass = $className;
176
        return $this;
177
    }
178
}
179