https://github.com/ixnode/php-checker
A collection of various PHP types check classes.
https://github.com/ixnode/php-checker
Last synced: 8 months ago
JSON representation
A collection of various PHP types check classes.
- Host: GitHub
- URL: https://github.com/ixnode/php-checker
- Owner: ixnode
- License: mit
- Created: 2022-12-30T15:09:22.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-25T09:31:56.000Z (almost 3 years ago)
- Last Synced: 2024-12-27T01:41:39.372Z (over 1 year ago)
- Language: PHP
- Size: 78.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Checker
[](https://github.com/ixnode/php-checker/releases)
[](https://www.php.net/supported-versions.php)
[](https://phpstan.org/user-guide/rule-levels)
[](https://www.php-fig.org/psr/psr-12/)
[](https://github.com/ixnode/php-checker/blob/master/LICENSE)
> A collection of various PHP types check classes.
## Introduction
This package is helpful to validate complex data types like complex arrays and to comply with DocBlock declarations
(static code analysis tools like PHPStan or Psalm). Uses and throws exceptions from
[ixnode/php-exception](https://github.com/ixnode/php-exception) as a "one-liner". Instead of using the following code:
```php
if (!is_array($value)) {
throw new TypeInvalidException('array', gettype($this->value));
}
```
just use this one:
```php
$checkedArray = (new Checker($value))->checkArray();
```
## Installation
```bash
composer require ixnode/php-checker
```
```bash
vendor/bin/php-checker -V
```
```bash
php-checker 0.1.0 (12-30-2022 18:08:35) - Björn Hempel
```
## Usage
### Example 1
```php
use Ixnode\PhpChecker\Checker;
```
```php
$array = (new Checker(.0))->checkFloat();
```
### Example 2
```php
use Ixnode\PhpChecker\CheckerArray;
```
```php
$array = (new CheckerArray([new Checker(123), new Checker(456), new Checker(678)])->checkClass(Checker::class);
```
### Example 3
```php
use Ixnode\PhpChecker\CheckerClass;
```
```php
$array = (new CheckerClass(new Checker(123)))->check(Checker::class);
```
### Example 4
```php
use Ixnode\PhpChecker\CheckerJson;
```
```php
$array = (new CheckerJson('{"1": 1, "2": 2, "3": 3}'))->check();
```
## Available checkers
### Class `Ixnode\PhpChecker\Checker`
Checks general data type specific properties.
| Method | Expected input | Method Parameters | Output value (if passed) | Exception |
|------------------------------------------------------------|---------------------------------|-------------------|--------------------------|-------------------------------------------------|
| `checkArray` (_Alias of_ `CheckerArray::check`) | `array` | `null` | _Same as input_ | `TypeInvalidException` || `checkBoolean` | `bool` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkClass` (_Alias of_ `CheckerClass::checkClass`) | `ClassName` | `class-string` | _Same as input_ | `TypeInvalidException`, `ClassInvalidException` |
| `checkFloat` | `float` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkInteger` | `int` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkIterable` | `iterable` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkJson` (_Alias of_ `CheckerJson::check`) | `json-string` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkObject` | `object` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkStdClass` (_Alias of_ `CheckerClass::checkStdClass`) | `stdClass` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkString` | `string` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkStringOrNull` | `string\|null` | `null` | _Same as input_ | `TypeInvalidException` |
### Class `Ixnode\PhpChecker\CheckerArray`
Checks array specific properties.
| Method | Expected input | Method Parameters | Output value (if passed) | Exception |
|-------------------------------|--------------------------------------------------------------------------------------------------|------------------------------------------|---------------------------------------------------------------------------------------------|-----------------------------------------------------|
| `check` | `array` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkArray` | `array>` | `null` or `array` | `array>` | `TypeInvalidException` |
| `checkAssoziative` | `array` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkClass` | `array` | `class-string` | _Same as input_ | `TypeInvalidException` |
| `checkFlat` | `array` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkSequential` | `array` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkString` | `array` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkStringOrNull` | `array` | `null` | _Same as input_ | `TypeInvalidException` |
| `checkIndex` | `array` | `string` | `mixed` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArray` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArrayArray` | `array>>` | `string`, `null` or `array` | `array>` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArrayAssoziative` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArrayClass` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArrayFlat` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArraySequential` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArrayString` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexArrayStringOrNull` | `array>` | `string` | `array` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexInteger` | `array` | `string` | `int` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexString` | `array` | `string` | `string` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
| `checkIndexStringOrNull` | `array` | `string` | `string\|null` (_index of given_) | `TypeInvalidException`, `ArrayKeyNotFoundException` |
### Class `Ixnode\PhpChecker\CheckerClass`
Checks class specific properties.
| Method | Expected input | Method Parameters | Output value (if passed) | Exception |
|-----------------|----------------|-------------------|--------------------------|-------------------------------------------------|
| `checkClass` | `ClassName` | `class-string` | _Same as input_ | `TypeInvalidException`, `ClassInvalidException` |
| `checkStdClass` | `stdClass` | `null` | _Same as input_ | `TypeInvalidException` |
### Class `Ixnode\PhpChecker\CheckerJson`
Checks JSON specific properties.
| Method | Expected input | Method Parameters | Output value (if passed) | Exception |
|----------|----------------|-------------------|--------------------------|------------------------|
| `check` | `json-string` | `null` | _Same as input_ | `TypeInvalidException` |
| `isJson` | `json-string` | `null` | `bool` | `null` |
## Development
```bash
git clone git@github.com:ixnode/php-checker.git && cd php-checker
```
```bash
composer install
```
```bash
composer test
```
## License
This tool is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details