Skip to content

Commit

Permalink
added abstract in-range parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvi committed Aug 11, 2016
1 parent 9da2b79 commit e963f61
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 83 deletions.
7 changes: 7 additions & 0 deletions examples/symfony.php
Expand Up @@ -29,6 +29,13 @@ public function __construct(\Symfony\Component\HttpFoundation\Request $request)
$this->initRequestParser($request);
}

public function inRange()
{
$name = $this->queryParameter('name')->int()->inRange(1, 5)->required();

return "Hello $name";
}

public function hello()
{
$name = $this->queryParameter('name')->string()->required();
Expand Down
39 changes: 39 additions & 0 deletions src/AbstractInRangeParser.php
@@ -0,0 +1,39 @@
<?php

namespace MPScholten\RequestParser;

abstract class AbstractInRangeParser extends AbstractValueParser
{
/**
* @var int|float $minValue
*/
protected $minValue;
/**
* @var int|float $maxValue
*/
protected $maxValue;

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

protected function parse($value)
{
if (!is_numeric($value)) {
return null;
}

if ($this instanceof IntInRangeParser) {
$value = (int) $value;
} elseif ($this instanceof FloatInRangeParser) {
$value = (float) $value;
}

if ($value >= $this->minValue && $value <= $this->maxValue) {
return $value;
}
}
}
40 changes: 40 additions & 0 deletions src/FloatInRangeParser.php
@@ -0,0 +1,40 @@
<?php

namespace MPScholten\RequestParser;

class FloatInRangeParser extends AbstractInRangeParser
{
protected function describe()
{
return "a float value between $this->minValue and $this->maxValue";
}

/**
* @param $value
* @return float
*/
protected function parse($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);
}
}
8 changes: 4 additions & 4 deletions src/FloatParser.php
Expand Up @@ -33,12 +33,12 @@ public function required($invalidValueMessage = null, $notFoundMessage = null)
}

/**
* @param float $minvalue
* @param float $minValue
* @param float $maxValue
* @return InRangeParser
* @return FloatInRangeParser
*/
public function inRange($minvalue, $maxValue)
public function inRange($minValue, $maxValue)
{
return new InRangeParser($this->config, $this->name, $this->value, $minvalue, $maxValue, 'float');
return new FloatInRangeParser($this->config, $this->name, $this->value, $minValue, $maxValue);
}
}
77 changes: 0 additions & 77 deletions src/InRangeParser.php

This file was deleted.

40 changes: 40 additions & 0 deletions src/IntInRangeParser.php
@@ -0,0 +1,40 @@
<?php

namespace MPScholten\RequestParser;

class IntInRangeParser extends AbstractInRangeParser
{
protected function describe()
{
return "an integer value between $this->minValue and $this->maxValue";
}

/**
* @param $value
* @return int
*/
protected function parse($value)
{
return parent::parse($value);
}

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

/**
* @throws \Exception
* @param string $invalidValueMessage
* @param string $notFoundMessage
* @return int
*/
public function required($invalidValueMessage = null, $notFoundMessage = null)
{
return parent::required($invalidValueMessage, $notFoundMessage);
}
}
4 changes: 2 additions & 2 deletions src/IntParser.php
Expand Up @@ -35,10 +35,10 @@ public function required($invalidValueMessage = null, $notFoundMessage = null)
/**
* @param int $minvalue
* @param int $maxValue
* @return InRangeParser
* @return IntInRangeParser
*/
public function inRange($minvalue, $maxValue)
{
return new InRangeParser($this->config, $this->name, $this->value, $minvalue, $maxValue, 'int');
return new IntInRangeParser($this->config, $this->name, $this->value, $minvalue, $maxValue);
}
}

0 comments on commit e963f61

Please sign in to comment.