Skip to content

Repository files navigation

PHP Checker

ReleasePHPPHPStanPHPCSLICENSE

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 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();

Installation

composer require ixnode/php-checker
vendor/bin/php-checker -V
php-checker 0.1.0 (12-30-2022 18:08:35) - Björn Hempel <bjoern@hempel.li>

Usage

Example 1

useIxnode\PhpChecker\Checker;
$array = (newChecker(.0))->checkFloat();

Example 2

useIxnode\PhpChecker\CheckerArray;
$array = (newCheckerArray([newChecker(123), newChecker(456), newChecker(678)])->checkClass(Checker::class);

Example 3

useIxnode\PhpChecker\CheckerClass;
$array = (newCheckerClass(newChecker(123)))->check(Checker::class);

Example 4

useIxnode\PhpChecker\CheckerJson;
$array = (newCheckerJson('{"1": 1, "2": 2, "3": 3}'))->check();

Available checkers

Class Ixnode\PhpChecker\Checker

Checks general data type specific properties.

MethodExpected inputMethod ParametersOutput value (if passed)Exception
checkArray (Alias ofCheckerArray::check)array<int|string, mixed>nullSame as inputTypeInvalidException
checkClass (Alias ofCheckerClass::checkClass)ClassNameclass-stringSame as inputTypeInvalidException, ClassInvalidException
checkFloatfloatnullSame as inputTypeInvalidException
checkIntegerintnullSame as inputTypeInvalidException
checkIterableiterablenullSame as inputTypeInvalidException
checkJson (Alias ofCheckerJson::check)json-stringnullSame as inputTypeInvalidException
checkObjectobjectnullSame as inputTypeInvalidException
checkStdClass (Alias ofCheckerClass::checkStdClass)stdClassnullSame as inputTypeInvalidException
checkStringstringnullSame as inputTypeInvalidException
checkStringOrNullstring|nullnullSame as inputTypeInvalidException

Class Ixnode\PhpChecker\CheckerArray

Checks array specific properties.

MethodExpected inputMethod ParametersOutput value (if passed)Exception
checkarray<int|string, mixed>nullSame as inputTypeInvalidException
checkArrayarray<int|tring, array<string, mixed>>null or array<int, string>array<int, array<string, mixed>>TypeInvalidException
checkAssoziativearray<string, mixed>nullSame as inputTypeInvalidException
checkClassarray<int|string, ClassName>class-stringSame as inputTypeInvalidException
checkFlatarray<int|string, bool|float|int|string|null>nullSame as inputTypeInvalidException
checkSequentialarray<int, mixed>nullSame as inputTypeInvalidException
checkStringarray<int|string, string>nullSame as inputTypeInvalidException
checkStringOrNullarray<int|string, string|null>nullSame as inputTypeInvalidException
checkIndexarray<int|;string, mixed>stringmixed (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayarray<int|string, array<int|string, mixed>>stringarray<int|string, mixed> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayArrayarray<int|string, array<int|string, array<string, mixed>>>string, null or array<int, string>array<int, array<string, mixed>> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayAssoziativearray<int|string, array<string, mixed>>stringarray<string, mixed> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayClassarray<int|string, array<int|string, ClassName>>stringarray<int|string, ClassName> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayFlatarray<int|string, array<int|string, bool|float|int|string|null>>stringarray<int|string, bool|float|int|string|null> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArraySequentialarray<int|string, array<int, mixed>>stringarray<int, string> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayStringarray<int|string, array<int|string, string>>stringarray<int|string, string> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexArrayStringOrNullarray<int|string, array<int|string, string|null>>stringarray<int|string, string|null> (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexIntegerarray<int|string, int>stringint (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexStringarray<int|string, string>stringstring (index of given)TypeInvalidException, ArrayKeyNotFoundException
checkIndexStringOrNullarray<int|string, string|null>stringstring|null (index of given)TypeInvalidException, ArrayKeyNotFoundException

Class Ixnode\PhpChecker\CheckerClass

Checks class specific properties.

MethodExpected inputMethod ParametersOutput value (if passed)Exception
checkClassClassNameclass-stringSame as inputTypeInvalidException, ClassInvalidException
checkStdClassstdClassnullSame as inputTypeInvalidException

Class Ixnode\PhpChecker\CheckerJson

Checks JSON specific properties.

MethodExpected inputMethod ParametersOutput value (if passed)Exception
checkjson-stringnullSame as inputTypeInvalidException
isJsonjson-stringnullboolnull

Development

git clone git@github.com:ixnode/php-checker.git &&cd php-checker
composer install
composer test

License

This tool is licensed under the MIT License - see the LICENSE file for details

About

A collection of various PHP types check classes.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages