承接 dilovanmatini/laravel-enumable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dilovanmatini/laravel-enumable

最新稳定版本:v1.0.2

Composer 安装命令:

composer require dilovanmatini/laravel-enumable

包简介

The Laravel Enumable trait provides a set of utility methods to enhance PHP enums. It includes methods for manipulating enum cases, retrieving labels, and converting enums to various formats.

README 文档

README

Latest Version on Packagist

Overview

The Laravel Enumable trait provides a set of utility methods to enhance PHP enums in Laravel. It includes methods for manipulating enum cases, retrieving labels, and converting enums to various formats.

Installation

To install the Laravel Enumable trait, add it to your project via Composer:

composer require dilovanmatini/laravel-enumable

Requirements

  • PHP 8.1 or higher
  • Laravel 10.0 or higher

Usage

Include the Enumable trait in your enum classes:

use DilovanMatini\Enumable\Traits\Enumable;

enum YourEnum: string
{
    use Enumable;

    case Example = 'example';
    // Add your cases here
}

Methods

Class Methods

  • values(): Returns an array of all enum values.
  • names(): Returns an array of all enum names.
  • labels(): Converts the enum to an associative array with values as keys and labels as values.
  • getCase(string $value): Returns the enum case for the given value.
  • getName(string $value): Returns the name of the enum case for the given value.
  • getLabel(string $value): Returns the label of the enum case for the given value.
  • toArray(): Converts the enum to an associative array with values as keys and names as values.
  • toCollection(): Converts the enum to a Laravel collection.
  • toSelectArray(): Alias for toLabelsArray().
  • exists(object|string $value): Checks if the given value exists in the enum.
  • random(): Returns a random enum case.
  • default(): Returns the default enum case.
  • first(): Returns the first enum case.
  • last(): Returns the last enum case.
  • count(): Returns the number of enum cases.
  • only(array $cases): Returns a new object with only the specified cases.
  • except(array $cases): Returns a new object with all cases except the specified ones.
  • generate(array $cases): Generates a new object with the given cases.

Instance (Object) Methods

  • label(): Returns the label of the enum case.
  • headline(): Returns the headline of the enum case.
  • str(bool $label = false): Returns a new object to apply Laravel string helper methods.

Examples

// Class methods
YourEnum::values(); // ['example']
YourEnum::names(); // ['Example']
YourEnum::labels(); // ['example' => 'Example']
YourEnum::getCase('example'); // YourEnum::Example
YourEnum::getName('example'); // 'Example'
YourEnum::getLabel('example'); // 'Example'
YourEnum::toArray(); // ['example' => 'Example']
YourEnum::toCollection(); // Collection {#1 ▼
    #items: App\Enums\YourEnum [▼
        "name" => "Example",
        "value" => "example",
    ]
}
YourEnum::toSelectArray(); // ['example' => 'Example']
YourEnum::exists('example'); // true
YourEnum::random(); // YourEnum::Example
YourEnum::default(); // YourEnum::Example
YourEnum::first(); // YourEnum::Example
YourEnum::last(); // YourEnum::Example
YourEnum::count(); // 1
YourEnum::only(['Example']); // YourEnum::Example
YourEnum::except(['Example']); // YourEnum::Example
YourEnum::generate(['Example']); // YourEnum::Example

// Instance methods
YourEnum::Example->label(); // 'Example'
YourEnum::Example->headline(); // 'Example'
YourEnum::Example->str()->camel(); // 'example'
YourEnum::Example->str()->plural(); // 'Examples'

Custom Labels

Override the setLabels method in your enum class to provide custom labels:

public static function setLabels(): array
{
    return [
        self::Example->value => 'Example Custom Label',
    ];
}

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 2.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-10