digitalion/laravel-makes

Collection of additional class creation commands for Artisan

1.0.14 2023-09-23 13:45 UTC

This package is auto-updated.

Last update: 2024-10-23 16:12:53 UTC


README

Packagist License Packagist Packagist Release Packagist Downloads Packagist Stars

Collection of additional class creation commands for Artisan

What It Does

This package expands on the basic artisan commands for creating classes.

Base stub

To customize the generation of Laravel classes, read the Laravel Documentation.

Installation

Require this package with composer. It is recommended to only require the package for development.

composer require digitalion/laravel-makes

Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

Usage

Once the package is installed, you'll have additional Artisan commands:

make:class

Create a new generic class in: App\Classes\

php artisan make:class DigitalionClass

make:enum

Create a new enum class in: App\Enums\

php artisan make:enum DigitalionEnum

Enums implement the trait Digitalion\LaravelMakes\Traits\EnumSerializableTrait that adds useful methods for using enums. View the enum methods

make:helper

Create a new helper class in: App\Helpers\

php artisan make:helper DigitalionHelper

make:interface

Create a new query scope class in: App\Interfaces\

php artisan make:interface DigitalionInterface

make:scope

Create a new query scope class in: App\Scopes\

php artisan make:scope DigitalionScope

make:service

Create a new query scope class in: App\Services\

php artisan make:service DigitalionService

make:trait

Create a new trait class in: App\Traits\

php artisan make:trait DigitalionTrait

Enum methods

Get the array options

$options = DigitalionEnum::options();

// dump $options
[
	'key1'	=> 'value1',
	'key2'	=> 'value2',
	'key3'	=> 'value3',
]

Get the array values

$values = DigitalionEnum::values();

// dump $values
[
	'value1',
	'value2',
	'value3',
]

Get the array keys

$keys = DigitalionEnum::keys();

// dump $keys
[
	'key1',
	'key2',
	'key3',
]

Get the string values

This method returns a string with all the values of the enum, divided by the comma. You may find this useful if you use it in Laravel's validation rules such as in:.

$valuesString = DigitalionEnum::valuesImplode();

// dump $valuesString
'value1,value2,value3'

Get the regex values

This method returns a string with regex to validate the Enum values. You may find this useful by using it in routes to accept only its values in parameters (Laravel Docs - Regular Expression Constraints).

$valuesRegex = DigitalionEnum::valuesRegex();

// dump $valuesRegex
'(value1|value2|value3)$'

Get the translated array

By passing the translation key prefix of the enum values, you will get an array with all the translated keys and values.

File: resources/lang/en/enums.php

return [
    'digitalion' => [
        'key1' => 'Value 1',
        'key2' => 'Value 2',
        'key3' => 'Value 3',
    ],
];

Use:

$trans = DigitalionEnum::trans('enums.digitalion');

// dump $trans
[
	'key1'	=> 'Value 1',
	'key2'	=> 'Value 2',
	'key3'	=> 'Value 3',
]

License

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