.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / flatted / php / flatted.php
1 <?php
2
3 /*!
4  * ISC License
5  * 
6  * Copyright (c) 2018-2020, 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     }
82     return $keys;
83   }
84
85   private static function loop($obj, $keys, &$input, &$set, &$output) {
86     foreach ($keys as $key) {
87       $value = $obj ? $output->$key : $output[$key];
88       if ($value instanceof FlattedString) {
89         Flatted::ref($obj, $key, $input[$value->value], $input, $set, $output);
90       }
91     }
92     return $output;
93   }
94
95   private static function relate(&$known, &$input, &$value) {
96     if (is_string($value)) {
97       $key = array_search($value, $known->key, true);
98       if ($key !== false) {
99         return $known->value[$key];
100       }
101       return Flatted::index($known, $input, $value);
102     }
103     if (is_array($value)) {
104       $key = array_search($value, $known->key, true);
105       if ($key !== false) {
106         return $known->value[$key];
107       }
108       return Flatted::index($known, $input, $value);
109     }
110     if (is_object($value)) {
111       $key = array_search($value, $known->key, true);
112       if ($key !== false) {
113         return $known->value[$key];
114       }
115       return Flatted::index($known, $input, $value);
116     }
117     return $value;
118   }
119
120   private static function ref($obj, &$key, &$value, &$input, &$set, &$output) {
121     if (is_array($value) && !in_array($value, $set, true)) {
122       $set[] = $value;
123       $value = Flatted::loop(false, array_keys($value), $input, $set, $value);
124     }
125     elseif (is_object($value) && !in_array($value, $set, true)) {
126       $set[] = $value;
127       $value = Flatted::loop(true, Flatted::keys($value), $input, $set, $value);
128     }
129     if ($obj) {
130       $output->$key = &$value;
131     }
132     else {
133       $output[$key] = &$value;
134     }
135   }
136
137   private static function transform(&$known, &$input, &$value) {
138     if (is_array($value)) {
139       return array_map(
140         function (&$value) use(&$known, &$input) {
141           return Flatted::relate($known, $input, $value);
142         },
143         $value
144       );
145     }
146     if (is_object($value)) {
147       $object = new stdClass;
148       $keys = Flatted::keys($value);
149       foreach ($keys as $key) {
150         $object->$key = Flatted::relate($known, $input, $value->$key);
151       }
152       return $object;
153     }
154     return $value;
155   }
156
157   private static function wrap(&$value) {
158     if (is_string($value)) {
159       return new FlattedString($value);
160     }
161     if (is_array($value)) {
162       return array_map('Flatted::wrap', $value);
163     }
164     if (is_object($value)) {
165       $keys = Flatted::keys($value);
166       foreach ($keys as $key) {
167         $value->$key = self::wrap($value->$key);
168       }
169       return $value;
170     }
171     return $value;
172   }
173 }
174 ?>