Skip to content

Commit

Permalink
WIP: PHP equivalent for interop
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Jul 4, 2020
1 parent 3138eaa commit 919017f
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
ISC License

Copyright (c) 2018, Andrea Giammarchi, @WebReflection
Copyright (c) 2018-2020, Andrea Giammarchi, @WebReflection

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
178 changes: 178 additions & 0 deletions php/flatted.php
@@ -0,0 +1,178 @@
<?php

/*!
* ISC License
*
* Copyright (c) 2018-2020, Andrea Giammarchi, @WebReflection
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

class FlattedString {
public function __construct($value) {
$this->value = $value;
}
}

class Flatted {

// public utilities
public static function parse($json, $assoc = false, $depth = 512, $options = 0) {
$input = array_map(
'Flatted::asString',
array_map(
'Flatted::wrap',
json_decode($json, $assoc, $depth, $options)
)
);
$value = &$input[0];
$set = array();
if (is_array($value))
return Flatted::revive(false, array_keys($value), $input, $set, $value);
if (is_object($value))
return Flatted::revive(true, Flatted::keys($value), $input, $set, $value);
return $value;
}

public static function stringify($value, $options = 0, $depth = 512) {
$known = new stdClass;
$known->key = array();
$known->value = array();
$input = array();
$output = array();
$i = intval(Flatted::add($known, $input, $value));
while ($i < count($input)) {
$output[$i] = Flatted::transform($known, $input, $input[$i]);
$i++;
}
return json_encode($output, $options, $depth);
}

// private helpers
private static function add(&$known, &$input, &$value) {
$input[] = &$value;
$index = strval(count($input) - 1);
$known->key[] = &$value;
$known->value[] = &$index;
return $index;
}

private static function asString(&$value) {
return $value instanceof FlattedString ? $value->value : $value;
}

private static function keys(&$value) {
$obj = new ReflectionObject($value);
$props = $obj->getProperties();
$keys = array();
foreach ($props as $prop) {
$keys[] = $prop->getName();
}
return $keys;
}

private static function relate(&$known, &$input, &$value) {
if (is_string($value)) {
$key = array_search($value, $known->key, true);
if ($key !== false) {
return $known->value[$key];
}
return Flatted::add($known, $input, $value);
}
if (is_array($value)) {
$key = array_search($value, $known->key, true);
if ($key !== false) {
return $known->value[$key];
}
return Flatted::add($known, $input, $value);
}
if (is_object($value)) {
$key = array_search($value, $known->key, true);
if ($key !== false) {
return $known->value[$key];
}
return Flatted::add($known, $input, $value);
}
return $value;
}

private static function revive($obj, $keys, &$input, &$set, &$output) {
foreach ($keys as $key) {
$value = $obj ? $output->$key : $output[$key];
if ($value instanceof FlattedString) {
$tmp = &$input[$value->value];
if (is_array($tmp) && !in_array($tmp, $set)) {
$set[] = &$tmp;
$replace = Flatted::revive(false, array_keys($tmp), $input, $set, $tmp);
Flatted::set($obj, $output, $key, $replace);
}
elseif (is_object($tmp) && !in_array($tmp, $set)) {
$set[] = &$tmp;
$replace = Flatted::revive(true, Flatted::keys($tmp), $input, $set, $tmp);
Flatted::set($obj, $output, $key, $replace);
}
else {
Flatted::set($obj, $output, $key, $tmp);
}
}
}
return $output;
}

private static function set(&$obj, &$ref, &$key, &$value) {
if ($obj) {
$ref->$key = &$value;
}
else {
$ref[$key] = &$value;
}
}

private static function transform(&$known, &$input, &$value) {
if (is_array($value)) {
return array_map(
function (&$value) use(&$known, &$input) {
return Flatted::relate($known, $input, $value);
},
$value
);
}
if (is_object($value)) {
$object = new stdClass;
$keys = Flatted::keys($value);
foreach ($keys as $key) {
$object->$key = Flatted::relate($known, $input, $value->$key);
}
return $object;
}
return $value;
}

private static function wrap(&$value) {
if (is_string($value)) {
return new FlattedString($value);
}
if (is_array($value)) {
return array_map('Flatted::wrap', $value);
}
if (is_object($value)) {
$keys = Flatted::keys($value);
foreach ($keys as $key) {
$value->$key = self::wrap($value->$key);
}
return $value;
}
return $value;
}
}
?>
35 changes: 35 additions & 0 deletions php/test.php
@@ -0,0 +1,35 @@
<?php

error_reporting(E_ALL | E_STRICT | E_NOTICE);

require_once('flatted.php');

class console {
public static function assert($condition, $message) {
if (!$condition) {
echo $message."\n";
exit(1);
}
}
}

console::assert(Flatted::stringify([null, null]) === '[[null,null]]', 'multiple null failed');

$a = array();
$o = new stdClass;

console::assert(Flatted::stringify($a) === '[[]]', 'empty Array');
console::assert(Flatted::stringify($o) === '[{}]', 'empty Object');

$a[] = &$a;
$o->o = &$o;

console::assert(Flatted::stringify($a) === '[["0"]]', 'recursive Array');
console::assert(Flatted::stringify($o) === '[{"o":"0"}]', 'recursive Object');

$b = Flatted::parse(Flatted::stringify($a));
console::assert(is_array($b) && $b[0] === $b, 'restoring recursive Array');

echo "OK\n";

?>

0 comments on commit 919017f

Please sign in to comment.