/*!
* ISC License
*
- * Copyright (c) 2018-2020, Andrea Giammarchi, @WebReflection
+ * Copyright (c) 2018-2021, 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
}
// private helpers
- private static function asString(&$value) {
+ private static function asString($value) {
return $value instanceof FlattedString ? $value->value : $value;
}
$obj = new ReflectionObject($value);
$props = $obj->getProperties();
$keys = array();
- foreach ($props as $prop) {
+ foreach ($props as $prop)
$keys[] = $prop->getName();
- }
return $keys;
}
private static function loop($obj, $keys, &$input, &$set, &$output) {
foreach ($keys as $key) {
$value = $obj ? $output->$key : $output[$key];
- if ($value instanceof FlattedString) {
+ if ($value instanceof FlattedString)
Flatted::ref($obj, $key, $input[$value->value], $input, $set, $output);
- }
}
return $output;
}
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::index($known, $input, $value);
- }
- if (is_array($value)) {
+ if (is_string($value) || is_array($value) || is_object($value)) {
$key = array_search($value, $known->key, true);
- if ($key !== false) {
+ if ($key !== false)
return $known->value[$key];
- }
- return Flatted::index($known, $input, $value);
- }
- if (is_object($value)) {
- $key = array_search($value, $known->key, true);
- if ($key !== false) {
- return $known->value[$key];
- }
return Flatted::index($known, $input, $value);
}
return $value;
private static function transform(&$known, &$input, &$value) {
if (is_array($value)) {
return array_map(
- function (&$value) use(&$known, &$input) {
+ 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) {
+ foreach ($keys as $key)
$object->$key = Flatted::relate($known, $input, $value->$key);
- }
return $object;
}
return $value;
}
- private static function wrap(&$value) {
+ private static function wrap($value) {
if (is_string($value)) {
return new FlattedString($value);
}