Skip to content

Commit

Permalink
added string validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kasitmp committed Mar 1, 2017
1 parent 413da18 commit e5440d8
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 2 deletions.
56 changes: 55 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\StringBetweenParser;
use MPScholten\RequestParser\Validator\StringLargerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\StringLargerThanParser;
use MPScholten\RequestParser\Validator\StringSmallerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\StringSmallerThanParser;
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,50 @@ public function rightTrim()
{
return new TrimParser($this->config, $this->name, $this->value, TrimParser::RIGHT_TRIM);
}

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

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

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

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

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

namespace MPScholten\RequestParser\Validator;

class StringBetweenParser 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 (!is_string($value)) {
return null;
}
$value = strlen($value);
return parent::parse($value);
}

/**
* @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);
}
}
44 changes: 44 additions & 0 deletions src/Validator/StringLargerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringLargerThanOrEqualToParser 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 (!is_string($value)) {
return null;
}
$value = strlen($value);
return parent::parse($value);
}

/**
* @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);
}
}
44 changes: 44 additions & 0 deletions src/Validator/StringLargerThanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser\Validator;

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

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (!is_string($value)) {
return null;
}
$value = strlen($value);
return parent::parse($value);
}

/**
* @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);
}
}
44 changes: 44 additions & 0 deletions src/Validator/StringSmallerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser\Validator;

class StringSmallerThanOrEqualToParser 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 (!is_string($value)) {
return null;
}
$value = strlen($value);
return parent::parse($value);
}

/**
* @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);
}
}
44 changes: 44 additions & 0 deletions src/Validator/StringSmallerThanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser\Validator;

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

/**
* @param $value
* @return int
*/
protected function parse($value)
{
if (!is_string($value)) {
return null;
}
$value = strlen($value);
return parent::parse($value);
}

/**
* @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 e5440d8

Please sign in to comment.