-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathUtilities.php
More file actions
234 lines (204 loc) · 6.65 KB
/
Utilities.php
File metadata and controls
234 lines (204 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php declare(strict_types=1);
namespace Rollbar;
final class Utilities
{
private static $ObjectHashes;
public static function isWindows()
{
return php_uname('s') == 'Windows NT';
}
public static function validateString(
$input,
$name = "?",
$len = null,
$allowNull = true
) {
if (is_null($input)) {
if (!$allowNull) {
throw new \InvalidArgumentException("\$$name must not be null");
}
return;
}
if (!is_string($input)) {
throw new \InvalidArgumentException("\$$name must be a string");
}
if (!is_null($len) && strlen($input) != $len) {
throw new \InvalidArgumentException("\$$name must be $len characters long, was '$input'");
}
}
public static function validateBoolean(
$input,
$name = "?",
$allowNull = true
) {
if (is_null($input)) {
if (!$allowNull) {
throw new \InvalidArgumentException("\$$name must not be null");
}
return;
}
if (!is_bool($input)) {
throw new \InvalidArgumentException("\$$name must be a boolean");
}
}
public static function validateInteger(
$input,
$name = "?",
$minValue = null,
$maxValue = null,
$allowNull = true
) {
if (is_null($input)) {
if (!$allowNull) {
throw new \InvalidArgumentException("\$$name must not be null");
}
return;
}
if (!is_int($input)) {
throw new \InvalidArgumentException("\$$name must be an integer");
}
if (!is_null($minValue) && $input < $minValue) {
throw new \InvalidArgumentException("\$$name must be >= $minValue");
}
if (!is_null($maxValue) && $input > $maxValue) {
throw new \InvalidArgumentException("\$$name must be <= $maxValue");
}
}
/**
* Serialize all, or the given keys, from the given object and store it
* in this class's internal store (see self::$ObjectHashes).
*/
public static function serializeForRollbarInternal($obj, ?array $customKeys = null)
{
return self::serializeForRollbar($obj, $customKeys, self::$ObjectHashes);
}
public static function serializeForRollbar(
$obj,
?array $customKeys = null,
&$objectHashes = array(),
$maxDepth = -1,
$depth = 0
) {
$returnVal = array();
if (is_object($obj)) {
if (self::serializedAlready($obj, $objectHashes)) {
return self::circularReferenceLabel($obj);
} else {
self::markSerialized($obj, $objectHashes);
}
}
if ($maxDepth > 0 && $depth > $maxDepth) {
return null;
}
foreach ($obj as $key => $val) {
try {
if (is_object($val)) {
$val = self::serializeObject(
$val,
$customKeys,
$objectHashes,
$maxDepth,
$depth
);
} elseif (is_array($val)) {
$val = self::serializeForRollbar(
$val,
$customKeys,
$objectHashes,
$maxDepth,
$depth+1
);
}
} catch (\Throwable $e) {
$val = 'Error during serialization: '.$e->getMessage();
}
if ($customKeys !== null && in_array($key, $customKeys)) {
$returnVal[$key] = $val;
} elseif (!is_null($val)) {
$returnVal[$key] = $val;
}
}
return $returnVal;
}
private static function serializeObject(
$obj,
?array $customKeys = null,
&$objectHashes = array(),
$maxDepth = -1,
$depth = 0
) {
if (self::serializedAlready($obj, $objectHashes)) {
return self::circularReferenceLabel($obj);
}
// Internal Rollbar classes.
if ($obj instanceof SerializerInterface) {
self::markSerialized($obj, $objectHashes);
return $obj->serialize();
}
// All other classes.
if ($obj instanceof \Serializable) {
self::markSerialized($obj, $objectHashes);
if (method_exists($obj, '__serialize')) {
return $obj->__serialize();
}
return $obj->serialize();
}
$serialized = array(
'class' => get_class($obj)
);
// Don't serialize Iterators as rewinding them does not guarantee the
// previous state.
if ($obj instanceof \Iterator) {
$serialized['value'] = 'non-serializable';
return $serialized;
}
$serialized['value'] = self::serializeForRollbar(
$obj,
$customKeys,
$objectHashes,
$maxDepth,
$depth+1
);
return $serialized;
}
private static function serializedAlready($obj, &$objectHashes)
{
if (!isset($objectHashes[spl_object_hash($obj)])) {
return false;
}
return true;
}
private static function markSerialized($obj, &$objectHashes)
{
$objectHashes[spl_object_hash($obj)] = true;
self::$ObjectHashes = $objectHashes;
}
private static function circularReferenceLabel($obj)
{
return '<CircularReference type:('.get_class($obj).') ref:('.spl_object_hash($obj).')>';
}
// from http://www.php.net/manual/en/function.uniqid.php#94959
public static function uuid4()
{
mt_srand();
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
}