lucastuzina/laranums 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lucastuzina/laranums

Composer 安装命令:

composer require lucastuzina/laranums

包简介

Provides a trait with useful utility methods for enums in Laravel

README 文档

README

Laranums is a Laravel package that turns PHP enums into a first-class, fully-featured domain primitive. One trait unlocks lookup, conversion, translations, attribute-driven UI metadata, navigation, filtering, comparison, ordering and more.

Packagist Downloads Version License

Installation

composer require lucastuzina/laranums

Quickstart

Drop the Laranum trait into any enum:

use Lucastuzina\Laranums\Laranum;

enum Status: string
{
    use Laranum;

    case Active   = 'active';
    case Inactive = 'inactive';
    case Pending  = 'pending';
}

Status::fromValue('active');             // Status::Active
Status::toSelectOptions();               // ['active' => 'Active', 'inactive' => 'Inactive', …]
Status::Active->next();                  // Status::Inactive
Status::Active->in([Status::Pending]);   // false

Documentation

Full docs live in the docs/ directory:

  • Case Attributes#[Label], #[Color], #[Icon], #[Description] plus label(), color(), icon(), description(), toSelectOptions().
  • Trait Methods — Every method the trait adds, organised by concern.
  • Utility Enums — Ready-to-use enums (Unit, HttpStatus, Day, Currency, Country, Language, …).
  • Laravel Integration — Validation rules, Faker provider, Eloquent query macros.
  • Artisan Command — Scaffold new enums with php artisan make:laranum.

Feature Overview

Case Attributes

Attach UI metadata directly to cases — no match statements, no framework lock-in.

use Lucastuzina\Laranums\Attributes\{Label, Color, Icon};

enum OrderStatus: string
{
    use Laranum;

    #[Label('Pending'), Color('yellow'), Icon('clock')]
    case PENDING = 'pending';

    #[Label('Paid'), Color('green'), Icon('check-circle')]
    case PAID = 'paid';
}

OrderStatus::PAID->color();   // 'green'
OrderStatus::toSelectOptions(); // ['pending' => 'Pending', 'paid' => 'Paid']

Full details

Method Cheatsheet

Concern Methods
Lookup fromName, fromValue, fromNameOrDefault, fromValueOrDefault, isValidName, isValidValue
Metadata className
Conversion toArray, toArrayReversed, names, values, collect
Translations trans, transNames, transValues
Attributes label, color, icon, description, toSelectOptions
Navigation first, last, count, at, index, next, previous
Filtering only, except, rand, random
Comparison is, in, notIn
Ordering compare, lessThan, greaterThan, between
JSON jsonSerialize
Validation rule, ruleOnly, ruleExcept

Full reference

Utility Enums

Ready-to-use, domain-neutral enums that ship with the full Laranum trait baked in.

Enum Purpose
Unit Length, weight, time, speed, temperature, … with built-in conversion.
HttpStatus IANA status codes with category helpers (isSuccess, isClientError, …).
HttpMethod isSafe, isIdempotent, hasRequestBody per RFC 7231.
Day ISO-8601 weekdays with isWeekday, isWeekend, short.
Month Leap-year-aware daysInMonth, quarter, short.
Quarter months(), firstMonth(), lastMonth().
Season Meteorological seasons with hemisphere support.
CardinalDirection 8-point compass with degrees, opposite, fromDegrees.
Continent Seven-continent model with ISO codes.
Currency ISO 4217 (~150 codes) with symbol, decimals, format.
Country All 249 ISO 3166-1 countries with alpha3, numeric, dialCode, continent, flag.
Language ISO 639-1 (~180 codes) with nativeName, isRtl.

Full details

Laravel Integration

  • Validation: OrderStatus::rule(), ::ruleOnly([...]), ::ruleExcept([...]) — returns Laravel's native Enum rule, drop straight into a FormRequest.
  • Faker: $faker->enum(OrderStatus::class) via LaranumFakerProvider.
  • Eloquent: Order::whereEnum('status', OrderStatus::PAID) and whereEnumNot, orWhereEnum macros — accept enum instances or arrays directly.

Full details

Artisan Command

php artisan make:laranum UserRole admin editor guest --type=string --lang=en,de

Scaffolds the enum and optional translation stubs under lang/{locale}/enums.php.

Full details

À la carte

If you don't want the whole trait, compose your own from the concerns under Lucastuzina\Laranums\Concerns\*:

use Lucastuzina\Laranums\Concerns\{HasLookup, HasAttributes};

enum MyEnum: string
{
    use HasLookup, HasAttributes;
    // ...
}

License

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

lucastuzina/laranums 适用场景与选型建议

lucastuzina/laranums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lucastuzina/laranums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lucastuzina/laranums 我们能提供哪些服务?
定制开发 / 二次开发

基于 lucastuzina/laranums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-26