Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yuvi
committed
Aug 11, 2016
1 parent
9da2b79
commit e963f61
Showing
7 changed files
with
132 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters