Skip to content

Commit

Permalink
Merge pull request #45 from kasitmp/add-string-length-validation
Browse files Browse the repository at this point in the history
Add string length validation
  • Loading branch information
mpscholten authored Mar 8, 2017
2 parents 45ae18a + 15142fb commit c8df364
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 2 deletions.
61 changes: 60 additions & 1 deletion src/StringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace MPScholten\RequestParser;

use MPScholten\RequestParser\Validator\EmailParser;
use MPScholten\RequestParser\Validator\StringLengthBetween;
use MPScholten\RequestParser\Validator\StringLengthLargerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\StringLengthLargerThanParser;
use MPScholten\RequestParser\Validator\StringLengthSmallerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\StringLengthSmallerThanParser;
use MPScholten\RequestParser\Validator\UrlParser;

class StringParser extends AbstractValueParser
Expand All @@ -27,7 +32,10 @@ public function defaultsTo($defaultValue)
}

/**
* @return string
* @param string $invalidValueMessage
* @param string $notFoundMessage
*
* @return null
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
Expand Down Expand Up @@ -71,4 +79,55 @@ public function rightTrim()
{
return new TrimParser($this->config, $this->name, $this->value, TrimParser::RIGHT_TRIM);
}

/**
* @param int $minValue
* @param int $maxValue
*
* @return StringLengthBetween
*/
public function lengthBetween($minValue, $maxValue)
{
return new StringLengthBetween($this->config, $this->name, $this->value, $minValue, $maxValue);
}

/**
* @param int $minValue
*
* @return StringLengthLargerThanParser
*/
public function lengthLargerThan($minValue)
{
return new StringLengthLargerThanParser($this->config, $this->name, $this->value, $minValue);
}

/**
* @param int $minValue
*
* @return StringLengthLargerThanOrEqualToParser
*/
public function lengthLargerThanOrEqualTo($minValue)
{
return new StringLengthLargerThanOrEqualToParser($this->config, $this->name, $this->value, $minValue);
}

/**
* @param int $maxValue
*
* @return StringLengthSmallerThanParser
*/
public function lengthSmallerThan($maxValue)
{
return new StringLengthSmallerThanParser($this->config, $this->name, $this->value, $maxValue);
}

/**
* @param int $maxValue
*
* @return StringLengthSmallerThanOrEqualToParser
*/
public function lengthSmallerThanOrEqualTo($maxValue)
{
return new StringLengthSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue);
}
}
43 changes: 43 additions & 0 deletions src/Validator/StringLengthBetween.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringLengthBetween extends AbstractBetweenParser
{
protected function describe()
{
return "a string with character length between $this->minValue and $this->maxValue";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (strlen($value) >= $this->minValue && strlen($value) <= $this->maxValue) {
return $value;
}
return null;
}

/**
* @param string $defaultValue
* @return string
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return string
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
43 changes: 43 additions & 0 deletions src/Validator/StringLengthLargerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringLengthLargerThanOrEqualToParser extends AbstractLargerThanOrEqualToParser
{
protected function describe()
{
return "a string longer than or equal to $this->minValue characters";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (strlen($value) >= $this->minValue) {
return $value;
}
return null;
}

/**
* @param string $defaultValue
* @return string
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return string
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
43 changes: 43 additions & 0 deletions src/Validator/StringLengthLargerThanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringLengthLargerThanParser extends AbstractLargerThanParser
{
protected function describe()
{
return "a string longer than $this->minValue characters";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (strlen($value) > $this->minValue) {
return $value;
}
return null;
}

/**
* @param string $defaultValue
* @return string
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return string
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
43 changes: 43 additions & 0 deletions src/Validator/StringLengthSmallerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringLengthSmallerThanOrEqualToParser extends AbstractSmallerThanOrEqualToParser
{
protected function describe()
{
return "a string shorter than or equal to $this->maxValue characters";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (strlen($value) <= $this->maxValue) {
return $value;
}
return null;
}

/**
* @param string $defaultValue
* @return string
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return string
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
43 changes: 43 additions & 0 deletions src/Validator/StringLengthSmallerThanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringLengthSmallerThanParser extends AbstractSmallerThanParser
{
protected function describe()
{
return "a string shorter than $this->maxValue characters";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (strlen($value) < $this->maxValue) {
return $value;
}
return null;
}

/**
* @param string $defaultValue
* @return string
*/
public function defaultsTo($defaultValue)
{
return parent::defaultsTo($defaultValue);
}

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return string
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
Loading

0 comments on commit c8df364

Please sign in to comment.