Completed
Push — master ( 2f25c9...4c0020 )
by
unknown
06:10
created

TypeChecker::isArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 12
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Fenos\Notifynder\Helpers;
4
5
use DateTime;
6
use Traversable;
7
use Carbon\Carbon;
8
use InvalidArgumentException;
9
10
/**
11
 * Class TypeChecker.
12
 */
13
class TypeChecker
14
{
15
    /**
16
     * @param $value
17
     * @param bool $strict
18
     * @return bool
19
     * @throws InvalidArgumentException
20
     */
21
    public static function isString($value, $strict = true)
22
    {
23
        if (! is_string($value)) {
24
            if ($strict) {
25
                throw new InvalidArgumentException('The value passed must be a string');
26
            }
27
28
            return false;
29
        }
30
31
        return true;
32
    }
33
34
    /**
35
     * @param $value
36
     * @param bool $strict
37
     * @return bool
38
     * @throws InvalidArgumentException
39
     */
40
    public static function isNumeric($value, $strict = true)
41
    {
42
        if (! is_numeric($value)) {
43
            if ($strict) {
44
                throw new InvalidArgumentException('The value passed must be a number');
45
            }
46
47
            return false;
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * @param $value
55
     * @param bool $strict
56
     * @return bool
57
     * @throws InvalidArgumentException
58
     */
59
    public static function isDate($value, $strict = true)
60
    {
61
        if ($value instanceof Carbon || $value instanceof DateTime) {
62
            return true;
63
        }
64
65
        if ($strict) {
66
            throw new InvalidArgumentException('The value passed must be an instance of Carbon\\Carbon or DateTime');
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * @param $value
74
     * @param bool $strict
75
     * @return bool
76
     * @throws InvalidArgumentException
77
     */
78 View Code Duplication
    public static function isArray($value, $strict = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
79
    {
80
        if (self::isIterable($value, $strict) && is_array($value)) {
81
            return true;
82
        }
83
84
        if ($strict) {
85
            throw new InvalidArgumentException('The value passed must be an array');
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param $value
93
     * @param bool $strict
94
     * @return bool
95
     * @throws InvalidArgumentException
96
     */
97 View Code Duplication
    public static function isIterable($value, $strict = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
    {
99
        if ((is_array($value) || $value instanceof Traversable) && count($value) > 0) {
100
            return true;
101
        }
102
103
        if ($strict) {
104
            throw new InvalidArgumentException('The value passed must be iterable');
105
        }
106
107
        return false;
108
    }
109
110
    /**
111
     * @param $notification
112
     * @param bool $strict
113
     * @return bool
114
     * @throws InvalidArgumentException
115
     */
116
    public static function isNotification($notification, $strict = true)
117
    {
118
        if (! is_a($notification, notifynder_config()->getNotificationModel())) {
119
            if ($strict) {
120
                throw new InvalidArgumentException('The value passed must be an Notification Model instance');
121
            }
122
123
            return false;
124
        }
125
126
        return true;
127
    }
128
}
129