vancodx / php-data-validation
VanCodX PHP Data Validation
1.0.1
2025-03-08 15:30 UTC
Requires
- php: ^8.3
Requires (Dev)
- roave/security-advisories: dev-latest
- vancodx/php-coding-style: ^1.3.0
- vancodx/php-testing: ^1.6.0
This package is auto-updated.
Last update: 2025-05-08 15:49:14 UTC
README
This library contains a set of static methods for validation the type of arguments and variables of functions and methods. The main goal is to improve the quality of data validation and static analysis. Each validation method contains a PHPStan tag in its DocBlock comment to narrow the type of "value" argument.
Installation
Install this package with the following command:
composer require vancodx/php-data-validation
Usage
Example of validation the type of a function argument:
<?php declare(strict_types=1); use VanCodX\Data\Validation\Validation as V; /** * @param list<int> $ids * @return void */ function printIds(array $ids): void { if (!V::isListLenOfIntId($ids)) { throw V::newArgumentException(compact('ids')); } \PHPStan\dumpType($ids); // Reports: Dumped type: non-empty-list<int<1, max>> echo 'IDs: ', implode(', ', $ids), "\n"; }
Example of validation the type of a variable:
<?php declare(strict_types=1); use VanCodX\Data\Validation\Validation as V; /** * @return void */ function printUsername(): void { $username = getenv('USER'); if (!V::isStrLen($username)) { throw V::newValueException(compact('username')); } \PHPStan\dumpType($username); // Reports: Dumped type: non-empty-string echo 'Username: ', $username, "\n"; }
Running tests
Use the following command for running the tests inside a Docker container:
docker compose up --build tests