PHPackages                             hegentopf/php-assert - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. hegentopf/php-assert

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

hegentopf/php-assert
====================

A lightweight and fluent PHP assertion library for easy and expressive validations.

v1.0.3(1y ago)3111MITPHPPHP &gt;=5.6CI passing

Since May 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/hegentopf/php-assert)[ Packagist](https://packagist.org/packages/hegentopf/php-assert)[ RSS](/packages/hegentopf-php-assert/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (4)Used By (0)

php-assert
==========

[](#php-assert)

A lightweight and fluent PHP assertion library for easy and expressive validations.

---

Overview
--------

[](#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
--------

[](#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 `stdClass` objects seamlessly

---

Installation
------------

[](#installation)

Install via Composer:

```
composer require hegentopf/php-assert
```

---

Usage
-----

[](#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
--------------------

[](#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()`
--------------------------------------------------------------------

[](#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 `$testPrivateProperties` is set to `false`.

### Examples:

[](#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
-------

[](#testing)

Unit tests are provided and use PHPUnit.

To run tests:

```
composer install
./vendor/bin/phpunit
```

---

License
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

---

Contribution
------------

[](#contribution)

Contributions, issues, and feature requests are welcome! Feel free to open a pull request or issue.

---

Maintainer
----------

[](#maintainer)

Michael Helmert

---

Thank you for using **php-assert**!

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance46

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 80% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

403d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/113784801?v=4)[hegentopf](/maintainers/hegentopf)[@hegentopf](https://github.com/hegentopf)

---

Top Contributors

[![hegentopf](https://avatars.githubusercontent.com/u/113784801?v=4)](https://github.com/hegentopf "hegentopf (12 commits)")[![LukasFre](https://avatars.githubusercontent.com/u/135229216?v=4)](https://github.com/LukasFre "LukasFre (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hegentopf-php-assert/health.svg)

```
[![Health](https://phpackages.com/badges/hegentopf-php-assert/health.svg)](https://phpackages.com/packages/hegentopf-php-assert)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
