Skip to content

Commit

Permalink
move trim type constants to TrimParser, add tests for all trim types,…
Browse files Browse the repository at this point in the history
… remove TrimType
  • Loading branch information
Yuval Herziger committed Feb 17, 2017
1 parent 2d21825 commit c5b1e15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/StringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 7 additions & 3 deletions src/TrimParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
10 changes: 0 additions & 10 deletions src/TrimType.php

This file was deleted.

5 changes: 3 additions & 2 deletions tests/ParserSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use MPScholten\RequestParser\StringParser;
use MPScholten\RequestParser\EmailParser;
use MPScholten\RequestParser\UrlParser;
use MPScholten\RequestParser\TrimType;
use MPScholten\RequestParser\TrimParser;

class ParserSpecTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -47,7 +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] ', TrimType::TRIM), '', '[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
12 changes: 12 additions & 0 deletions tests/TypeSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public function testTrim()
$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 c5b1e15

Please sign in to comment.