hegentopf/php-assert
Composer 安装命令:
composer require hegentopf/php-assert
包简介
A lightweight and fluent PHP assertion library for easy and expressive validations.
README 文档
README
A lightweight and fluent PHP assertion library for easy and expressive validations.
Overview
php-assert is a PHP assertion utility designed to provide an elegant and chainable interface for validating data types, values, and object keys. It supports a variety of common assertions such as type checks, range checks, string validations, date comparisons, and more.
The library throws AssertException on failed assertions, allowing you to handle validation errors cleanly.
Features
- Fluent and chainable assertion syntax
- Assertions for types: int, float, bool, string, array, object, null, callable, etc.
- Value comparisons: greater than, less than, between, equality, etc.
- String validations: length, regex, contains, startsWith, endsWith, email
- Date validations: isDate, date range checks
- Optional keys support for objects and arrays
- Strict and non-strict checks
- Works with PHP arrays and
stdClassobjects seamlessly
Installation
Install via Composer:
composer require hegentopf/php-assert
Usage
use Hegentopf\Assert\Assert; use Hegentopf\Assert\AssertException; // Simple assertion Assert::that(42)->isInt()->isGreaterThan(10); // Assert key in array or object Assert::thatKey(['foo' => 'bar'], 'foo')->isString()->hasMinLength(3); // Optional key validation (passes if key is missing or null) Assert::thatKey($obj, 'optionalDate')->isOptional()->isDate();
Available Assertions
The following assertions are currently implemented and chainable:
isInt(),isStrictInt()isFloat(),isStrictFloat()isNumeric()isGreaterThan($min),isGreaterThanOrEqual($min)isLessThan($max),isLessThanOrEqual($max)isBetween($min, $max, $inclusive = true)isBool(),isStrictBool()isJson()isBase64()isObject()isArray()isNotInArray(array $haystack, $strict = false)isInArray(array $haystack, $strict = false)isCallable()isNull()isEqualTo($comparison)isNotEqualTo($comparison)isSameAs($comparison)isNotSameAs($comparison)isString(),isStrictString()hasLength()hasMinLength($minLength)hasMaxLength($maxLength)hasLengthBetween($minLength, $maxLength)matchesRegex($pattern)startsWith($prefix, $caseSensitive = false)endsWith($suffix, $caseSensitive = false)contains($needle, $caseSensitive = false)isEmail()isDate()isDateGreaterThan($date)isDateGreaterThanOrEqual($date)isDateLessThan($date)isDateLessThanOrEqual($date)isDateBetween($minDate, $maxDate, $inclusive = true)hasArrayLengthAssertion( $length )hasMinArrayLength( $minLength )hasMaxArrayLength( $maxLength )hasArrayLengthBetween( $minLength, $maxLength )
Array and Object Loop Assertions with each() and eachRecursive()
The each() method allows you to apply assertions to every element of an array or every property of an object (only the first level).
The eachRecursive() method applies assertions to every element of an array or every property of an object recursively, including all nested arrays and objects.
This is especially useful when you want to ensure that all values in a (possibly nested) array or object structure meet a specific type or condition.
By default, all properties of an object are validated — including private, protected, and public ones.
If you want to restrict validation to public properties only, you can pass false to the optional $testPrivateProperties parameter.
Note:
Protected properties are treated the same as private ones — they are included by default and excluded only if$testPrivatePropertiesis set tofalse.
Examples:
use Hegentopf\Assert\Assert; // Check if all values in the array are floats (first level only) Assert::that([3.14, 2.71])->isArray()->each()->isStrictFloat(); // Works with associative arrays as well Assert::that(['a' => 1, 'b' => 2])->isArray()->each()->isInt(); // Validate nested arrays: check that each element is an array (first level) Assert::that([[1, 2], [3, 4]])->isArray()->each()->isArray(); // Recursively check that all values in nested arrays are integers Assert::that([[1, 2], [3, 4]])->isArray()->eachRecursive()->isInt(); // Optional validation for each element (first level) Assert::that([null, 3.14, 2.71])->isArray()->each()->isOptional()->isStrictFloat(); // Optional validation for all nested elements Assert::that([[null, 3.14], [2.71, null]])->isArray()->eachRecursive()->isOptional()->isStrictFloat(); // Check all properties (public, protected, and private) of an object class Example { private int $a = 1; protected int $b = 2; public int $c = 3; } $obj = new Example(); Assert::that($obj)->each()->isInt(); // all properties are checked Assert::that($obj)->each(false)->isInt(); // only public properties ($c) // Recursively check values in nested arrays and objects $obj1 = new stdClass(); $obj1->value1 = [1]; $obj1->value2 = 2; $obj1->value3 = [[2]]; Assert::that([[$obj1]])->eachRecursive()->isInt();
With each() and eachRecursive(), you can chain any assertions and they will be applied to every element in the array or every property in the object (either only the first level or recursively). Optional checks (isOptional()) are correctly propagated to nested arrays and objects.
Testing
Unit tests are provided and use PHPUnit.
To run tests:
composer install ./vendor/bin/phpunit
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contribution
Contributions, issues, and feature requests are welcome! Feel free to open a pull request or issue.
Maintainer
Michael Helmert
Thank you for using php-assert!
hegentopf/php-assert 适用场景与选型建议
hegentopf/php-assert 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hegentopf/php-assert 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hegentopf/php-assert 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-20