Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcinorlowski/php-type-asserts
PHP Data Type validation asserts
https://github.com/marcinorlowski/php-type-asserts
Last synced: about 12 hours ago
JSON representation
PHP Data Type validation asserts
- Host: GitHub
- URL: https://github.com/marcinorlowski/php-type-asserts
- Owner: MarcinOrlowski
- License: mit
- Created: 2022-09-26T13:16:52.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-30T18:46:01.000Z (9 months ago)
- Last Synced: 2024-09-21T10:04:54.558Z (4 days ago)
- Language: PHP
- Homepage:
- Size: 44.9 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# PHP Type Asserts #
[![Latest Stable Version](https://poser.pugx.org/marcin-orlowski/php-type-asserts/v/stable)](https://packagist.org/packages/marcin-orlowski/php-type-asserts)
[![License](https://poser.pugx.org/marcin-orlowski/php-type-asserts/license)](https://packagist.org/packages/marcin-orlowski/php-type-asserts)PHP Data type assertions.
---
This package provides helper methods to validate variable data type. While there are handy native
methods like `is_string()` or `is_array()`, you can have then check for single type at a time.
This package allows to validate against type union (i.e. `STRING|INT`) or ensure that priovided
`string` refers to existing class. Also, contrary to native methods, if there's no match and
variable contains data of non-welcomed type, exception is thrown which lets you simplify your
code flow.## Installation ##
```bash
composer require --dev marcin-orlowski/type-asserts
```----
## Usage example ##
The following code ensures `$var` is of either `int` or `float` type before math is done:
```php
use MarcinOrlowski\TypeAsserts\Type;
use MarcinOrlowski\TypeAsserts\Validator;$var = 'foo';
Validator::assertIsType($var, [Type::INT, Type::FLOAT]);
$result = $var * 5;
```The following code ensures `$var` refers to existing class, before we try to instantiate it:
```php
use MarcinOrlowski\TypeAsserts\Type;
use MarcinOrlowski\TypeAsserts\Validator;$cls = 'non-existing';
Validator::assertIsType($cls, Type::EXISTING_CLASS);
$obj = new $cls;
```----
## License ##
* Written and copyrighted ©2014-2022 by Marcin Orlowski
* Open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)