Skip to content

Commit

Permalink
scrutiny
Browse files Browse the repository at this point in the history
  • Loading branch information
James Galecki committed Mar 6, 2018
1 parent 084f347 commit 2cb2a29
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
99 changes: 66 additions & 33 deletions src/Filter/Ints.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,91 @@ final class Ints
*/
public static function filter($value, bool $allowNull = false, int $minValue = null, int $maxValue = PHP_INT_MAX)
{
if ($allowNull === true && $value === null) {
if (self::valueIsNullAndValid($allowNull, $value)) {
return null;
}

$valueInt = self::getValueInt($value);

if ($minValue !== null && $valueInt < $minValue) {
throw new FilterException("{$valueInt} is less than {$minValue}");
}

if ($valueInt > $maxValue) {
throw new FilterException("{$valueInt} is greater than {$maxValue}");
}

return $valueInt;
}

private static function valueIsNullAndValid(bool $allowNull, $value = null) : bool
{
if ($allowNull === false && $value === null) {
throw new FilterException('Value failed filtering, $allowNull is set to false');
}
return $allowNull === true && $value === null;
}

private static function getValueInt($value) : int
{
$valueInt = null;
if (is_int($value)) {
$valueInt = $value;
} elseif (is_string($value)) {
$value = trim($value);
return $value;
}

if (strlen($value) === 0) {
throw new FilterException('$value string length is zero');
}
if (is_string($value)) {
return self::handleStringValues($value);
}

$stringToCheckDigits = $value;
throw new FilterException('"' . var_export($value, true) . '" $value is not a string');
}

if ($value[0] === '-' || $value[0] === '+') {
$stringToCheckDigits = substr($value, 1);
}
private static function handleStringValues(string $value) : int
{
$value = trim($value);

if (!ctype_digit($stringToCheckDigits)) {
throw new FilterException(
"{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally "
. "surrounded by whitespace"
);
}
self::enforceStringLength($value);
self::checkDigits($value);
$casted = self::castAndEnforceValidIntegerSize($value);

$phpIntMin = ~PHP_INT_MAX;
return $casted;
}

$casted = (int)$value;
private static function enforceStringLength(string $value)
{
if (strlen($value) === 0) {
throw new FilterException('$value string length is zero');
}
}

if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) {
throw new FilterException("{$value} was greater than a max int of " . PHP_INT_MAX);
}
private static function checkDigits(string $value)
{
$stringToCheckDigits = $value;

if ($casted === $phpIntMin && $value !== (string)$phpIntMin) {
throw new FilterException("{$value} was less than a min int of {$phpIntMin}");
}
if ($value[0] === '-' || $value[0] === '+') {
$stringToCheckDigits = substr($value, 1);
}

$valueInt = $casted;
} else {
throw new FilterException('"' . var_export($value, true) . '" $value is not a string');
if (!ctype_digit($stringToCheckDigits)) {
throw new FilterException(
"{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally "
. "surrounded by whitespace"
);
}
}

if ($minValue !== null && $valueInt < $minValue) {
throw new FilterException("{$valueInt} is less than {$minValue}");
private static function castAndEnforceValidIntegerSize(string $value) : int
{
$phpIntMin = ~PHP_INT_MAX;
$casted = (int)$value;
if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) {
throw new FilterException("{$value} was greater than a max int of " . PHP_INT_MAX);
}

if ($valueInt > $maxValue) {
throw new FilterException("{$valueInt} is greater than {$maxValue}");
if ($casted === $phpIntMin && $value !== (string)$phpIntMin) {
throw new FilterException("{$value} was less than a min int of {$phpIntMin}");
}

return $valueInt;
return $casted;
}
}
2 changes: 1 addition & 1 deletion tests/Filter/UnsignedIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function filterAllowNullSuccess()
* @test
* @covers ::filter
* @expectedException \TraderInteractive\Exceptions\FilterException
* @expectedExceptionMessage "NULL" $value is not a string
* @expectedExceptionMessage Value failed filtering, $allowNull is set to false
*/
public function filterAllowNullFail()
{
Expand Down

0 comments on commit 2cb2a29

Please sign in to comment.