flatted.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*!
  3. * ISC License
  4. *
  5. * Copyright (c) 2018-2020, Andrea Giammarchi, @WebReflection
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  12. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  13. * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  14. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  16. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. class FlattedString {
  20. public function __construct($value) {
  21. $this->value = $value;
  22. }
  23. }
  24. class Flatted {
  25. // public utilities
  26. public static function parse($json, $assoc = false, $depth = 512, $options = 0) {
  27. $input = array_map(
  28. 'Flatted::asString',
  29. array_map(
  30. 'Flatted::wrap',
  31. json_decode($json, $assoc, $depth, $options)
  32. )
  33. );
  34. $value = &$input[0];
  35. $set = array();
  36. $set[] = &$value;
  37. if (is_array($value))
  38. return Flatted::loop(false, array_keys($value), $input, $set, $value);
  39. if (is_object($value))
  40. return Flatted::loop(true, Flatted::keys($value), $input, $set, $value);
  41. return $value;
  42. }
  43. public static function stringify($value, $options = 0, $depth = 512) {
  44. $known = new stdClass;
  45. $known->key = array();
  46. $known->value = array();
  47. $input = array();
  48. $output = array();
  49. $i = intval(Flatted::index($known, $input, $value));
  50. while ($i < count($input)) {
  51. $output[$i] = Flatted::transform($known, $input, $input[$i]);
  52. $i++;
  53. }
  54. return json_encode($output, $options, $depth);
  55. }
  56. // private helpers
  57. private static function asString(&$value) {
  58. return $value instanceof FlattedString ? $value->value : $value;
  59. }
  60. private static function index(&$known, &$input, &$value) {
  61. $input[] = &$value;
  62. $index = strval(count($input) - 1);
  63. $known->key[] = &$value;
  64. $known->value[] = &$index;
  65. return $index;
  66. }
  67. private static function keys(&$value) {
  68. $obj = new ReflectionObject($value);
  69. $props = $obj->getProperties();
  70. $keys = array();
  71. foreach ($props as $prop) {
  72. $keys[] = $prop->getName();
  73. }
  74. return $keys;
  75. }
  76. private static function loop($obj, $keys, &$input, &$set, &$output) {
  77. foreach ($keys as $key) {
  78. $value = $obj ? $output->$key : $output[$key];
  79. if ($value instanceof FlattedString) {
  80. Flatted::ref($obj, $key, $input[$value->value], $input, $set, $output);
  81. }
  82. }
  83. return $output;
  84. }
  85. private static function relate(&$known, &$input, &$value) {
  86. if (is_string($value)) {
  87. $key = array_search($value, $known->key, true);
  88. if ($key !== false) {
  89. return $known->value[$key];
  90. }
  91. return Flatted::index($known, $input, $value);
  92. }
  93. if (is_array($value)) {
  94. $key = array_search($value, $known->key, true);
  95. if ($key !== false) {
  96. return $known->value[$key];
  97. }
  98. return Flatted::index($known, $input, $value);
  99. }
  100. if (is_object($value)) {
  101. $key = array_search($value, $known->key, true);
  102. if ($key !== false) {
  103. return $known->value[$key];
  104. }
  105. return Flatted::index($known, $input, $value);
  106. }
  107. return $value;
  108. }
  109. private static function ref($obj, &$key, &$value, &$input, &$set, &$output) {
  110. if (is_array($value) && !in_array($value, $set, true)) {
  111. $set[] = $value;
  112. $value = Flatted::loop(false, array_keys($value), $input, $set, $value);
  113. }
  114. elseif (is_object($value) && !in_array($value, $set, true)) {
  115. $set[] = $value;
  116. $value = Flatted::loop(true, Flatted::keys($value), $input, $set, $value);
  117. }
  118. if ($obj) {
  119. $output->$key = &$value;
  120. }
  121. else {
  122. $output[$key] = &$value;
  123. }
  124. }
  125. private static function transform(&$known, &$input, &$value) {
  126. if (is_array($value)) {
  127. return array_map(
  128. function (&$value) use(&$known, &$input) {
  129. return Flatted::relate($known, $input, $value);
  130. },
  131. $value
  132. );
  133. }
  134. if (is_object($value)) {
  135. $object = new stdClass;
  136. $keys = Flatted::keys($value);
  137. foreach ($keys as $key) {
  138. $object->$key = Flatted::relate($known, $input, $value->$key);
  139. }
  140. return $object;
  141. }
  142. return $value;
  143. }
  144. private static function wrap(&$value) {
  145. if (is_string($value)) {
  146. return new FlattedString($value);
  147. }
  148. if (is_array($value)) {
  149. return array_map('Flatted::wrap', $value);
  150. }
  151. if (is_object($value)) {
  152. $keys = Flatted::keys($value);
  153. foreach ($keys as $key) {
  154. $value->$key = self::wrap($value->$key);
  155. }
  156. return $value;
  157. }
  158. return $value;
  159. }
  160. }
  161. ?>