diff --git a/src/PrepareRejectBucket.php b/src/PrepareRejectBucket.php index ff7c25c..2246733 100644 --- a/src/PrepareRejectBucket.php +++ b/src/PrepareRejectBucket.php @@ -21,39 +21,50 @@ public function __construct($name) private function compile(string $input, int $depth = 2): string { return <<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; };