vancodx/php-data-validation
最新稳定版本:1.0.2
Composer 安装命令:
composer require --dev vancodx/php-data-validation
包简介
VanCodX PHP Data Validation
README 文档
README
This library contains a set of static methods for validation the type of variables and arguments of functions and methods. The main goal is to improve the quality of data validation and static analysis. Each validation method has "@phpstan-assert-if-true" 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
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-27