Download the PHP package hegentopf/php-assert without Composer
On this page you can find all versions of the php package hegentopf/php-assert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hegentopf/php-assert
More information about hegentopf/php-assert
Files in hegentopf/php-assert
Package php-assert
Short Description A lightweight and fluent PHP assertion library for easy and expressive validations.
License MIT
Informations about the package php-assert
php-assert
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:
Usage
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:
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:
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!
All versions of php-assert with dependencies
ext-json Version *