-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from mpscholten/string-trim
added trim() to string parser
- Loading branch information
Showing
4 changed files
with
104 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace MPScholten\RequestParser; | ||
|
||
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) | ||
{ | ||
$this->trimType = $trimType; | ||
parent::__construct($config, $name, $value); | ||
} | ||
|
||
protected function describe() | ||
{ | ||
return "a text string"; | ||
} | ||
|
||
protected function parse($value) | ||
{ | ||
if ($this->trimType === self::TRIM) { | ||
return trim((string) $value); | ||
} elseif ($this->trimType === self::LEFT_TRIM) { | ||
return ltrim((string) $value); | ||
} elseif ($this->trimType === self::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); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
use MPScholten\RequestParser\AbstractValueParser; | ||
use MPScholten\RequestParser\Config; | ||
use MPScholten\RequestParser\DateTimeParser; | ||
use MPScholten\RequestParser\ExceptionFactory; | ||
use MPScholten\RequestParser\ExceptionMessageFactory; | ||
use MPScholten\RequestParser\IntParser; | ||
use MPScholten\RequestParser\FloatParser; | ||
use MPScholten\RequestParser\InvalidValueException; | ||
|
@@ -16,6 +14,7 @@ | |
use MPScholten\RequestParser\StringParser; | ||
use MPScholten\RequestParser\EmailParser; | ||
use MPScholten\RequestParser\UrlParser; | ||
use MPScholten\RequestParser\TrimParser; | ||
|
||
class ParserSpecTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
@@ -47,6 +46,9 @@ 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', '[email protected]'), '', '[email protected]'], | ||
[new TrimParser(new Config(), 'emailAddress', ' [email protected] ', TrimParser::TRIM), '', '[email protected]'], | ||
[new TrimParser(new Config(), 'emailAddress', ' [email protected]', TrimParser::LEFT_TRIM), '', '[email protected]'], | ||
[new TrimParser(new Config(), 'emailAddress', '[email protected] ', TrimParser::RIGHT_TRIM), '', '[email protected]'], | ||
[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]] | ||
|
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 |
---|---|---|
|
@@ -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,24 @@ public function testEmail() | |
$this->assertInstanceOf(EmailParser::class, $spec->string()->email()); | ||
} | ||
|
||
public function testTrim() | ||
{ | ||
$spec = new TypeParser(new Config(), 'emailAddress', ' [email protected] '); | ||
$this->assertInstanceOf(TrimParser::class, $spec->string()->trim()); | ||
} | ||
|
||
public function testLeftTrim() | ||
{ | ||
$spec = new TypeParser(new Config(), 'emailAddress', ' [email protected]'); | ||
$this->assertInstanceOf(TrimParser::class, $spec->string()->leftTrim()); | ||
} | ||
|
||
public function testRightTrim() | ||
{ | ||
$spec = new TypeParser(new Config(), 'emailAddress', '[email protected] '); | ||
$this->assertInstanceOf(TrimParser::class, $spec->string()->rightTrim()); | ||
} | ||
|
||
public function testOneOf() | ||
{ | ||
$spec = new TypeParser(new Config(), 'type', 'b'); | ||
|