This source file includes following definitions.
- __construct
- parse
- stringify
- asString
- index
- keys
- loop
- relate
- ref
- transform
- wrap
1 <?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 class FlattedString {
22 public function __construct($value) {
23 $this->value = $value;
24 }
25 }
26
27 class Flatted {
28
29
30 public static function parse($json, $assoc = false, $depth = 512, $options = 0) {
31 $input = array_map(
32 'Flatted::asString',
33 array_map(
34 'Flatted::wrap',
35 json_decode($json, $assoc, $depth, $options)
36 )
37 );
38 $value = &$input[0];
39 $set = array();
40 $set[] = &$value;
41 if (is_array($value))
42 return Flatted::loop(false, array_keys($value), $input, $set, $value);
43 if (is_object($value))
44 return Flatted::loop(true, Flatted::keys($value), $input, $set, $value);
45 return $value;
46 }
47
48 public static function stringify($value, $options = 0, $depth = 512) {
49 $known = new stdClass;
50 $known->key = array();
51 $known->value = array();
52 $input = array();
53 $output = array();
54 $i = intval(Flatted::index($known, $input, $value));
55 while ($i < count($input)) {
56 $output[$i] = Flatted::transform($known, $input, $input[$i]);
57 $i++;
58 }
59 return json_encode($output, $options, $depth);
60 }
61
62
63 private static function asString($value) {
64 return $value instanceof FlattedString ? $value->value : $value;
65 }
66
67 private static function index(&$known, &$input, &$value) {
68 $input[] = &$value;
69 $index = strval(count($input) - 1);
70 $known->key[] = &$value;
71 $known->value[] = &$index;
72 return $index;
73 }
74
75 private static function keys(&$value) {
76 $obj = new ReflectionObject($value);
77 $props = $obj->getProperties();
78 $keys = array();
79 foreach ($props as $prop)
80 $keys[] = $prop->getName();
81 return $keys;
82 }
83
84 private static function loop($obj, $keys, &$input, &$set, &$output) {
85 foreach ($keys as $key) {
86 $value = $obj ? $output->$key : $output[$key];
87 if ($value instanceof FlattedString)
88 Flatted::ref($obj, $key, $input[$value->value], $input, $set, $output);
89 }
90 return $output;
91 }
92
93 private static function relate(&$known, &$input, &$value) {
94 if (is_string($value) || is_array($value) || is_object($value)) {
95 $key = array_search($value, $known->key, true);
96 if ($key !== false)
97 return $known->value[$key];
98 return Flatted::index($known, $input, $value);
99 }
100 return $value;
101 }
102
103 private static function ref($obj, &$key, &$value, &$input, &$set, &$output) {
104 if (is_array($value) && !in_array($value, $set, true)) {
105 $set[] = $value;
106 $value = Flatted::loop(false, array_keys($value), $input, $set, $value);
107 }
108 elseif (is_object($value) && !in_array($value, $set, true)) {
109 $set[] = $value;
110 $value = Flatted::loop(true, Flatted::keys($value), $input, $set, $value);
111 }
112 if ($obj) {
113 $output->$key = &$value;
114 }
115 else {
116 $output[$key] = &$value;
117 }
118 }
119
120 private static function transform(&$known, &$input, &$value) {
121 if (is_array($value)) {
122 return array_map(
123 function ($value) use(&$known, &$input) {
124 return Flatted::relate($known, $input, $value);
125 },
126 $value
127 );
128 }
129 if (is_object($value)) {
130 $object = new stdClass;
131 $keys = Flatted::keys($value);
132 foreach ($keys as $key)
133 $object->$key = Flatted::relate($known, $input, $value->$key);
134 return $object;
135 }
136 return $value;
137 }
138
139 private static function wrap($value) {
140 if (is_string($value)) {
141 return new FlattedString($value);
142 }
143 if (is_array($value)) {
144 return array_map('Flatted::wrap', $value);
145 }
146 if (is_object($value)) {
147 $keys = Flatted::keys($value);
148 foreach ($keys as $key) {
149 $value->$key = self::wrap($value->$key);
150 }
151 return $value;
152 }
153 return $value;
154 }
155 }