Skip to content

Commit

Permalink
ApiManager: Add support for rewriting types.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored Mar 14, 2020
1 parent e69a528 commit 97d3d86
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private function callActionMethods(BaseEndpoint $endpoint, string $action, strin
$args[$pName] = $params;
} elseif (isset($params[$pName]) === true) {
if ($params[$pName]) {
$args[$pName] = $params[$pName];
$args[$pName] = $this->fixType($params[$pName], (($type = $parameter->getType()) !== null) ? $type : null);
} elseif (($type = $parameter->getType()) !== null) {
$args[$pName] = $this->returnEmptyValue($endpoint, $pName, $type);
}
Expand Down Expand Up @@ -311,4 +311,31 @@ private function getBodyParams(string $method): array
return [];
}

/**
* Rewrite given type to preference type by annotation.
*
* 1. If type is nullable, keep original haystack
* 2. Empty value rewrite to null, if null is supported
*
* @param mixed $haystack
* @param \ReflectionType|null $type
* @return mixed
*/
private function fixType($haystack, ?\ReflectionType $type)
{
if ($type === null) {
return $haystack;
}

if (!$haystack && $type->allowsNull()) {
return null;
}

if ($type->getName() === 'bool') {
return \in_array($haystack, ['1', 'true', 'yes'], true) === true;
}

return $haystack;
}

}

0 comments on commit 97d3d86

Please sign in to comment.