pepperfm/macros-for-laravel

A small macro registry for Laravel facades / macroable classes.

Installs: 41

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/pepperfm/macros-for-laravel

0.0.4 2026-01-20 19:58 UTC

This package is auto-updated.

Last update: 2026-01-20 19:59:03 UTC


README

Small macro registry for Laravel facades and Macroable classes. It lets you enable macro groups via config and register everything automatically at boot.

Install

composer r pepperfm/macros-for-laravel

Laravel auto-discovers the provider: Pepperfm\LaravelMacros\Providers\LaravelMacrosServiceProvider.

Publish config

php artisan vendor:publish --tag=macros-for-laravel-config

config/macros-for-laravel.php:

return [
    'enabled' => env('MACROS_ENABLED', true),
    'profile' => env('MACROS_PROFILE', 'default'),
    'conflicts' => 'throw', // throw | overwrite
    'unreachable' => 'throw', // throw | skip
    'profiles' => [
        'default' => [
            \Pepperfm\LaravelMacros\Groups\Support\ArrCastMacros::class => true,
            \Pepperfm\LaravelMacros\Groups\Support\ArrNativeMacros::class => false,
            \Pepperfm\LaravelMacros\Groups\Support\CollectionMacros::class => true,
        ],
        // 'http' => [
        //     \Pepperfm\LaravelMacros\Groups\Facades\ResponseMacros::class => true,
        // ],
    ],
];

Switch profiles via env:

MACROS_PROFILE=http

You can also use the legacy top-level groups list (no profiles):

'groups' => [
    \Pepperfm\LaravelMacros\Groups\Support\ArrCastMacros::class => true,
];

Built-in macros

Arr cast helpers

Available when ArrCastMacros is enabled:

Arr::bool($array, 'flag');
Arr::int($array, 'count');
Arr::toFloat($array, 'ratio');
Arr::toString($array, 'name', null, true);
Arr::toArray($array, 'items');
Arr::toEnum($array, 'status', Status::class, $default = null);

Arr native array helpers

Available when ArrNativeMacros is enabled:

Arr::values($array);
Arr::keys($array);
Arr::keyFirst($array);
Arr::keyLast($array);
Arr::flip($array);
Arr::combine(['a', 'b'], [1, 2]);
Arr::unique(['a', 'a', 'b']);
Arr::reverse([1, 2, 3]);

Collection paginate

Available when CollectionMacros is enabled:

collect([1, 2, 3])->paginate(2);

Collection filters

Available when CollectionMacros is enabled:

collect([1, null, 2])->filterNotNull();
collect(['', ' ', 'ok', null])->filterNotBlank();

Custom groups

Create a group that implements Pepperfm\LaravelMacros\Contracts\MacroGroupContract, then add it to a profile (or to groups in legacy mode). It will be resolved via the container.