A collection of various PHP types check classes.
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 as a "one-liner". Instead of using the following code:
if (!is_array($value)) {
thrownewTypeInvalidException('array', gettype($this->value));
}just use this one:
$checkedArray = (newChecker($value))->checkArray();composer require ixnode/php-checkervendor/bin/php-checker -Vphp-checker 0.1.0 (12-30-2022 18:08:35) - Björn Hempel <bjoern@hempel.li>useIxnode\PhpChecker\Checker;$array = (newChecker(.0))->checkFloat();useIxnode\PhpChecker\CheckerArray;$array = (newCheckerArray([newChecker(123), newChecker(456), newChecker(678)])->checkClass(Checker::class);useIxnode\PhpChecker\CheckerClass;$array = (newCheckerClass(newChecker(123)))->check(Checker::class);useIxnode\PhpChecker\CheckerJson;$array = (newCheckerJson('{"1": 1, "2": 2, "3": 3}'))->check();Checks general data type specific properties.
| Method | Expected input | Method Parameters | Output value (if passed) | Exception |
|---|---|---|---|---|
checkArray (Alias ofCheckerArray::check) | array<int|string, mixed> | null | Same as input | TypeInvalidException |
checkClass (Alias ofCheckerClass::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 ofCheckerJson::check) | json-string | null | Same as input | TypeInvalidException |
checkObject | object | null | Same as input | TypeInvalidException |
checkStdClass (Alias ofCheckerClass::checkStdClass) | stdClass | null | Same as input | TypeInvalidException |
checkString | string | null | Same as input | TypeInvalidException |
checkStringOrNull | string|null | null | Same as input | TypeInvalidException |
Checks array specific properties.
| Method | Expected input | Method Parameters | Output value (if passed) | Exception |
|---|---|---|---|---|
check | array<int|string, mixed> | null | Same as input | TypeInvalidException |
checkArray | array<int|tring, array<string, mixed>> | null or array<int, string> | array<int, array<string, mixed>> | TypeInvalidException |
checkAssoziative | array<string, mixed> | null | Same as input | TypeInvalidException |
checkClass | array<int|string, ClassName> | class-string | Same as input | TypeInvalidException |
checkFlat | array<int|string, bool|float|int|string|null> | null | Same as input | TypeInvalidException |
checkSequential | array<int, mixed> | null | Same as input | TypeInvalidException |
checkString | array<int|string, string> | null | Same as input | TypeInvalidException |
checkStringOrNull | array<int|string, string|null> | null | Same as input | TypeInvalidException |
checkIndex | array<int|;string, mixed> | string | mixed (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArray | array<int|string, array<int|string, mixed>> | string | array<int|string, mixed> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArrayArray | array<int|string, array<int|string, array<string, mixed>>> | string, null or array<int, string> | array<int, array<string, mixed>> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArrayAssoziative | array<int|string, array<string, mixed>> | string | array<string, mixed> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArrayClass | array<int|string, array<int|string, ClassName>> | string | array<int|string, ClassName> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArrayFlat | array<int|string, array<int|string, bool|float|int|string|null>> | string | array<int|string, bool|float|int|string|null> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArraySequential | array<int|string, array<int, mixed>> | string | array<int, string> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArrayString | array<int|string, array<int|string, string>> | string | array<int|string, string> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexArrayStringOrNull | array<int|string, array<int|string, string|null>> | string | array<int|string, string|null> (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexInteger | array<int|string, int> | string | int (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexString | array<int|string, string> | string | string (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
checkIndexStringOrNull | array<int|string, string|null> | string | string|null (index of given) | TypeInvalidException, ArrayKeyNotFoundException |
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 |
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 |
git clone git@github.com:ixnode/php-checker.git &&cd php-checkercomposer installcomposer testThis tool is licensed under the MIT License - see the LICENSE file for details