flatted.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*!
  3. * ISC License
  4. *
  5. * Copyright (c) 2018-2021, 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. return $keys;
  74. }
  75. private static function loop($obj, $keys, &$input, &$set, &$output) {
  76. foreach ($keys as $key) {
  77. $value = $obj ? $output->$key : $output[$key];
  78. if ($value instanceof FlattedString)
  79. Flatted::ref($obj, $key, $input[$value->value], $input, $set, $output);
  80. }
  81. return $output;
  82. }
  83. private static function relate(&$known, &$input, &$value) {
  84. if (is_string($value) || is_array($value) || is_object($value)) {
  85. $key = array_search($value, $known->key, true);
  86. if ($key !== false)
  87. return $known->value[$key];
  88. return Flatted::index($known, $input, $value);
  89. }
  90. return $value;
  91. }
  92. private static function ref($obj, &$key, &$value, &$input, &$set, &$output) {
  93. if (is_array($value) && !in_array($value, $set, true)) {
  94. $set[] = $value;
  95. $value = Flatted::loop(false, array_keys($value), $input, $set, $value);
  96. }
  97. elseif (is_object($value) && !in_array($value, $set, true)) {
  98. $set[] = $value;
  99. $value = Flatted::loop(true, Flatted::keys($value), $input, $set, $value);
  100. }
  101. if ($obj) {
  102. $output->$key = &$value;
  103. }
  104. else {
  105. $output[$key] = &$value;
  106. }
  107. }
  108. private static function transform(&$known, &$input, &$value) {
  109. if (is_array($value)) {
  110. return array_map(
  111. function ($value) use(&$known, &$input) {
  112. return Flatted::relate($known, $input, $value);
  113. },
  114. $value
  115. );
  116. }
  117. if (is_object($value)) {
  118. $object = new stdClass;
  119. $keys = Flatted::keys($value);
  120. foreach ($keys as $key)
  121. $object->$key = Flatted::relate($known, $input, $value->$key);
  122. return $object;
  123. }
  124. return $value;
  125. }
  126. private static function wrap($value) {
  127. if (is_string($value)) {
  128. return new FlattedString($value);
  129. }
  130. if (is_array($value)) {
  131. return array_map('Flatted::wrap', $value);
  132. }
  133. if (is_object($value)) {
  134. $keys = Flatted::keys($value);
  135. foreach ($keys as $key) {
  136. $value->$key = self::wrap($value->$key);
  137. }
  138. return $value;
  139. }
  140. return $value;
  141. }
  142. }
  143. ?>