Skip to content

Commit c7b44df

Browse files
authored
Merge pull request #84 from wollanup/bool-cast
feat(actionStrategy): Auto cast to bool
2 parents e6c3e8f + 7f7c7e4 commit c7b44df

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Slim/Handlers/Strategies/ActionStrategy.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Psr\Http\Message\UploadedFileInterface;
1919
use ReflectionClass;
2020
use ReflectionException;
21+
use ReflectionNamedType;
2122
use ReflectionParameter;
2223
use Slim\Interfaces\InvocationStrategyInterface;
2324

@@ -166,6 +167,13 @@ protected function buildParams(
166167
"Missing or null required parameter '{$name}' in " . $r->getName() . "::" . $m->getName()
167168
);
168169
}
170+
$paramType = $param->getType();
171+
if ($paramType instanceof ReflectionNamedType) {
172+
$paramTypeName = $paramType->getName();
173+
if ($paramTypeName === 'bool') {
174+
$paramValue = $this->toBool($paramValue);
175+
}
176+
}
169177
$buildParams[] = $paramValue;
170178
}
171179
}
@@ -246,4 +254,23 @@ protected function callHandler(
246254

247255
return $response;
248256
}
257+
258+
protected function toBool($value): bool
259+
{
260+
if (is_string($value)) {
261+
$value = strtolower($value);
262+
}
263+
return in_array($value, [
264+
true,
265+
1,
266+
'true',
267+
'1',
268+
'on',
269+
'checked',
270+
'selected',
271+
'active',
272+
'yes',
273+
'oui',
274+
], true);
275+
}
249276
}

0 commit comments

Comments
 (0)