Skip to content

Commit

Permalink
Update for new language levels
Browse files Browse the repository at this point in the history
  • Loading branch information
NanoSector committed May 26, 2019
1 parent b71bb52 commit 78240b5
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 113 deletions.
12 changes: 0 additions & 12 deletions .idea/validation-closures.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 46 additions & 37 deletions src/Ranges.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
namespace ValidationClosures;


use Closure;
use InvalidArgumentException;

class Ranges
{
/**
* @param int $minimumLength
* @param int $maximumLength
*
* @return \Closure
* @return Closure
*/
public static function stringWithLengthBetween(int $minimumLength, int $maximumLength): \Closure
public static function stringWithLengthBetween(int $minimumLength, int $maximumLength): Closure
{
if ($maximumLength < 0 || $maximumLength < 1)
throw new \InvalidArgumentException('Minimum length cannot be below 0, maximum length cannot be below 1');
if ($maximumLength < 0 || $maximumLength < 1) {
throw new InvalidArgumentException('Minimum length cannot be below 0, maximum length cannot be below 1');
}

return function ($value) use ($minimumLength, $maximumLength)
return static function ($value) use ($minimumLength, $maximumLength)
{
return Types::string()($value) && static::intBetween($minimumLength, $maximumLength)(strlen($value));
};
Expand All @@ -32,14 +36,15 @@ public static function stringWithLengthBetween(int $minimumLength, int $maximumL
* @param int $minimum
* @param int $maximum
*
* @return \Closure
* @return Closure
*/
public static function intBetween(int $minimum, int $maximum): \Closure
public static function intBetween(int $minimum, int $maximum): Closure
{
if ($maximum <= $minimum)
throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
if ($maximum <= $minimum) {
throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
}

return function ($value) use ($minimum, $maximum)
return static function ($value) use ($minimum, $maximum)
{
return Types::int()($value) && ($value >= $minimum && $value <= $maximum);
};
Expand All @@ -49,14 +54,15 @@ public static function intBetween(int $minimum, int $maximum): \Closure
* @param int $minimum
* @param int $maximum
*
* @return \Closure
* @return Closure
*/
public static function intBetweenExclusive(int $minimum, int $maximum): \Closure
public static function intBetweenExclusive(int $minimum, int $maximum): Closure
{
if ($maximum <= $minimum)
throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
if ($maximum <= $minimum) {
throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
}

return function ($value) use ($minimum, $maximum)
return static function ($value) use ($minimum, $maximum)
{
return Types::int()($value) && ($value > $minimum && $value < $maximum);
};
Expand All @@ -66,14 +72,15 @@ public static function intBetweenExclusive(int $minimum, int $maximum): \Closure
* @param float $minimum
* @param float $maximum
*
* @return \Closure
* @return Closure
*/
public static function floatBetween(float $minimum, float $maximum): \Closure
public static function floatBetween(float $minimum, float $maximum): Closure
{
if ($maximum <= $minimum)
throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
if ($maximum <= $minimum) {
throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
}

return function ($value) use ($minimum, $maximum)
return static function ($value) use ($minimum, $maximum)
{
return Types::float()($value) && ($value >= $minimum && $value <= $maximum);
};
Expand All @@ -83,14 +90,15 @@ public static function floatBetween(float $minimum, float $maximum): \Closure
* @param float $minimum
* @param float $maximum
*
* @return \Closure
* @return Closure
*/
public static function floatBetweenExclusive(float $minimum, float $maximum): \Closure
public static function floatBetweenExclusive(float $minimum, float $maximum): Closure
{
if ($maximum <= $minimum)
throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
if ($maximum <= $minimum) {
throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
}

return function ($value) use ($minimum, $maximum)
return static function ($value) use ($minimum, $maximum)
{
return Types::float()($value) && ($value > $minimum && $value < $maximum);
};
Expand All @@ -99,11 +107,11 @@ public static function floatBetweenExclusive(float $minimum, float $maximum): \C
/**
* @param array ...$allowedValues
*
* @return \Closure
* @return Closure
*/
public static function enum(...$allowedValues): \Closure
public static function enum(...$allowedValues): Closure
{
return function ($value) use ($allowedValues)
return static function ($value) use ($allowedValues)
{
return in_array($value, $allowedValues, true);
};
Expand All @@ -112,11 +120,11 @@ public static function enum(...$allowedValues): \Closure
/**
* @param array ...$allowedTypes
*
* @return \Closure
* @return Closure
*/
public static function typeEnum(...$allowedTypes): \Closure
public static function typeEnum(...$allowedTypes): Closure
{
return function ($value) use ($allowedTypes)
return static function ($value) use ($allowedTypes)
{
return in_array(gettype($value), $allowedTypes, true);
};
Expand All @@ -125,16 +133,17 @@ public static function typeEnum(...$allowedTypes): \Closure
/**
* @param array ...$allowedValues
*
* @return \Closure
* @return Closure
*/
public static function stringOneOf(...$allowedValues): \Closure
public static function stringOneOf(...$allowedValues): Closure
{
if (!Utils::validateArray(Types::string(), $allowedValues))
throw new \InvalidArgumentException('Ranges::stringOneOf expects arguments of type string only');
if (!Utils::validateArray(Types::string(), $allowedValues)) {
throw new InvalidArgumentException('Ranges::stringOneOf expects arguments of type string only');
}

return function ($value) use ($allowedValues)
return static function ($value) use ($allowedValues)
{
return Types::string() && in_array($value, $allowedValues);
return Types::string() && in_array($value, $allowedValues, true);
};
}
}
41 changes: 25 additions & 16 deletions src/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

namespace ValidationClosures;

use Closure;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionException;

/**
* Class Reflection
* @package ValidationClosures
Expand Down Expand Up @@ -36,34 +41,38 @@
*/
class Reflection
{
/**
* @param string $class
*
* @return \ReflectionClass
*/
public static function createReflectionObject(string $class): \ReflectionClass
/**
* @param string $class
*
* @return ReflectionClass
* @throws ReflectionException
*/
public static function createReflectionObject(string $class): ReflectionClass
{
if (!class_exists($class))
throw new \InvalidArgumentException('The given class does not exist');
if (!class_exists($class)) {
throw new InvalidArgumentException('The given class does not exist');
}

return new \ReflectionClass($class);
return new ReflectionClass($class);
}

/**
* @param string $method
* @param array $arguments
*
* @return \Closure
* @return Closure
*/
public static function __callStatic(string $method, array $arguments): \Closure
public static function __callStatic(string $method, array $arguments): Closure
{
if (!method_exists(\ReflectionClass::class, $method))
throw new \InvalidArgumentException('Cannot create closure from method ReflectionClass::' . $method . ', it does not exist');
if (!method_exists(ReflectionClass::class, $method)) {
throw new InvalidArgumentException('Cannot create closure from method ReflectionClass::' . $method . ', it does not exist');
}

return function ($value) use ($method, $arguments)
return static function ($value) use ($method, $arguments)
{
if (!Types::string()($value))
return false;
if (!Types::string()($value)) {
return false;
}

$reflection = static::createReflectionObject($value);

Expand Down
40 changes: 21 additions & 19 deletions src/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,82 @@

namespace ValidationClosures;

use Closure;

class Types
{
/**
* @return \Closure
* @return Closure
*/
public static function string(): \Closure
public static function string(): Closure
{
return Utils::createClosureFromCallable('is_string');
}

/**
* @return \Closure
* @return Closure
*/
public static function int(): \Closure
public static function int(): Closure
{
return Utils::createClosureFromCallable('is_int');
}

/**
* @return \Closure
* @return Closure
*/
public static function float(): \Closure
public static function float(): Closure
{
return Utils::createClosureFromCallable('is_float');
}

/**
* @return \Closure
* @return Closure
*/
public static function boolean(): \Closure
public static function boolean(): Closure
{
return Utils::createClosureFromCallable('is_bool');
}

/**
* @return \Closure
* @return Closure
*/
public static function array(): \Closure
public static function array(): Closure
{
return Utils::createClosureFromCallable('is_array');
}

/**
* @return \Closure
* @return Closure
*/
public static function callable(): \Closure
public static function callable(): Closure
{
return Utils::createClosureFromCallable('is_callable');
}

/**
* @return \Closure
* @return Closure
*/
public static function object(): \Closure
public static function object(): Closure
{
return Utils::createClosureFromCallable('is_object');
}

/**
* @return \Closure
* @return Closure
*/
public static function numeric(): \Closure
public static function numeric(): Closure
{
return Utils::createClosureFromCallable('is_numeric');
}

/**
* @param string $class
*
* @return \Closure
* @return Closure
*/
public static function instanceof(string $class): \Closure
public static function instanceof(string $class): Closure
{
return function ($value) use ($class)
return static function ($value) use ($class)
{
return $value instanceof $class;
};
Expand Down

0 comments on commit 78240b5

Please sign in to comment.