New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

mll-lab/php-utils

Shared PHP utility functions of MLL
62,795 2
Install
composer require mll-lab/php-utils
Latest Version:v7.4.0
PHP:^7.4 || ^8
Laravel:8 – 12
License:MIT
Last Updated:Nov 18, 2026
Links: GitHub  ·  Packagist
Maintainer: spawnia

php-utils

.github/workflows/format.yml .github/workflows/release.yml .github/workflows/validate.yml Code Coverage

Latest Stable Version Total Downloads

Shared PHP utility functions of MLL

Installation

Install through composer

composer require mll-lab/php-utils

Usage

See tests.

SafeCast

PHP's native type casts like (int) and (float) can produce unexpected results, especially when casting from strings. The SafeCast utility provides safe alternatives that validate input before casting:

Each type has two variants:

  • toX(): returns the cast value or throws \InvalidArgumentException
  • tryX(): returns the cast value or null (like Enum::tryFrom())
use MLL\Utils\SafeCast;

// Safe integer casting
SafeCast::toInt(42);           // 42
SafeCast::toInt('42');         // 42
SafeCast::toInt('hello');      // throws InvalidArgumentException
SafeCast::tryInt('hello');     // null

// Safe float casting
SafeCast::toFloat(3.14);       // 3.14
SafeCast::toFloat('3.14');     // 3.14
SafeCast::toFloat('abc');      // throws InvalidArgumentException
SafeCast::tryFloat('abc');     // null

// Safe string casting
SafeCast::toString(42);        // '42'
SafeCast::toString(null);      // ''
SafeCast::tryString([1, 2]);   // null

// Safe boolean casting
SafeCast::toBool(true);        // true
SafeCast::toBool(1);           // true
SafeCast::toBool('0');         // false
SafeCast::toBool('true');      // throws InvalidArgumentException
SafeCast::tryBool('true');     // null

See tests for more examples.

Holidays

You can add custom holidays by registering a method that returns a map of holidays for a given year. Set this up in a central place that always runs before your application, e.g. a bootstrap method.

use MLL\Holidays\BavarianHolidays;

BavarianHolidays::$loadUserDefinedHolidays = static function (int $year): array {
    switch ($year) {
        case 2019:
            return ['22.03' => 'Day of the Tentacle'];
        default:
            return [];
    }
};

Custom holidays have precedence over the holidays inherent to this library.

Asserting iterable values

Use the trait AssertsIterableValues in a PHPUnit test case to compare the values of two iterables in order, ignoring their keys, like assertArraysHaveIdenticalValues() from PHPUnit 13 does for arrays. This saves converting a Collection, a generator or a filtered array with ->values()->all() or array_values() before assertSame().

use MLL\Utils\PHPUnit\AssertsIterableValues;
use PHPUnit\Framework\TestCase;

final class FooTest extends TestCase
{
    use AssertsIterableValues;

    public function testFoo(): void
    {
        self::assertIterablesHaveIdenticalValues(['a', 'b'], $collection->filter()->unique());
    }
}

Requires phpunit/phpunit.

PHPStan extension

This library provides a PHPStan extension that is either registered through PHPStan Extension Installer or registered manually by adding the following to your phpstan.neon:

includes:
+- vendor/mll-lab/php-utils/extension.neon
+- vendor/mll-lab/php-utils/rules.neon

Requires spaze/phpstan-disallowed-calls.

Changelog

See CHANGELOG.md.

Contributing

See CONTRIBUTING.md.

License

This package is licensed using the MIT License.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

660,512,042 9,702
laravel/framework

The Laravel Framework.

625,568,548 34,998
laravel/tinker

Powerful REPL for the Laravel framework.

531,591,559 7,442
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

450,320,528 608
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

424,642,029 4,662

Version History

Version Released PHP Laravel License
v7.4.0 ^7.4 || ^8 ^8.73 || ^9 || ^10 || ^11 || ^12 MIT
v7.3.0 ^7.4 || ^8 ^8.73 || ^9 || ^10 || ^11 || ^12 MIT
v7.2.0 ^7.4 || ^8 ^8.73 || ^9 || ^10 || ^11 || ^12 MIT
v7.1.0 ^7.4 || ^8 ^8.73 || ^9 || ^10 || ^11 || ^12 MIT
v7.0.1 ^7.4 || ^8 ^8.73 || ^9 || ^10 || ^11 || ^12 MIT