Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/backendtea/assert
https://github.com/backendtea/assert
Last synced: 17 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/backendtea/assert
- Owner: BackEndTea
- License: mit
- Created: 2020-04-21T18:37:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-11T16:50:58.000Z (over 4 years ago)
- Last Synced: 2024-11-24T19:41:29.822Z (about 2 months ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Assert
Static analysis safe type assertions, trough callbacks.
## Usage
The `BackEndTea\Assert\Assert` class provides four methods:
* `Assert::either(callable(mixed): TOne, callable(mixed): TTwo): callable(mixed): TOne|TTwo`
* `Assert::both(callable(mixed): TOne, callable(mixed): TTwo): callable(mixed): TOne&TTwo`
* `Assert::not(callable(mixed): T): callable(mixed): ~T`
* `Assert::all(callable(mixed): T): callable(array): array`These methods are understood by static analysis tools capable of parsing template types (like Psalm & PHPStan).
Currently the `not` method is not understood by these tools.Callable to use are provided by the `Type` class, for example: `Type::string()` will return a callable that validates strings.
You can create your own callbacks, and those should be understood by the tooling, as long as your annotations are correct.Note that this library differs, in that it returns the validated value. Using this new value will make the tooling understand what it is.
```php
use BackEndTea\Assert\Assert;
use BackEndTea\Assert\Type;/**
* @param mixed $input
*/
function($input): ?string
{
$callable = Assert::either(
Type::string(),
Type::null()
);
return $callable($input);
}
```