承接 brzez/php-magic-enum 相关项目开发

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

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

brzez/php-magic-enum

Composer 安装命令:

composer require brzez/php-magic-enum

包简介

README 文档

README

Easy to use & extend enum value objects for php

Overview

PHP lacks enums. Everyone usually just uses const values, which don't really provide type safety, and can create messy code.

This might be a solution to this problem. It provides a enum value object.

This magic enum can be instanced via magic methods (named the same as consts) which create new instances of the enum with value set to the consts value.

Example enum:

class OrderStatus extends \MagicEnum\MagicEnum
{
	const CREATED        = 1;
	const COMPLETE       = 2;
	const CANCELLED      = 3;
	const PROCESSING     = 4;

	// easy way to extend with the status (for labels/translations etc)
	public function getLabel()
	{
		$name = strtolower($this->getName());
		return "order_status.${name}";
	}
}

Usage:

// create new instance:
$status = OrderStatus::PROCESSING();
$status->getValue() // => 4

// type safety
public function setStatus(OrderStatus $status)
{
  $this->status = $status;
}
// safe setter (but less annoying)
public function setStatus($status)
{
  if($status instanceof OrderStatus){
    $this->status = $status;
  }else{
    $this->status = new OrderStatus($status); // this validates if $status is defined in OrderStatus const values
  }
}

Extras:

{# it's easy to extend the enums with some additional features for example: #}
{{ order.status.label|trans }}

todo:

  • Add to packagist
  • Write help for installation via composer
  • Write some more usage help

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固