Skip to content

Commit

Permalink
Merge pull request #39 from mpscholten/string-trim
Browse files Browse the repository at this point in the history
added trim() to string parser
  • Loading branch information
mpscholten authored Feb 17, 2017
2 parents 3e9ea49 + c5b1e15 commit 1254985
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/StringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ public function email()
{
return new EmailParser($this->config, $this->name, $this->value);
}

public function trim()
{
return new TrimParser($this->config, $this->name, $this->value, TrimParser::TRIM);
}

public function leftTrim()
{
return new TrimParser($this->config, $this->name, $this->value, TrimParser::LEFT_TRIM);
}

public function rightTrim()
{
return new TrimParser($this->config, $this->name, $this->value, TrimParser::RIGHT_TRIM);
}
}
66 changes: 66 additions & 0 deletions src/TrimParser.php
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);
}
}
6 changes: 4 additions & 2 deletions tests/ParserSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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]]
Expand Down
19 changes: 19 additions & 0 deletions tests/TypeSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 1254985

Please sign in to comment.