Test Failed
Pull Request — master (#74)
by Anatoly
05:22 queued 02:58
created

InvalidArgumentException   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 22
c 1
b 0
f 0
dl 0
loc 136
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getInvalidValue() 0 3 1
A throwIfEmptyArray() 0 5 3
A throwIfNotArray() 0 5 2
A throwIfNotString() 0 5 2
A throwIfEmptyString() 0 5 3
A throwIfEmpty() 0 5 3
A throwIfNotInteger() 0 5 2
A throwIfNotImplemented() 0 5 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Exception;
13
14
/**
15
 * Import functions
16
 */
17
use function is_array;
18
use function is_int;
19
use function is_string;
20
use function is_subclass_of;
21
22
/**
23
 * InvalidArgumentException
24
 *
25
 * @since 2.9.0
26
 */
27
class InvalidArgumentException extends Exception
28
{
29
30
    /**
31
     * Gets an invalid value
32
     *
33
     * @return mixed
34
     */
35
    public function getInvalidValue()
36
    {
37
        return $this->fromContext('invalidValue');
38
    }
39
40
    /**
41
     * Throws the exception if the given operand isn't an array
42
     *
43
     * @param mixed $operand
44
     * @param string $message
45
     *
46
     * @return void
47
     * @throws static
48
     */
49
    public static function throwIfNotArray($operand, string $message) : void
50
    {
51
        if (!is_array($operand)) {
52
            throw new static($message, [
53
                'invalidValue' => $operand,
54
            ]);
55
        }
56
    }
57
58
    /**
59
     * Throws the exception if the given operand isn't an integer
60
     *
61
     * @param mixed $operand
62
     * @param string $message
63
     *
64
     * @return void
65
     * @throws static
66
     */
67
    public static function throwIfNotInteger($operand, string $message) : void
68
    {
69
        if (!is_int($operand)) {
70
            throw new static($message, [
71
                'invalidValue' => $operand,
72
            ]);
73
        }
74
    }
75
76
    /**
77
     * Throws the exception if the given operand isn't a string
78
     *
79
     * @param mixed $operand
80
     * @param string $message
81
     *
82
     * @return void
83
     * @throws static
84
     */
85
    public static function throwIfNotString($operand, string $message) : void
86
    {
87
        if (!is_string($operand)) {
88
            throw new static($message, [
89
                'invalidValue' => $operand,
90
            ]);
91
        }
92
    }
93
94
    /**
95
     * Throws the exception if the given operand doesn't implement the given class
96
     *
97
     * @param mixed $operand
98
     * @param string $className
99
     * @param string $message
100
     *
101
     * @return void
102
     * @throws static
103
     */
104
    public static function throwIfNotImplemented($operand, string $className, string $message) : void
105
    {
106
        if (!is_subclass_of($operand, $className)) {
107
            throw new static($message, [
108
                'invalidValue' => $operand,
109
            ]);
110
        }
111
    }
112
113
    /**
114
     * Throws the exception if the given operand is an empty
115
     *
116
     * @param mixed $operand
117
     * @param string $message
118
     *
119
     * @return void
120
     * @throws static
121
     */
122
    public static function throwIfEmpty($operand, string $message) : void
123
    {
124
        if ('' === $operand || [] === $operand) {
125
            throw new static($message, [
126
                'invalidValue' => $operand,
127
            ]);
128
        }
129
    }
130
131
    /**
132
     * Throws the exception if the given operand isn't an array or empty
133
     *
134
     * @param mixed $operand
135
     * @param string $message
136
     *
137
     * @return void
138
     * @throws static
139
     */
140
    public static function throwIfEmptyArray($operand, string $message) : void
141
    {
142
        if ([] === $operand || !is_array($operand)) {
143
            throw new static($message, [
144
                'invalidValue' => $operand,
145
            ]);
146
        }
147
    }
148
149
    /**
150
     * Throws the exception if the given operand isn't a string or empty
151
     *
152
     * @param mixed $operand
153
     * @param string $message
154
     *
155
     * @return void
156
     * @throws static
157
     */
158
    public static function throwIfEmptyString($operand, string $message) : void
159
    {
160
        if ('' === $operand || !is_string($operand)) {
161
            throw new static($message, [
162
                'invalidValue' => $operand,
163
            ]);
164
        }
165
    }
166
}
167