https://github.com/jdecool/guard-clauses
https://github.com/jdecool/guard-clauses
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jdecool/guard-clauses
- Owner: jdecool
- Created: 2025-02-04T21:26:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-07T20:37:47.000Z (over 1 year ago)
- Last Synced: 2026-02-01T13:49:43.621Z (5 months ago)
- Language: PHP
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Guard Clauses
[](https://packagist.org/packages/jdecool/guard-clauses)
[](https://github.com/jdecool/guard-clauses/actions?ref=main)
[](https://packagist.org/packages/jdecool/guard-clauses)
[](https://packagist.org/packages/jdecool/guard-clauses)
[](https://packagist.org/packages/jdecool/guard-clauses)
A PHP library providing a clean and expressive way to implement guard clauses in your code. Built on top of [webmozart/assert](https://github.com/webmozart/assert), this library offers a fluent interface for input validation and defensive programming.
## Installation
You can install the package via Composer:
```bash
composer require jdecool/guard-clauses
```
## Requirements
- PHP 8.1 or higher
## Usage
The library provides a `Guard` class with static methods for various assertions:
```php
use JDecool\GuardClauses\Guard;
// Basic type checks
$validatedValue = Guard::string($value);
$validatedValue = Guard::integer($value);
$validatedValue = Guard::float($value);
$validatedValue = Guard::boolean($value);
$validatedValue = Guard::object($value);
// String validations
$validatedValue = Guard::stringNotEmpty($value);
$validatedValue = Guard::contains($string, $substring);
$validatedValue = Guard::startsWith($string, $prefix);
$validatedValue = Guard::endsWith($string, $suffix);
// Numeric comparisons
$validatedValue = Guard::greaterThan($number, $limit);
$validatedValue = Guard::lessThan($number, $limit);
$validatedValue = Guard::range($number, $min, $max);
// Array operations
$validatedValue = Guard::isArray($value);
$validatedValue = Guard::count($array, $expectedCount);
$validatedValue = Guard::keyExists($array, $key);
// And many more...
```
Each assertion method accepts an optional message parameter that will be used in the exception if the assertion fails:
```php
$validatedValue = Guard::string($value, 'The value must be a string');
```
## Error Handling
When an assertion fails, a `GuardClausesException` is thrown, which extends from `InvalidArgumentException`.
## Development
To contribute to the development of this library:
1. Clone the repository
2. Install dependencies: `composer install`
3. Run tests: `composer test`
4. Run static analysis: `composer lint`
## License
This package is open-sourced software licensed under the [MIT license](LICENSE).