Skip to content

Commit

Permalink
Update (#134)
Browse files Browse the repository at this point in the history
* PHP Linting (Pint)

* Check data before merging  (#91)

* try to map parameters to their types

* Update RequestValidator.php

* check data before casting

* Refactoring

* style ci

Co-authored-by: Adam Campbell <[email protected]>
Co-authored-by: Bastien Philippe <[email protected]>
Co-authored-by: Lars <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent 894cedd commit 9b92968
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 0 additions & 2 deletions _ide_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/** @noinspection PhpUnusedAliasInspection */

namespace Illuminate\Testing {

/**
* @see \Spectator\Assertions
*
Expand All @@ -25,7 +24,6 @@ class TestResponse
}

namespace Illuminate\Foundation\Testing {

/**
* @see \Spectator\Assertions
*
Expand Down
4 changes: 4 additions & 0 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
24 changes: 24 additions & 0 deletions src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9b92968

Please sign in to comment.