From 6a8397af078020c5139a09dafcefe550a58556e2 Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Tue, 7 Feb 2017 15:04:03 +0100 Subject: [PATCH 1/7] added trim() to string parser --- src/StringParser.php | 14 +++++++++ src/TrimParser.php | 62 ++++++++++++++++++++++++++++++++++++++++ src/TrimType.php | 10 +++++++ tests/ParserSpecTest.php | 5 ++-- tests/TypeSpecTest.php | 7 +++++ 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/TrimParser.php create mode 100644 src/TrimType.php diff --git a/src/StringParser.php b/src/StringParser.php index 7661a2a..2a40dcc 100644 --- a/src/StringParser.php +++ b/src/StringParser.php @@ -53,4 +53,18 @@ public function email() { return new EmailParser($this->config, $this->name, $this->value); } + + public function trim() + { + return new TrimParser($this->config, $this->name, $this->value, TrimType::TRIM); + } + + public function leftTrim() + { + return new TrimParser($this->config, $this->name, $this->value, TrimType::LEFT_TRIM); + } + public function rightTrim() + { + return new TrimParser($this->config, $this->name, $this->value, TrimType::RIGHT_TRIM); + } } diff --git a/src/TrimParser.php b/src/TrimParser.php new file mode 100644 index 0000000..06d955c --- /dev/null +++ b/src/TrimParser.php @@ -0,0 +1,62 @@ +trimType = $trimType; + } + + protected function describe() + { + return "a valid email address"; + } + + protected function parse($value) + { + if ($this->trimType === TrimType::TRIM) { + return trim((string) $value); + } elseif ($this->trimType === TrimType::LEFT_TRIM) { + return ltrim((string) $value); + } elseif ($this->trimType === TrimType::RIGHT_TRIM) { + return rtrim((string) $value); + } + return (string) $value; + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsTo($defaultValue) + { + return parent::defaultsTo($defaultValue); + } + + /** + * @throws \Exception + * @return string + */ + public function required($invalidValueMessage = null, $notFoundMessage = null) + { + return parent::required($invalidValueMessage, $notFoundMessage); + } + + /** + * @param string $defaultValue + * @return string + */ + public function defaultsToIfEmpty($defaultValue) + { + if ($this->value === '') { + return $defaultValue; + } + + return $this->defaultsTo($defaultValue); + } +} diff --git a/src/TrimType.php b/src/TrimType.php new file mode 100644 index 0000000..f86cd08 --- /dev/null +++ b/src/TrimType.php @@ -0,0 +1,10 @@ + true], ['value' => false]] diff --git a/tests/TypeSpecTest.php b/tests/TypeSpecTest.php index 73af073..4bfc045 100644 --- a/tests/TypeSpecTest.php +++ b/tests/TypeSpecTest.php @@ -16,6 +16,7 @@ use MPScholten\RequestParser\ExceptionFactory; use MPScholten\RequestParser\IntParser; use MPScholten\RequestParser\FloatParser; +use MPScholten\RequestParser\TrimParser; use MPScholten\RequestParser\UrlParser; use MPScholten\RequestParser\YesNoBooleanParser; use MPScholten\RequestParser\BooleanParser; @@ -57,6 +58,12 @@ public function testEmail() $this->assertInstanceOf(EmailParser::class, $spec->string()->email()); } + public function testTrim() + { + $spec = new TypeParser(new Config(), 'emailAddress', ' john@doe.com '); + $this->assertInstanceOf(TrimParser::class, $spec->string()->trim()); + } + public function testOneOf() { $spec = new TypeParser(new Config(), 'type', 'b'); From 3d48d5b5ea49638e04d3672a0663ef1e6eb0da67 Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Tue, 7 Feb 2017 15:08:14 +0100 Subject: [PATCH 2/7] test --- tests/ParserSpecTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index 471689d..92bc78c 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -47,7 +47,7 @@ public function specWithValueAndDefaultValue() [new StringParser(new Config(), 'name', 'quintly'), '', 'quintly'], [new UrlParser(new Config(), 'referrer', 'https://www.quintly.com/'), 'https://www.quintly.com/blog/', 'https://www.quintly.com/'], [new EmailParser(new Config(), 'emailAddress', 'john@doe.com'), '', 'john@doe.com'], - [new TrimParser(new Config(), 'emailAddress', 'john@doe.com', TrimType::TRIM), '', ' john@doe.com '], + [new TrimParser(new Config(), 'emailAddress', ' john@doe.com ', TrimType::TRIM), '', 'john@doe.com'], [new OneOfParser(new Config(), 'type', 'a', ['a', 'b']), 'b', 'a'], [new DateTimeParser(new Config(), 'createdAt', '2015-02-02'), new \DateTime('2015-01-01'), new \DateTime('2015-02-02')], [new JsonParser(new Config(), 'config', '{"value":false}'), ['value' => true], ['value' => false]] From 5f03c0fc714ab76e0085754c3a660acfe9baabde Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Tue, 7 Feb 2017 15:13:34 +0100 Subject: [PATCH 3/7] fix parser --- src/TrimParser.php | 2 +- tests/ParserSpecTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TrimParser.php b/src/TrimParser.php index 06d955c..e001904 100644 --- a/src/TrimParser.php +++ b/src/TrimParser.php @@ -8,8 +8,8 @@ class TrimParser extends AbstractValueParser public function __construct(Config $config, $name, $value, $trimType) { - parent::__construct($config, $name, $value); $this->trimType = $trimType; + parent::__construct($config, $name, $value); } protected function describe() diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index 92bc78c..471689d 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -47,7 +47,7 @@ public function specWithValueAndDefaultValue() [new StringParser(new Config(), 'name', 'quintly'), '', 'quintly'], [new UrlParser(new Config(), 'referrer', 'https://www.quintly.com/'), 'https://www.quintly.com/blog/', 'https://www.quintly.com/'], [new EmailParser(new Config(), 'emailAddress', 'john@doe.com'), '', 'john@doe.com'], - [new TrimParser(new Config(), 'emailAddress', ' john@doe.com ', TrimType::TRIM), '', 'john@doe.com'], + [new TrimParser(new Config(), 'emailAddress', 'john@doe.com', TrimType::TRIM), '', ' john@doe.com '], [new OneOfParser(new Config(), 'type', 'a', ['a', 'b']), 'b', 'a'], [new DateTimeParser(new Config(), 'createdAt', '2015-02-02'), new \DateTime('2015-01-01'), new \DateTime('2015-02-02')], [new JsonParser(new Config(), 'config', '{"value":false}'), ['value' => true], ['value' => false]] From 856c9b9da0dea68e1dd7c331f7f5d87ba790a13d Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Tue, 7 Feb 2017 15:15:30 +0100 Subject: [PATCH 4/7] fix test (stop experimenting) --- tests/ParserSpecTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ParserSpecTest.php b/tests/ParserSpecTest.php index 471689d..92bc78c 100644 --- a/tests/ParserSpecTest.php +++ b/tests/ParserSpecTest.php @@ -47,7 +47,7 @@ public function specWithValueAndDefaultValue() [new StringParser(new Config(), 'name', 'quintly'), '', 'quintly'], [new UrlParser(new Config(), 'referrer', 'https://www.quintly.com/'), 'https://www.quintly.com/blog/', 'https://www.quintly.com/'], [new EmailParser(new Config(), 'emailAddress', 'john@doe.com'), '', 'john@doe.com'], - [new TrimParser(new Config(), 'emailAddress', 'john@doe.com', TrimType::TRIM), '', ' john@doe.com '], + [new TrimParser(new Config(), 'emailAddress', ' john@doe.com ', TrimType::TRIM), '', 'john@doe.com'], [new OneOfParser(new Config(), 'type', 'a', ['a', 'b']), 'b', 'a'], [new DateTimeParser(new Config(), 'createdAt', '2015-02-02'), new \DateTime('2015-01-01'), new \DateTime('2015-02-02')], [new JsonParser(new Config(), 'config', '{"value":false}'), ['value' => true], ['value' => false]] From 67ab5ef24d76bdadf03c633150a207ab79bd9b9f Mon Sep 17 00:00:00 2001 From: Yuval Date: Tue, 7 Feb 2017 17:44:24 +0100 Subject: [PATCH 5/7] Update StringParser.php --- src/StringParser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/StringParser.php b/src/StringParser.php index 2a40dcc..ebb04aa 100644 --- a/src/StringParser.php +++ b/src/StringParser.php @@ -63,6 +63,7 @@ public function leftTrim() { return new TrimParser($this->config, $this->name, $this->value, TrimType::LEFT_TRIM); } + public function rightTrim() { return new TrimParser($this->config, $this->name, $this->value, TrimType::RIGHT_TRIM); From 2d218254c6b9743fd4ea0a78dcd4511d28058206 Mon Sep 17 00:00:00 2001 From: Yuval Date: Wed, 15 Feb 2017 09:04:34 +0100 Subject: [PATCH 6/7] Update TrimParser.php --- src/TrimParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TrimParser.php b/src/TrimParser.php index e001904..7ab16e6 100644 --- a/src/TrimParser.php +++ b/src/TrimParser.php @@ -14,7 +14,7 @@ public function __construct(Config $config, $name, $value, $trimType) protected function describe() { - return "a valid email address"; + return "a text string"; } protected function parse($value) From c5b1e1528779be400664f8f23dba6736f560ac15 Mon Sep 17 00:00:00 2001 From: Yuval Herziger Date: Fri, 17 Feb 2017 10:33:51 +0100 Subject: [PATCH 7/7] move trim type constants to TrimParser, add tests for all trim types, remove TrimType --- src/StringParser.php | 6 +++--- src/TrimParser.php | 10 +++++++--- src/TrimType.php | 10 ---------- tests/ParserSpecTest.php | 5 +++-- tests/TypeSpecTest.php | 12 ++++++++++++ 5 files changed, 25 insertions(+), 18 deletions(-) delete mode 100644 src/TrimType.php diff --git a/src/StringParser.php b/src/StringParser.php index ebb04aa..b2b35bd 100644 --- a/src/StringParser.php +++ b/src/StringParser.php @@ -56,16 +56,16 @@ public function email() public function trim() { - return new TrimParser($this->config, $this->name, $this->value, TrimType::TRIM); + return new TrimParser($this->config, $this->name, $this->value, TrimParser::TRIM); } public function leftTrim() { - return new TrimParser($this->config, $this->name, $this->value, TrimType::LEFT_TRIM); + return new TrimParser($this->config, $this->name, $this->value, TrimParser::LEFT_TRIM); } public function rightTrim() { - return new TrimParser($this->config, $this->name, $this->value, TrimType::RIGHT_TRIM); + return new TrimParser($this->config, $this->name, $this->value, TrimParser::RIGHT_TRIM); } } diff --git a/src/TrimParser.php b/src/TrimParser.php index 7ab16e6..7e8f167 100644 --- a/src/TrimParser.php +++ b/src/TrimParser.php @@ -4,6 +4,10 @@ class TrimParser extends AbstractValueParser { + const TRIM = 'trim'; + const LEFT_TRIM = 'left_trim'; + const RIGHT_TRIM = 'right_trim'; + private $trimType; public function __construct(Config $config, $name, $value, $trimType) @@ -19,11 +23,11 @@ protected function describe() protected function parse($value) { - if ($this->trimType === TrimType::TRIM) { + if ($this->trimType === self::TRIM) { return trim((string) $value); - } elseif ($this->trimType === TrimType::LEFT_TRIM) { + } elseif ($this->trimType === self::LEFT_TRIM) { return ltrim((string) $value); - } elseif ($this->trimType === TrimType::RIGHT_TRIM) { + } elseif ($this->trimType === self::RIGHT_TRIM) { return rtrim((string) $value); } return (string) $value; diff --git a/src/TrimType.php b/src/TrimType.php deleted file mode 100644 index f86cd08..0000000 --- a/src/TrimType.php +++ /dev/null @@ -1,10 +0,0 @@ - true], ['value' => false]] diff --git a/tests/TypeSpecTest.php b/tests/TypeSpecTest.php index 4bfc045..9ec4419 100644 --- a/tests/TypeSpecTest.php +++ b/tests/TypeSpecTest.php @@ -64,6 +64,18 @@ public function testTrim() $this->assertInstanceOf(TrimParser::class, $spec->string()->trim()); } + public function testLeftTrim() + { + $spec = new TypeParser(new Config(), 'emailAddress', ' john@doe.com'); + $this->assertInstanceOf(TrimParser::class, $spec->string()->leftTrim()); + } + + public function testRightTrim() + { + $spec = new TypeParser(new Config(), 'emailAddress', 'john@doe.com '); + $this->assertInstanceOf(TrimParser::class, $spec->string()->rightTrim()); + } + public function testOneOf() { $spec = new TypeParser(new Config(), 'type', 'b');