1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPCollections; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An utility class for checking |
11
|
|
|
* different type of data. |
12
|
|
|
*/ |
13
|
|
|
class Checker |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Checks that two values are equals. |
17
|
|
|
* |
18
|
|
|
* @param mixed $firstValue |
19
|
|
|
* @param mixed $secondValue |
20
|
|
|
* @param string $message |
21
|
|
|
* |
22
|
|
|
* @throws \InvalidArgumentException |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
1 |
|
public static function isEqual($firstValue, $secondValue, string $message): bool |
27
|
|
|
{ |
28
|
1 |
|
if ($firstValue !== $secondValue) { |
29
|
1 |
|
throw new InvalidArgumentException($message); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Checks that a value is and object |
37
|
|
|
* if not throws an exception. |
38
|
|
|
* |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @param string $message |
41
|
|
|
* |
42
|
|
|
* @throws \InvalidArgumentException |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
2 |
|
public static function isObject($value, string $message): bool |
47
|
|
|
{ |
48
|
2 |
|
if (!is_object($value)) { |
49
|
1 |
|
throw new InvalidArgumentException($message); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks that an object is of the desired |
57
|
|
|
* type, if not throws an exception. |
58
|
|
|
* |
59
|
|
|
* @param object $object |
60
|
|
|
* @param string $type |
61
|
|
|
* @param string $message |
62
|
|
|
* |
63
|
|
|
* @throws \InvalidArgumentException |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
31 |
|
public static function objectIsOfType(object $object, string $type, string $message): bool |
68
|
|
|
{ |
69
|
31 |
|
if (!$object instanceof $type) { |
70
|
3 |
|
throw new InvalidArgumentException($message); |
71
|
|
|
} |
72
|
|
|
|
73
|
30 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks that a Dictionary key or value |
78
|
|
|
* is of the desire type, if not throws |
79
|
|
|
* an exception. |
80
|
|
|
* |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @param mixed $valueType |
83
|
|
|
* @param string $message |
84
|
|
|
* |
85
|
|
|
* @throws \InvalidArgumentException |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
36 |
|
public static function valueIsOfType($value, $valueType, string $message): bool |
90
|
|
|
{ |
91
|
36 |
|
$newValueType = is_object($value) ? get_class($value) : gettype($value); |
92
|
|
|
|
93
|
36 |
|
if ($newValueType != $valueType) { |
94
|
4 |
|
throw new InvalidArgumentException($message); |
95
|
|
|
} |
96
|
|
|
|
97
|
35 |
|
return true; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|