1. Go to this page and download the library: Download hegentopf/php-assert library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hegentopf / php-assert example snippets
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();
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();
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.