From 5f435c2e53481acd7c686b35069a15bd94a814f6 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Fri, 8 Dec 2023 09:50:02 -0800 Subject: [PATCH] CLI-1226: Support objects as API params --- src/Command/Api/ApiBaseCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Command/Api/ApiBaseCommand.php b/src/Command/Api/ApiBaseCommand.php index b1c96921d..9a2dc1124 100644 --- a/src/Command/Api/ApiBaseCommand.php +++ b/src/Command/Api/ApiBaseCommand.php @@ -177,7 +177,7 @@ private function getParamFromInput(InputInterface $input, string $paramName): ar return NULL; } - private function castParamType(array $paramSpec, array|string|bool|int $value): array|bool|int|string { + private function castParamType(array $paramSpec, array|string|bool|int $value): array|bool|int|string|object { $oneOf = $this->getParamTypeOneOf($paramSpec); if (isset($oneOf)) { $types = []; @@ -187,8 +187,8 @@ private function castParamType(array $paramSpec, array|string|bool|int $value): } $types[] = $type['type']; } - if ((in_array('integer', $types, TRUE) || in_array('int', $types, TRUE)) - && ctype_digit($value)) { + $isInt = (in_array('integer', $types, TRUE) || in_array('int', $types, TRUE)); + if ($isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); } } @@ -208,12 +208,13 @@ private function castParamType(array $paramSpec, array|string|bool|int $value): return $this->doCastParamType($type, $value); } - private function doCastParamType(string $type, mixed $value): array|bool|int|string { + private function doCastParamType(string $type, mixed $value): array|bool|int|string|object { return match ($type) { 'int', 'integer' => (int) $value, 'bool', 'boolean' => $this->castBool($value), 'array' => is_string($value) ? explode(',', $value) : (array) $value, 'string' => (string) $value, + 'object' => json_decode($value, FALSE, 512, JSON_THROW_ON_ERROR), 'mixed' => $value, }; } @@ -401,7 +402,7 @@ private function getParamTypeOneOf(array $paramSpec): ?array { return $oneOf; } - private function castParamToArray(mixed $paramSpec, array|string $originalValue): string|array|bool|int { + private function castParamToArray(array $paramSpec, array|string $originalValue): string|array|bool|int { if (array_key_exists('items', $paramSpec) && array_key_exists('type', $paramSpec['items'])) { if (!is_array($originalValue)) { $originalValue = $this->doCastParamType('array', $originalValue);