root/frontend/node_modules/flatted/php/flatted.php

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __construct
  2. parse
  3. stringify
  4. asString
  5. index
  6. keys
  7. loop
  8. relate
  9. ref
  10. transform
  11. wrap

   1 <?php
   2
   3 /*!
   4  * ISC License
   5  * 
   6  * Copyright (c) 2018-2021, Andrea Giammarchi, @WebReflection
   7  *
   8  * Permission to use, copy, modify, and/or distribute this software for any
   9  * purpose with or without fee is hereby granted, provided that the above
  10  * copyright notice and this permission notice appear in all copies.
  11  *
  12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  13  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  14  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  15  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  16  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  17  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  18  * PERFORMANCE OF THIS SOFTWARE.
  19  */
  20
  21 class FlattedString {
  22   public function __construct($value) {
  23     $this->value = $value;
  24   }
  25 }
  26
  27 class Flatted {
  28
  29   // public utilities
  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   // private helpers
  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 }

/* [<][>][^][v][top][bottom][index][help] */