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');