Completed
Push — master ( 602b35...1783b4 )
by Richard
01:45
created

LazyAssertion   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 60
rs 10
c 1
b 0
f 0
wmc 10
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A that() 0 8 1
A tryAll() 0 10 2
B __call() 0 18 5
A verifyNow() 0 8 2
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 $alwaysTryAll = false;
100
    private $thisChainTryAll = false;
101
    private $currentChain;
102
    private $errors = array();
103
104
    public function that($value, $propertyPath, $defaultMessage = null)
105
    {
106
        $this->currentChainFailed = false;
107
        $this->thisChainTryAll = false;
108
        $this->currentChain = \Assert\that($value, $defaultMessage, $propertyPath);
109
110
        return $this;
111
    }
112
113
    public function tryAll()
114
    {
115
        if (!$this->currentChain) {
116
            $this->alwaysTryAll = true;
117
        }
118
119
        $this->thisChainTryAll = true;
120
121
        return $this;
122
    }
123
124
    public function __call($method, $args)
125
    {
126
        if ($this->alwaysTryAll === false
127
            && $this->thisChainTryAll === false
128
            && $this->currentChainFailed === true
129
        ) {
130
            return $this;
131
        }
132
133
        try {
134
            call_user_func_array(array($this->currentChain, $method), $args);
135
        } catch (AssertionFailedException $e) {
136
            $this->errors[] = $e;
137
            $this->currentChainFailed = true;
138
        }
139
140
        return $this;
141
    }
142
143
    /**
144
     * @throws \Assert\LazyAssertionException
145
     * @return bool
146
     */
147
    public function verifyNow()
148
    {
149
        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...
150
            throw LazyAssertionException::fromErrors($this->errors);
151
        }
152
153
        return true;
154
    }
155
}
156