Assert::getErrorMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Common;
26
27
use InvalidArgumentException;
28
29
/**
30
 * Assert
31
 *
32
 * Assert the type of an argument value or values.
33
 * @author Celestino Diaz <[email protected]>
34
 */
35
class Assert {
36
37
    /** @var boolean */
38
    public static $throwExceptions = true;
39
40
    /**
41
     * Check if the argument is a string and not empty, accepts empty strings.
42
     * @param string $argument the arguments to validate
43
     * @throws \InvalidArgumentException if the validation fails
44
     * @return boolean check result
45
     */
46 2
    public static function isString($argument) {
47 2
        return self::handleArgumentValidation(is_string($argument),
48 2
            $argument, "The argument must be of type string."
49 2
        );
50
    }
51
52
    /**
53
     * Check if the argument is an integer.
54
     * @param integer $argument the argument to check
55
     * @throws \InvalidArgumentException if the validation fails
56
     * @return boolean check result
57
     */
58 2
    public static function isInteger($argument) {
59 2
        return self::handleArgumentValidation(is_int($argument),
60 2
            $argument, "The argument must be of type integer."
61 2
        );
62
    }
63
64
    /**
65
     * Check if the argument is a string or a integer, accepts empty values.
66
     * @param string $argument the argument to validate
67
     * @throws \InvalidArgumentException if the validation fails
68
     * @return boolean check result
69
     */
70 2
    public static function isStringOrInteger($argument) {
71 2
        return self::handleArgumentValidation(
72 2
            (is_string($argument) || is_int($argument)),
73 2
            $argument, "The argument must be of type integer or string."
74 2
        );
75
    }
76
77
    /**
78
     * Check if the arguments is a float.
79
     * @param float $argument the argument to check
80
     * @throws \InvalidArgumentException if the validation fails
81
     * @return boolean check result
82
     */
83 2
    public static function isFloat($argument) {
84 2
        return self::handleArgumentValidation(is_float($argument),
85 2
            $argument, "The argument must be of type float."
86 2
        );
87
    }
88
89
    /**
90
     * Check if the argument is a boolean.
91
     * @param boolean $argument the argument to validate
92
     * @throws \InvalidArgumentException if the validation fails
93
     * @return boolean check result
94
     */
95 2
    public static function isBoolean($argument) {
96 2
        return self::handleArgumentValidation(is_bool($argument),
97 2
            $argument, "The argument must be of type boolean."
98 2
        );
99
    }
100
101
    /**
102
     * Check if the argument is not empty.
103
     * @param mixed $argument the argument to validate
104
     * @throws \InvalidArgumentException if the validation fails
105
     * @return boolean check result
106
     */
107 2
    public static function isNotEmpty($argument) {
108 2
        return self::handleArgumentValidation((!empty($argument)),
109 2
            $argument, "The argument must not be empty."
110 2
        );
111
    }
112
113
    /**
114
     * Check if a function is available by its name.
115
     * @param string $argument the argument to validate
116
     * @throws \InvalidArgumentException if the validation fails
117
     * @return boolean check result
118
     */
119 2
    public static function isFunctionAvailable($argument) {
120 2
        return self::handleArgumentValidation(function_exists($argument),
121 2
            $argument, "The argument must be an available function."
122 2
        );
123
    }
124
125
    /**
126
     * Check if the argument is traversable.
127
     * @param \Traversable|array $argument the argument to validate
128
     * @throws \InvalidArgumentException if the validation fails
129
     * @return boolean check result
130
     */
131 2
    public static function isTraversable($argument) {
132 2
        return self::handleArgumentValidation(
133 2
            (is_array($argument) || ($argument instanceof \Traversable)),
134 2
            $argument, "The argument must be traversable."
135 2
        );
136
    }
137
138
    /**
139
     * Check if the argument is callable.
140
     * @param callable $argument the argument to validate
141
     * @throws \InvalidArgumentException if the validation fails
142
     * @return boolean check result
143
     */
144 2
    public static function isCallable($argument) {
145 2
        return self::handleArgumentValidation(is_callable($argument),
146 2
            $argument, "The argument must be callable."
147 2
        );
148
    }
149
150
    /**
151
     * Check if the argument is an object.
152
     * @param object $argument the argument to validate
153
     * @throws \InvalidArgumentException if the validation fails
154
     * @return boolean check result
155
     */
156 2
    public static function isObject($argument) {
157 2
        return self::handleArgumentValidation(is_object($argument),
158 2
            $argument, "The argument must be an object."
159 2
        );
160
    }
161
162
    /**
163
     * Check if the argument is a resource.
164
     * @param resource $argument the argument to validate
165
     * @throws \InvalidArgumentException if the validation fails
166
     * @return boolean check result
167
     */
168 2
    public static function isResource($argument) {
169 2
        return self::handleArgumentValidation(is_resource($argument),
170 2
            $argument, "The argument must be a resource."
171 2
        );
172
    }
173
174
    /**
175
     * Handle an invalid argument.
176
     * @param boolean $validationSuccess
177
     * @param mixed $argument
178
     * @param string $errorMessage
179
     * @throws \InvalidArgumentException
180
     * @return boolean false if exception is turned off
181
     */
182 14
    public static function handleArgumentValidation($validationSuccess, $argument, $errorMessage) {
183 14
        if ($validationSuccess) {
184 11
            return true;
185
        }
186
187 3
        if (self::$throwExceptions) {
188 1
            throw self::getInvalidArgumentException($argument, $errorMessage);
189
        }
190 2
        trigger_error(self::getErrorMessage($argument, $errorMessage), E_USER_WARNING);
191 1
        return false;
192
    }
193
194
    /**
195
     * Throw an \InvalidArgumentException describing the argument and adding a helpful error message.
196
     * @param mixed $argument the arguments which are invalid
197
     * @param string $errorMessage the error message to attach
198
     * @return \InvalidArgumentException
199
     */
200 2
    public static function getInvalidArgumentException($argument, $errorMessage) {
201 2
        return new InvalidArgumentException(self::getErrorMessage($argument, $errorMessage));
202
    }
203
204
    /**
205
     * Return the formatted error message.
206
     * @param mixed $argument
207
     * @param string $errorMessage
208
     * @return string the error message
209
     */
210 3
    private static function getErrorMessage($argument, $errorMessage) {
211 3
        return sprintf(
212 3
            "Unexpected argument %s. %s",
213 3
            self::getArgumentStringRepresentation($argument),
214
            $errorMessage
215 3
        );
216
    }
217
218
    /**
219
     * Return the argument string representation.
220
     * @param mixed $argument the argument to return the representation
221
     * @return string the argument representation
222
     */
223 4
    private static function getArgumentStringRepresentation($argument) {
224 4
        switch (gettype($argument)) {
225 4
            case "object":
226 3
                $representation = sprintf("[object #%s] %s", spl_object_hash($argument), get_class($argument));
227 3
                break;
228 1
            default:
229 1
                $representation = sprintf(
230 1
                    "[%s] %s",
231 1
                    gettype($argument),
232 1
                    str_replace(["\r", "\n", " "], "", var_export($argument, true))
233 1
                );
234 4
        }
235 4
        return $representation;
236
    }
237
238
}
239