peltonsolutions/laravel-enums 问题修复 & 功能扩展

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

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

peltonsolutions/laravel-enums

最新稳定版本:1.1.0

Composer 安装命令:

composer require peltonsolutions/laravel-enums

包简介

A custom package for Laravel for handling enum values on Models.

README 文档

README

laravel-enums is a package by Pelton Solutions that allows Laravel developers to work easily with enumerated values. Enums (short for 'enumerations') represent a set of named constants in your code. By introducing enums, this package significantly enhances the readability and maintainability of your code.

In addition, you can optionally make your Enum fields nullable. To use this feature, simply have your Enum class extend the NullableEnum class.

Example:

class ContentPageStatus extends \PeltonSolutions\LaravelEnums\Models\Enum
{
	const DRAFT = 'draft';
	const SCHEDULED = 'scheduled';
	const PUBLISHED = 'published';
	const ARCHIVED = 'archived';

	public static function map(): array
	{
		return [
			static::DRAFT => trans('content_page.statuses.draft'),
			static::SCHEDULED => trans('content_page.statuses.scheduled'),
			static::PUBLISHED => trans('content_page.statuses.published'),
			static::ARCHIVED => trans('content_page.statuses.archived'),
		];
	}
}
class ContentPage extends Model
{
	protected $casts = [
		'status' => ContentPageStatus::class
	];
}

These examples demonstrate how you could use enumerations in a Laravel model. ContentPageStatus is an enumeration that represents the possible status values that a ContentPage could have. The ContentPage model includes a casting to this enumeration, enabling more robust input validation and a more explicit declaration of the possible 'status' values. The package ensures value validation by throwing a \PeltonSolutions\LaravelEnums\Models\Exceptions\InvalidEnumValueException exception if you try to set a value that is not in the specified enumeration.

Install

You can install the package via composer using the following command:

composer require peltonsolutions/laravel-enums

Testing

To ensure that laravel-enums is functioning correctly, you can run the package's tests using:

composer test

Security

If you discover any security-related issues, please email security@peltonsolutions.com instead of using the issue tracker.

Credits

License

laravel-enums is open-sourced software. It's licensed under the MIT license, which is a permissive license allowing the software to be used, modified, and shared.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-05