From 9682cb058c468329a3273607d2fcb172fdfd9835 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 15 Jul 2020 16:54:44 +0400 Subject: Added string parser Signed-off-by: Valentin Popov --- src/Parsers/StringParser.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/Parsers/StringParser.php (limited to 'src/Parsers/StringParser.php') diff --git a/src/Parsers/StringParser.php b/src/Parsers/StringParser.php new file mode 100644 index 0000000..1bc7a60 --- /dev/null +++ b/src/Parsers/StringParser.php @@ -0,0 +1,40 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Parsers + */ +final class StringParser { + /** + * @param $value + * + * @return string|null + */ + public static function get($value): ?string { + $result = filter_var($value, FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE); + + return is_string($result) ? trim($result) : null; + } +} -- cgit v1.2.3 From 5062d35a0d3a6463ef03c8f83fe2f93ac9099a62 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 15 Jul 2020 16:59:38 +0400 Subject: Updated parsers Signed-off-by: Valentin Popov --- src/Parsers/IntegerParser.php | 6 +++--- src/Parsers/StringParser.php | 10 +++------- tests/Parsers/IntegerParserTest.php | 4 ---- tests/Parsers/StringParserTest.php | 2 +- 4 files changed, 7 insertions(+), 15 deletions(-) (limited to 'src/Parsers/StringParser.php') diff --git a/src/Parsers/IntegerParser.php b/src/Parsers/IntegerParser.php index 95df2dd..3eb4ea1 100644 --- a/src/Parsers/IntegerParser.php +++ b/src/Parsers/IntegerParser.php @@ -28,9 +28,9 @@ final class IntegerParser { /** * @param mixed $value * - * @return int|null + * @return int */ - public static function get($value): ?int { - return filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); + public static function get($value): int { + return filter_var($value, FILTER_VALIDATE_INT); } } diff --git a/src/Parsers/StringParser.php b/src/Parsers/StringParser.php index 1bc7a60..c22d8f1 100644 --- a/src/Parsers/StringParser.php +++ b/src/Parsers/StringParser.php @@ -19,8 +19,6 @@ declare(strict_types = 1); namespace EPGService\Parsers; -use function is_string; - /** * @copyright Copyright © 2020 “Valentin Popov” * @license http://www.apache.org/licenses/LICENSE-2.0 @@ -30,11 +28,9 @@ final class StringParser { /** * @param $value * - * @return string|null + * @return string */ - public static function get($value): ?string { - $result = filter_var($value, FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE); - - return is_string($result) ? trim($result) : null; + public static function get($value): string { + return filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); } } diff --git a/tests/Parsers/IntegerParserTest.php b/tests/Parsers/IntegerParserTest.php index 7f5cce7..56c13ce 100644 --- a/tests/Parsers/IntegerParserTest.php +++ b/tests/Parsers/IntegerParserTest.php @@ -29,10 +29,6 @@ use PHPUnit\Framework\TestCase; * @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)); diff --git a/tests/Parsers/StringParserTest.php b/tests/Parsers/StringParserTest.php index 518defb..5c1a82b 100644 --- a/tests/Parsers/StringParserTest.php +++ b/tests/Parsers/StringParserTest.php @@ -29,7 +29,7 @@ use PHPUnit\Framework\TestCase; */ final class StringParserTest extends TestCase { public function testValid(): void { - $string = '

Hello WorldÆØÅ!

'; + $string = '

Hello World!

'; self::assertEquals('Hello World!', StringParser::get($string)); } } -- cgit v1.2.3 From c0d5496035e5ac7ec44a832428a8d651a950903d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 15 Jul 2020 19:13:16 +0400 Subject: Added interface of parser Signed-off-by: Valentin Popov --- src/Parsers/BaseParser.php | 34 ++++++++++++++++++++++++++++++++++ src/Parsers/IntegerParser.php | 2 +- src/Parsers/StringParser.php | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/Parsers/BaseParser.php (limited to 'src/Parsers/StringParser.php') diff --git a/src/Parsers/BaseParser.php b/src/Parsers/BaseParser.php new file mode 100644 index 0000000..d36508d --- /dev/null +++ b/src/Parsers/BaseParser.php @@ -0,0 +1,34 @@ + + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Parsers + */ +interface BaseParser { + /** + * @param mixed $value + * + * @return mixed + */ + public static function get($value); +} diff --git a/src/Parsers/IntegerParser.php b/src/Parsers/IntegerParser.php index 3eb4ea1..ba89315 100644 --- a/src/Parsers/IntegerParser.php +++ b/src/Parsers/IntegerParser.php @@ -24,7 +24,7 @@ namespace EPGService\Parsers; * @license http://www.apache.org/licenses/LICENSE-2.0 * @package EPGService\Parsers */ -final class IntegerParser { +final class IntegerParser implements BaseParser { /** * @param mixed $value * diff --git a/src/Parsers/StringParser.php b/src/Parsers/StringParser.php index c22d8f1..8473106 100644 --- a/src/Parsers/StringParser.php +++ b/src/Parsers/StringParser.php @@ -24,7 +24,7 @@ namespace EPGService\Parsers; * @license http://www.apache.org/licenses/LICENSE-2.0 * @package EPGService\Parsers */ -final class StringParser { +final class StringParser implements BaseParser { /** * @param $value * -- cgit v1.2.3