Guardsman :guardsman:
, (*1)
A guard clause assertion library to enforce parameter preconditions., (*2)
This provides a framework free, simple, chainable api for method parameter validation that throws
exceptions on failure., (*3)
It is not intended as a validation library for end user input. If this is your use case then you should
try one of these validation libraries instead., (*4)
Installation
composer require guardsman/guardsman
Example Usage
public function rename($name) {
\Guardsman\check($name)
->isString()
->isNotEmpty();
…
}
public function setSeconds($seconds) {
\Guardsman\check($seconds)
->isInteger()
->isGreaterThanOrEqualTo(0)
->isLessThan(60);
…
}
public function changeStatus($status) {
\Guardsman\check($status)
->isValueOf(self::validStatuses);
…
}
Preconditions
Array
\Guardsman\check($subject)->isValueOf(array $array);
\Guardsman\check($subject)->isNotValueOf(array $array);
\Guardsman\check($subject)->isKeyOf(array $array);
\Guardsman\check($subject)->isNotKeyOf(array $array);
DateTime
Methods will first check that the subject is an instance of \DateTimeInterface
, (*5)
\Guardsman\check($subject)->isBefore(\DateTimeInterface $limit);
\Guardsman\check($subject)->isBeforeOrEqualTo(\DateTimeInterface $limit);
\Guardsman\check($subject)->isAfter(\DateTimeInterface $limit);
\Guardsman\check($subject)->isAfterOrEqualTo(\DateTimeInterface $limit);
Empty
\Guardsman\check($subject)->isNotEmpty();
Number
Methods that accept a limit will first check that the subject is numeric.
Limits will then be checked to ensure they are numeric and positive., (*6)
\Guardsman\check($subject)->isNumeric();
\Guardsman\check($subject)->isInteger();
\Guardsman\check($subject)->isFloat();
\Guardsman\check($subject)->isGreaterThan($limit);
\Guardsman\check($subject)->isGreaterThanOrEqualTo($limit);
\Guardsman\check($subject)->isLessThan($limit);
\Guardsman\check($subject)->isLessThanOrEqualTo($limit);
\Guardsman\check($subject)->isPositive();
\Guardsman\check($subject)->isNegative();
String
Methods that accept a limit will first check that the subject is a string and that the encoding matches mb_internal_encoding
Limits will then be checked to ensure they are numeric and positive., (*7)
\Guardsman\check($subject)->isString();
\Guardsman\check($subject)->isShorterThan($limit);
\Guardsman\check($subject)->isShorterThanOrEqualTo($limit);
\Guardsman\check($subject)->isLongerThan($limit);
\Guardsman\check($subject)->isLongerThanOrEqualTo($limit);
\Guardsman\check($subject)->matchesRegex($pattern);
Extending Guardsman
Simply extend the Guardsman class with your own methods and create a check function under your namespace., (*8)
src\Your\Namespace\SuperGuard.php, (*9)
namespace Your\Namespace;
class SuperGuard extends \Guardsman\Guardsman
{
public function isYourPreconditionMethod()
{
…
}
}
src\Your\Namespace\check.php, (*10)
namespace Your\Namespace;
function check($subject)
{
return new SuperGuard($subject);
}
composer.json, (*11)
"autoload": {
"files": ["src/Your/Namespace/check.php"]
}
Usage:, (*12)
\Your\Namespace\check($subject)->isYourPreconditionMethod();
Show Thanks
If you find this useful then please show your thanks with a small donation., (*13)