-
Notifications
You must be signed in to change notification settings - Fork 1
/
object-navigation-experiment.php
44 lines (31 loc) · 1.15 KB
/
object-navigation-experiment.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
<?php
$header = 'application/vnd.odd-profile.v456+json';
$versionMatch = preg_match('~' . quotemeta('application/vnd.odd-profile') . '.' . 'v' . '(\d+)' . quotemeta('+json') . '~', $header, $matches);
print_r($matches[1]);
exit(0);
// This is an experiment to see how easy it is to navigate
// through a PHP object, creating objects/arrays along
// the way.
// A node in a document could be addressed with a path, and
// a value written/added at that path.
$path = "top/posts/conversations/conversation-1";
$content = "hello";
$o = new stdClass() ;
$currentPoint = $o;
$elems = explode("/", $path);
$endElem = $elems[count($elems)-1];
$elems = array_slice($elems,0,count($elems)-1);
// Navigate through the object creating parts of the tree that are missing
foreach ($elems as $elem) {
if (isset($currentPoint->$elem)) {
$currentPoint = $currentPoint->$elem;
} else {
$currentPoint->$elem = new stdClass();
$currentPoint = $currentPoint->$elem;
}
}
// At the penultimate node, set the value of the final node to the content
$currentPoint->$endElem = $content;
if ($currentPoint->$endElem)
// print_r($o);
echo json_encode($o);