finagin/laravel-extra-support

Extra Support methods for Laravel

0.1.0 2021-07-15 16:25 UTC

This package is auto-updated.

Last update: 2024-05-15 22:53:49 UTC


README

StyleCI GitHub Actions GitHub Issues Total Downloads Latest Stable Version Software License

Installation

composer require finagin/laravel-extra-support

Usage

Str::randomWithExclude()

Returns random string with excludes.

use Illuminate\Support\Str;

Str::randomWithExclude();
Str::randomWithExclude(15);
Str::randomWithExclude(16, ['a', 'b', 'c']);
Str::randomWithExclude(16, 'abc');

Str::randomAlpha()

Returns random string without numeric.

use Illuminate\Support\Str;

Str::randomAlpha();
Str::randomAlpha(15);

Customization

<?php

namespace App\Services;

use Finagin\ExtraSupport\Services\MacrosRegistrar;
use Illuminate\Support\Str;

class CustomMacrosRegistrar extends MacrosRegistrar
{
    /**
     * @return \Illuminate\Support\Collection|array
     */
    public function additionalRegisters()
    {
        return [
            '\\Illuminate\\Support\\Str@randomExcludeSimilar' => 'registerMacroRandomExcludeSimilar',
        ];
    }

    protected function registerMacroRandomExcludeSimilar()
    {
        Str::macro('randomExcludeSimilar', static function ($length = 16) {
            return Str::randomWithExclude($length, ['1', 'l', '0', 'O']);
        });
    }
}

In config replace registrar with new registrar class:

<?php

return [

    'registrar' => \App\Services\CustomMacrosRegistrar::class,
    
    // ...
];

Finally, add dependencies to the appropriate section if you need them:

<?php

return [
    // ...
    'dependencies' => [
        // ...
        '\\Illuminate\\Support\\Str@randomExcludeSimilar' => [
            '\\Illuminate\\Support\\Str@randomWithExclude',
        ],
        // ...
    ],
    // ...
];

License

The MIT License (MIT). Please see License File for more information.