Completed
Push — master ( 83b55d...472d84 )
by Justin
03:47 queued 36s
created

Utilities::serializeForRollbar()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
rs 8.4444
c 0
b 0
f 0
cc 8
nc 13
nop 2
1
<?php namespace Rollbar;
2
3
final class Utilities
4
{
5
    public static function isWindows()
6
    {
7
        return php_uname('s') == 'Windows NT';
8
    }
9
10
    public static function validateString(
11
        $input,
12
        $name = "?",
13
        $len = null,
14
        $allowNull = true
15
    ) {
16
        if (is_null($input)) {
17
            if (!$allowNull) {
18
                throw new \InvalidArgumentException("\$$name must not be null");
19
            }
20
            return;
21
        }
22
23
        if (!is_string($input)) {
24
            throw new \InvalidArgumentException("\$$name must be a string");
25
        }
26
        if (!is_null($len) && strlen($input) != $len) {
27
            throw new \InvalidArgumentException("\$$name must be $len characters long, was '$input'");
28
        }
29
    }
30
31
    public static function validateBoolean(
32
        $input,
33
        $name = "?",
34
        $allowNull = true
35
    ) {
36
        if (is_null($input)) {
37
            if (!$allowNull) {
38
                throw new \InvalidArgumentException("\$$name must not be null");
39
            }
40
            return;
41
        }
42
43
        if (!is_bool($input)) {
44
            throw new \InvalidArgumentException("\$$name must be a boolean");
45
        }
46
    }
47
48
    public static function validateInteger(
49
        $input,
50
        $name = "?",
51
        $minValue = null,
52
        $maxValue = null,
53
        $allowNull = true
54
    ) {
55
        if (is_null($input)) {
56
            if (!$allowNull) {
57
                throw new \InvalidArgumentException("\$$name must not be null");
58
            }
59
            return;
60
        }
61
62
        if (!is_integer($input)) {
63
            throw new \InvalidArgumentException("\$$name must be an integer");
64
        }
65
        if (!is_null($minValue) && $input < $minValue) {
66
            throw new \InvalidArgumentException("\$$name must be >= $minValue");
67
        }
68
        if (!is_null($maxValue) && $input > $maxValue) {
69
            throw new \InvalidArgumentException("\$$name must be <= $maxValue");
70
        }
71
    }
72
73
    public static function serializeForRollbar(
74
        $obj,
75
        array $customKeys = null
76
    ) {
77
        $returnVal = array();
78
79
        foreach ($obj as $key => $val) {
80
            if ($val instanceof \Serializable) {
81
                $val = $val->serialize();
82
            } elseif (is_array($val)) {
83
                $val = self::serializeForRollbar($val);
84
            } elseif (is_object($val)) {
85
                $val = array(
86
                    'class' => get_class($val),
87
                    'value' => $val
88
                );
89
            }
90
            
91
            if ($customKeys !== null && in_array($key, $customKeys)) {
92
                $returnVal[$key] = $val;
93
            } elseif (!is_null($val)) {
94
                $returnVal[$key] = $val;
95
            }
96
        }
97
98
        return $returnVal;
99
    }
100
101
    // from http://www.php.net/manual/en/function.uniqid.php#94959
102
    public static function uuid4()
103
    {
104
        mt_srand();
105
        return sprintf(
106
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
107
            // 32 bits for "time_low"
108
            mt_rand(0, 0xffff),
109
            mt_rand(0, 0xffff),
110
            // 16 bits for "time_mid"
111
            mt_rand(0, 0xffff),
112
            // 16 bits for "time_hi_and_version",
113
            // four most significant bits holds version number 4
114
            mt_rand(0, 0x0fff) | 0x4000,
115
            // 16 bits, 8 bits for "clk_seq_hi_res",
116
            // 8 bits for "clk_seq_low",
117
            // two most significant bits holds zero and one for variant DCE1.1
118
            mt_rand(0, 0x3fff) | 0x8000,
119
            // 48 bits for "node"
120
            mt_rand(0, 0xffff),
121
            mt_rand(0, 0xffff),
122
            mt_rand(0, 0xffff)
123
        );
124
    }
125
}
126