-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strictly check request parameters data types
- Loading branch information
Showing
16 changed files
with
439 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Apitte\Core\Mapping\Parameter; | ||
|
||
use Apitte\Core\Exception\Logical\InvalidArgumentException; | ||
use Apitte\Core\Schema\EndpointParameter; | ||
|
||
class BooleanTypeMapper implements ITypeMapper | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function normalize($value, EndpointParameter $parameter): ?bool | ||
{ | ||
if ($value === null || $value === '') { | ||
return null; | ||
} | ||
|
||
if (is_bool($value)) { | ||
return $value; | ||
} | ||
|
||
if ($value === '1' || $value === 'true') { | ||
return true; | ||
} | ||
|
||
if ($value === '0' || $value === 'false') { | ||
return false; | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf( | ||
'Parameter "%s" should be of type boolean. Pass "true" or "1" for true and "false" or "0" for false.', | ||
$parameter->getName() | ||
)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Apitte\Core\Mapping\Parameter; | ||
|
||
use Apitte\Core\Exception\Logical\InvalidArgumentException; | ||
use Apitte\Core\Schema\EndpointParameter; | ||
use DateTimeImmutable; | ||
use TypeError; | ||
|
||
class DateTimeTypeMapper implements ITypeMapper | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function normalize($value, EndpointParameter $parameter): ?DateTimeImmutable | ||
{ | ||
if ($value === null || $value === '') { | ||
return null; | ||
} | ||
|
||
try { | ||
$value = DateTimeImmutable::createFromFormat(DATE_ATOM, $value); | ||
} catch (TypeError $e) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Parameter "%s" should be of type datetime in format ISO 8601 (Y-m-d\TH:i:sP).', | ||
$parameter->getName() | ||
)); | ||
} | ||
|
||
if ($value !== false) { | ||
return $value; | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf( | ||
'Parameter "%s" should be of type datetime in format ISO 8601 (Y-m-d\TH:i:sP).', | ||
$parameter->getName() | ||
)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Apitte\Core\Mapping\Parameter; | ||
|
||
use Apitte\Core\Schema\EndpointParameter; | ||
|
||
class ScalarTypeMapper implements ITypeMapper | ||
{ | ||
|
||
/** | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function normalize($value, EndpointParameter $parameter) | ||
{ | ||
if ($value === null || $value === '') { | ||
return null; | ||
} | ||
|
||
// boolean | ||
if (is_bool($value)) { | ||
return $value; | ||
} | ||
|
||
if ($value === '1' || $value === 'true') { | ||
return true; | ||
} | ||
|
||
if ($value === '0' || $value === 'false') { | ||
return false; | ||
} | ||
|
||
// int | ||
if (is_int($value) || (is_string($value) && preg_match('#^-?[0-9]+\z#', $value))) { | ||
return (int) $value; | ||
} | ||
|
||
// float | ||
if (is_float($value) || (is_string($value) && preg_match('#^-?[0-9]*[.]?[0-9]+\z#', $value))) { | ||
return (float) $value; | ||
} | ||
|
||
return (string) $value; | ||
} | ||
|
||
} |
Oops, something went wrong.