From 30bc68e6e2d642ec1a8258de377d46782825f616 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 15 Jul 2020 16:48:22 +0400 Subject: Added integer parser Signed-off-by: Valentin Popov --- src/Parsers/IntegerParser.php | 36 +++++++++++++++++++++++++++++++++ tests/Parsers/IntegerParserTest.php | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/Parsers/IntegerParser.php create mode 100644 tests/Parsers/IntegerParserTest.php diff --git a/src/Parsers/IntegerParser.php b/src/Parsers/IntegerParser.php new file mode 100644 index 0000000..95df2dd --- /dev/null +++ b/src/Parsers/IntegerParser.php @@ -0,0 +1,36 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Parsers + */ +final class IntegerParser { + /** + * @param mixed $value + * + * @return int|null + */ + public static function get($value): ?int { + return filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); + } +} diff --git a/tests/Parsers/IntegerParserTest.php b/tests/Parsers/IntegerParserTest.php new file mode 100644 index 0000000..7f5cce7 --- /dev/null +++ b/tests/Parsers/IntegerParserTest.php @@ -0,0 +1,40 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package Tests\Parsers + */ +final class IntegerParserTest extends TestCase { + public function testInvalid(): void { + self::assertNull(IntegerParser::get(null)); + } + + public function testValid(): void { + $number = Faker::create()->numberBetween(100, 200); + self::assertEquals($number, IntegerParser::get((string) $number)); + } +} -- cgit v1.2.3