diff --git a/_ide_helpers.php b/_ide_helpers.php index 9f39f17..7748f20 100644 --- a/_ide_helpers.php +++ b/_ide_helpers.php @@ -5,7 +5,6 @@ /** @noinspection PhpUnusedAliasInspection */ namespace Illuminate\Testing { - /** * @see \Spectator\Assertions * @@ -25,7 +24,6 @@ class TestResponse } namespace Illuminate\Foundation\Testing { - /** * @see \Spectator\Assertions * diff --git a/src/Support/Format.php b/src/Support/Format.php index 54f9df0..749d538 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -7,9 +7,13 @@ class Format // https://joshtronic.com/2013/09/02/how-to-use-colors-in-command-line-output/ const TEXT_GREEN = '0;32'; + const TEXT_RED = '0;31'; + const TEXT_WHITE = '1;37'; + const TEXT_LIGHT_GREY = '0;37'; + const TEXT_DARK_GREY = '1;30'; const STYLE_ITALIC = '3'; diff --git a/src/Validation/RequestValidator.php b/src/Validation/RequestValidator.php index 719cea6..239e04d 100644 --- a/src/Validation/RequestValidator.php +++ b/src/Validation/RequestValidator.php @@ -149,6 +149,30 @@ protected function validateParameters() } } + /** + * @param mixed $parameter + * @param string|null $type + * @return mixed + */ + private function castParameter($parameter, ?string $type) + { + if ($type === null) { + return $parameter; + } + + if ($type === 'integer' && filter_var($parameter, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE) !== null) { + return (int) $parameter; + } elseif ($type === 'number' && filter_var($parameter, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) !== null) { + return (float) $parameter; + } elseif ($type === 'boolean' && filter_var($parameter, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) { + return filter_var($parameter, FILTER_VALIDATE_BOOLEAN); + } elseif ($type === 'string') { + return (string) $parameter; + } + + return $parameter; + } + /** * @throws RequestValidationException|SchemaValidationException */ diff --git a/tests/RequestValidatorTest.php b/tests/RequestValidatorTest.php index 3b074af..9ca98f9 100644 --- a/tests/RequestValidatorTest.php +++ b/tests/RequestValidatorTest.php @@ -530,6 +530,9 @@ public function test_handles_query_parameters_int(): void return []; })->middleware(Middleware::class); + $this->getJson('/users-by-id/foo') + ->assertInvalidRequest(); + $this->getJson('/users-by-id/1') ->assertValidRequest(); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0943691..047c35d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,9 +8,13 @@ abstract class TestCase extends Orchestra { const NULLABLE_MISSING = 0; + const NULLABLE_EMPTY_STRING = 1; + const NULLABLE_VALID = 2; + const NULLABLE_INVALID = 3; + const NULLABLE_NULL = 4; protected function getEnvironmentSetUp($app)