Skip to content

Commit

Permalink
CLI-1226: Support objects as API params
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Dec 8, 2023
1 parent cc786c9 commit 5f435c2
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Command/Api/ApiBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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));

Check warning on line 190 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ } $types[] = $type['type']; } - $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); + $isInt = in_array('integer', $types, TRUE) && in_array('int', $types, TRUE); if ($isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); }

Check warning on line 190 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalOrAllSubExprNegation": --- Original +++ New @@ @@ } $types[] = $type['type']; } - $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); + $isInt = !in_array('integer', $types, TRUE) || !in_array('int', $types, TRUE); if ($isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); }

Check warning on line 190 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalOrNegation": --- Original +++ New @@ @@ } $types[] = $type['type']; } - $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); + $isInt = !(in_array('integer', $types, TRUE) || in_array('int', $types, TRUE)); if ($isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); }

Check warning on line 190 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalOrSingleSubExprNegation": --- Original +++ New @@ @@ } $types[] = $type['type']; } - $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); + $isInt = !in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); if ($isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); }

Check warning on line 190 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalOrSingleSubExprNegation": --- Original +++ New @@ @@ } $types[] = $type['type']; } - $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); + $isInt = in_array('integer', $types, TRUE) || !in_array('int', $types, TRUE); if ($isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); }
if ($isInt && ctype_digit($value)) {

Check warning on line 191 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": --- Original +++ New @@ @@ $types[] = $type['type']; } $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); - if ($isInt && ctype_digit($value)) { + if (!$isInt && !ctype_digit($value)) { return $this->doCastParamType('integer', $value); } } elseif ($paramSpec['type'] === 'array') {

Check warning on line 191 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $types[] = $type['type']; } $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); - if ($isInt && ctype_digit($value)) { + if ($isInt || ctype_digit($value)) { return $this->doCastParamType('integer', $value); } } elseif ($paramSpec['type'] === 'array') {

Check warning on line 191 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ $types[] = $type['type']; } $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); - if ($isInt && ctype_digit($value)) { + if (!($isInt && ctype_digit($value))) { return $this->doCastParamType('integer', $value); } } elseif ($paramSpec['type'] === 'array') {

Check warning on line 191 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $types[] = $type['type']; } $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); - if ($isInt && ctype_digit($value)) { + if (!$isInt && ctype_digit($value)) { return $this->doCastParamType('integer', $value); } } elseif ($paramSpec['type'] === 'array') {

Check warning on line 191 in src/Command/Api/ApiBaseCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $types[] = $type['type']; } $isInt = in_array('integer', $types, TRUE) || in_array('int', $types, TRUE); - if ($isInt && ctype_digit($value)) { + if ($isInt && !ctype_digit($value)) { return $this->doCastParamType('integer', $value); } } elseif ($paramSpec['type'] === 'array') {
return $this->doCastParamType('integer', $value);
}
}
Expand All @@ -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,
};
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5f435c2

Please sign in to comment.