https://github.com/guardsman/guardsman
Guard clause assertion library to enforce parameter preconditions in PHP
https://github.com/guardsman/guardsman
assertion-library guard php preconditions
Last synced: 5 months ago
JSON representation
Guard clause assertion library to enforce parameter preconditions in PHP
- Host: GitHub
- URL: https://github.com/guardsman/guardsman
- Owner: guardsman
- License: mit
- Archived: true
- Created: 2015-08-23T07:35:40.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2016-07-15T05:58:31.000Z (almost 10 years ago)
- Last Synced: 2025-05-25T09:42:06.256Z (about 1 year ago)
- Topics: assertion-library, guard, php, preconditions
- Language: PHP
- Homepage:
- Size: 69.3 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Guardsman :guardsman:

A guard clause assertion library to enforce parameter preconditions.
This provides a framework free, simple, chainable api for method parameter validation that throws
exceptions on failure.
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](https://packagist.org/search/?q=validation) instead.
## Installation
```bash
composer require guardsman/guardsman
```
## Example Usage
```php
public function rename($name) {
\Guardsman\check($name)
->isString()
->isNotEmpty();
…
}
```
```php
public function setSeconds($seconds) {
\Guardsman\check($seconds)
->isInteger()
->isGreaterThanOrEqualTo(0)
->isLessThan(60);
…
}
```
```php
public function changeStatus($status) {
\Guardsman\check($status)
->isValueOf(self::validStatuses);
…
}
```
## Preconditions
### Array
```php
\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`*
```php
\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
```php
\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.*
```php
\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.*
```php
\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.
src\Your\Namespace\SuperGuard.php
```php
namespace Your\Namespace;
class SuperGuard extends \Guardsman\Guardsman
{
public function isYourPreconditionMethod()
{
…
}
}
```
src\Your\Namespace\check.php
```php
namespace Your\Namespace;
function check($subject)
{
return new SuperGuard($subject);
}
```
composer.json
```
"autoload": {
"files": ["src/Your/Namespace/check.php"]
}
```
Usage:
```php
\Your\Namespace\check($subject)->isYourPreconditionMethod();
```
### Show Thanks
If you find this useful then please show your thanks with [a small donation](https://paypal.me/le6o/10).