1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Helpers; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Traversable; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Fenos\Notifynder\Models\Notification; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TypeChecker. |
13
|
|
|
*/ |
14
|
|
|
class TypeChecker |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param $value |
18
|
|
|
* @param bool $strict |
19
|
|
|
* @return bool |
20
|
|
|
* @throws InvalidArgumentException |
21
|
|
|
*/ |
22
|
|
|
public static function isString($value, $strict = true) |
23
|
|
|
{ |
24
|
|
|
if (! is_string($value)) { |
25
|
|
|
if ($strict) { |
26
|
|
|
throw new InvalidArgumentException('The value passed must be a string'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $value |
37
|
|
|
* @param bool $strict |
38
|
|
|
* @return bool |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
*/ |
41
|
|
|
public static function isNumeric($value, $strict = true) |
42
|
|
|
{ |
43
|
|
|
if (! is_numeric($value)) { |
44
|
|
|
if ($strict) { |
45
|
|
|
throw new InvalidArgumentException('The value passed must be a number'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $value |
56
|
|
|
* @param bool $strict |
57
|
|
|
* @return bool |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public static function isDate($value, $strict = true) |
61
|
|
|
{ |
62
|
|
|
if ($value instanceof Carbon || $value instanceof DateTime) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($strict) { |
67
|
|
|
throw new InvalidArgumentException('The value passed must be an instance of Carbon\\Carbon or DateTime'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $value |
75
|
|
|
* @param bool $strict |
76
|
|
|
* @return bool |
77
|
|
|
* @throws InvalidArgumentException |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public static function isArray($value, $strict = true) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (self::isIterable($value, $strict) && is_array($value)) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($strict) { |
86
|
|
|
throw new InvalidArgumentException('The value passed must be an array'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $value |
94
|
|
|
* @param bool $strict |
95
|
|
|
* @return bool |
96
|
|
|
* @throws InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public static function isIterable($value, $strict = true) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if ((is_array($value) || $value instanceof Traversable) && count($value) > 0) { |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($strict) { |
105
|
|
|
throw new InvalidArgumentException('The value passed must be iterable'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $notification |
113
|
|
|
* @param bool $strict |
114
|
|
|
* @return bool |
115
|
|
|
* @throws InvalidArgumentException |
116
|
|
|
*/ |
117
|
|
|
public static function isNotification($notification, $strict = true) |
118
|
|
|
{ |
119
|
|
|
if (! is_a($notification, app('notifynder.resolver.model')->getModel(Notification::class))) { |
120
|
|
|
if ($strict) { |
121
|
|
|
throw new InvalidArgumentException('The value passed must be an Notification Model instance'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.