ixnode/php-checker

PHP Checker - A collection of various PHP types check classes.

0.1.9 2023-06-25 09:31 UTC

This package is auto-updated.

Last update: 2024-04-25 11:23:34 UTC


README

Release PHP PHPStan PHPCS 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 as a "one-liner". Instead of using the following code:

if (!is_array($value)) {
    throw new TypeInvalidException('array', gettype($this->value));
}

just use this one:

$checkedArray = (new Checker($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

use Ixnode\PhpChecker\Checker;
$array = (new Checker(.0))->checkFloat();

Example 2

use Ixnode\PhpChecker\CheckerArray;
$array = (new CheckerArray([new Checker(123), new Checker(456), new Checker(678)])->checkClass(Checker::class);

Example 3

use Ixnode\PhpChecker\CheckerClass;
$array = (new CheckerClass(new Checker(123)))->check(Checker::class);

Example 4

use Ixnode\PhpChecker\CheckerJson;
$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<int|string, mixed> 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<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

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

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