-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest-utils.php
89 lines (83 loc) · 1.65 KB
/
test-utils.php
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
<?php
function recursiveEqual($a, $b)
{
if (is_object($a)) {
if (!is_object($b)) {
return FALSE;
}
foreach ($a as $key => $value) {
if (!isset($b->$key)) {
return FALSE;
}
if (!recursiveEqual($value, $b->$key)) {
return FALSE;
}
}
foreach ($b as $key => $value) {
if (!isset($a->$key)) {
return FALSE;
}
}
return TRUE;
}
if (is_array($a)) {
if (!is_array($b)) {
return FALSE;
}
foreach ($a as $key => $value) {
if (!isset($b[$key])) {
return FALSE;
}
if (!recursiveEqual($value, $b[$key])) {
return FALSE;
}
}
foreach ($b as $key => $value) {
if (!isset($a[$key])) {
return FALSE;
}
}
return TRUE;
}
return $a === $b;
}
function pointerGet(&$value, $path = "", $strict = FALSE)
{
if ($path == "") {
return $value;
} else if ($path[0] != "/") {
throw new Exception("Invalid path: $path");
}
$parts = explode("/", $path);
array_shift($parts);
foreach ($parts as $part) {
$part = str_replace("~1", "/", $part);
$part = str_replace("~0", "~", $part);
if (is_array($value) && is_numeric($part)) {
$value = & $value[$part];
} else if (is_object($value)) {
if (isset($value->$part)) {
$value = & $value->$part;
} else if ($strict) {
throw new Exception("Path does not exist: $path");
} else {
return NULL;
}
} else if ($strict) {
throw new Exception("Path does not exist: $path");
} else {
return NULL;
}
}
return $value;
}
function pointerJoin($parts)
{
$result = "";
foreach ($parts as $part) {
$part = str_replace("~", "~0", $part);
$part = str_replace("/", "~1", $part);
$result .= "/" . $part;
}
return $result;
}