Skip to content

Commit

Permalink
management of array with object
Browse files Browse the repository at this point in the history
  • Loading branch information
JoMessina committed Oct 19, 2023
1 parent de6bacc commit d989595
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/PrepareRejectBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,50 @@ public function __construct($name)
private function compile(string $input, int $depth = 2): string
{
return <<<PHP
(function(){
(function() use ($input){
\$serializer = function(\$input, int \$depth = 2) use (&\$serializer) {
if (\$depth === 0 || !is_object(\$input)) {
if (\$depth === 0 || !is_array(\$input) && !is_object(\$input)) {
return json_encode(\$input);
}
\$object = new ReflectionObject(\$input);
\$output = [];
foreach (\$object->getProperties() as \$property) {
\$value = \$property->getValue(\$input);
\$output[] = \$serializer(\$value, \$depth - 1);
if (is_object(\$input)) {
\$object = new ReflectionObject(\$input);
foreach (\$object->getProperties() as \$property) {
\$value = \$property->getValue(\$input);
\$output[\$property->getName()] = \$serializer(\$value, \$depth - 1);
}
}
if (is_array(\$input)) {
foreach (\$input as \$key => \$value) {
\$output[\$key] = \$serializer(\$value, \$depth - 1);
}
}
return \$output;
};
return \$serializer($input, $depth)
return \$serializer(\$input, $depth);
})()
PHP;
}

private function evaluate(array $context, mixed $input, int $depth = 2): array
{
$serializer = function($input, int $depth = 2) use (&$serializer) {
if ($depth === 0 || !is_object($input)) {
$serializer = function ($input, int $depth = 2) use(&$serializer) {
if ($depth === 0 || !is_array($input) && !is_object($input)) {
return json_encode($input);
}

$output = [];
$object = new ReflectionObject($input);

foreach ($object->getProperties() as $property) {
$value = $property->getValue($input);
$output[] = $serializer($value, $depth - 1);
if (is_object($input)) {
$object = new ReflectionObject($input);
foreach ($object->getProperties() as $property) {
$value = $property->getValue($input);
$output[$property->getName()] = $serializer($value, $depth - 1);
}
}
if (is_array($input)) {
foreach ($input as $key => $value) {
$output[$key] = $serializer($value, $depth - 1);
}
}

return $output;
};

Expand Down

0 comments on commit d989595

Please sign in to comment.