Skip to content

Commit

Permalink
Merge pull request #41 from mpscholten/min-max-parser
Browse files Browse the repository at this point in the history
[WIP] min/max parser and restructured namespaces (ValidationParser)
  • Loading branch information
mpscholten authored Feb 28, 2017
2 parents 1254985 + 5b2e42d commit 413da18
Show file tree
Hide file tree
Showing 24 changed files with 729 additions and 18 deletions.
42 changes: 42 additions & 0 deletions src/FloatParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace MPScholten\RequestParser;

use MPScholten\RequestParser\Validator\FloatBetweenParser;
use MPScholten\RequestParser\Validator\FloatLargerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\FloatLargerThanParser;
use MPScholten\RequestParser\Validator\FloatSmallerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\FloatSmallerThanParser;

class FloatParser extends AbstractValueParser
{
protected function describe()
Expand Down Expand Up @@ -41,4 +47,40 @@ public function between($minValue, $maxValue)
{
return new FloatBetweenParser($this->config, $this->name, $this->value, $minValue, $maxValue);
}

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

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

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

/**
* @param int $maxValue
* @return FloatSmallerThanOrEqualToParser
*/
public function smallerThanOrEqualTo($maxValue)
{
return new FloatSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue);
}
}
48 changes: 45 additions & 3 deletions src/IntParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace MPScholten\RequestParser;

use MPScholten\RequestParser\Validator\IntBetweenParser;
use MPScholten\RequestParser\Validator\IntLargerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\IntLargerThanParser;
use MPScholten\RequestParser\Validator\IntSmallerThanOrEqualToParser;
use MPScholten\RequestParser\Validator\IntSmallerThanParser;

class IntParser extends AbstractValueParser
{
protected function describe()
Expand Down Expand Up @@ -33,12 +39,48 @@ public function required($invalidValueMessage = null, $notFoundMessage = null)
}

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

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

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

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

/**
* @param int $maxValue
* @return IntSmallerThanOrEqualToParser
*/
public function smallerThanOrEqualTo($maxValue)
{
return new IntBetweenParser($this->config, $this->name, $this->value, $minvalue, $maxValue);
return new IntSmallerThanOrEqualToParser($this->config, $this->name, $this->value, $maxValue);
}
}
3 changes: 3 additions & 0 deletions src/StringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace MPScholten\RequestParser;

use MPScholten\RequestParser\Validator\EmailParser;
use MPScholten\RequestParser\Validator\UrlParser;

class StringParser extends AbstractValueParser
{
protected function describe()
Expand Down
2 changes: 2 additions & 0 deletions src/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MPScholten\RequestParser;

use MPScholten\RequestParser\Validator\OneOfParser;

class TypeParser
{
private $value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace MPScholten\RequestParser;
namespace MPScholten\RequestParser\Validator;

use MPScholten\RequestParser\AbstractValueParser;
use MPScholten\RequestParser\Config;

abstract class AbstractBetweenParser extends AbstractValueParser
{
Expand Down
25 changes: 25 additions & 0 deletions src/Validator/AbstractLargerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MPScholten\RequestParser\Validator;

use MPScholten\RequestParser\AbstractValueParser;
use MPScholten\RequestParser\Config;

abstract class AbstractLargerThanOrEqualToParser extends AbstractValueParser
{
protected $minValue;

public function __construct(Config $config, $name, $value, $minValue)
{
$this->minValue = $minValue;
parent::__construct($config, $name, $value);
}

protected function parse($value)
{
if ($value >= $this->minValue) {
return $value;
}
return null;
}
}
25 changes: 25 additions & 0 deletions src/Validator/AbstractLargerThanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MPScholten\RequestParser\Validator;

use MPScholten\RequestParser\AbstractValueParser;
use MPScholten\RequestParser\Config;

abstract class AbstractLargerThanParser extends AbstractValueParser
{
protected $minValue;

public function __construct(Config $config, $name, $value, $minValue)
{
$this->minValue = $minValue;
parent::__construct($config, $name, $value);
}

protected function parse($value)
{
if ($value > $this->minValue) {
return $value;
}
return null;
}
}
25 changes: 25 additions & 0 deletions src/Validator/AbstractSmallerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MPScholten\RequestParser\Validator;

use MPScholten\RequestParser\AbstractValueParser;
use MPScholten\RequestParser\Config;

abstract class AbstractSmallerThanOrEqualToParser extends AbstractValueParser
{
protected $maxValue;

public function __construct(Config $config, $name, $value, $maxValue)
{
$this->maxValue = $maxValue;
parent::__construct($config, $name, $value);
}

protected function parse($value)
{
if ($value <= $this->maxValue) {
return $value;
}
return null;
}
}
25 changes: 25 additions & 0 deletions src/Validator/AbstractSmallerThanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace MPScholten\RequestParser\Validator;

use MPScholten\RequestParser\AbstractValueParser;
use MPScholten\RequestParser\Config;

abstract class AbstractSmallerThanParser extends AbstractValueParser
{
protected $maxValue;

public function __construct(Config $config, $name, $value, $maxValue)
{
$this->maxValue = $maxValue;
parent::__construct($config, $name, $value);
}

protected function parse($value)
{
if ($value < $this->maxValue) {
return $value;
}
return null;
}
}
4 changes: 3 additions & 1 deletion src/EmailParser.php → src/Validator/EmailParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace MPScholten\RequestParser;
namespace MPScholten\RequestParser\Validator;

use MPScholten\RequestParser\AbstractValueParser;

class EmailParser extends AbstractValueParser
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MPScholten\RequestParser;
namespace MPScholten\RequestParser\Validator;

class FloatBetweenParser extends AbstractBetweenParser
{
Expand Down
44 changes: 44 additions & 0 deletions src/Validator/FloatLargerThanOrEqualToParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MPScholten\RequestParser\Validator;

class FloatLargerThanOrEqualToParser extends AbstractLargerThanOrEqualToParser
{
protected function describe()
{
return "a float larger than or equal to $this->minValue";
}

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

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

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

namespace MPScholten\RequestParser\Validator;

class FloatLargerThanParser extends AbstractLargerThanParser
{
protected function describe()
{
return "a float larger than $this->minValue";
}

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

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

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

0 comments on commit 413da18

Please sign in to comment.