grifart/enum 问题修复 & 功能扩展

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

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

grifart/enum

Composer 安装命令:

composer require grifart/enum

包简介

Provides bullet proof enums with behaviours.

README 文档

README

Enumeration value object. Enumerate values and behaviour with type-safety.

Repositories gitlab.grifart.cz and github.com. pipeline status

Sponsored by grifart.com.

Do you like video 📺 instead of reading? Here is one in english and one in czech.

Introduction

Enums represent predefined set of values. The available values are defined statically by each enum class. Each value is represented by an instance of this class in a flyweight manner.

  • This enum allows you to add individual behaviour for every enum value (as in Java). This allows you to transform your switches/ifs into more readable composition. (see example bellow)
  • Checks enum annotations if phpdoc-declared methods are properly declared (will generate docblock for you when not specified or incorrect)
  • ===, == and usage of switches is supported
  • string or integer scalar keys are supported
  • Easily access scalar value of enum DayOfWeek::MONDAY()->toScalar() or (string) DayOfWeek::MONDAY()

Also includes:

  • It is type safe. By annotating your enumeration type, you are guaranteed that there will be no other values then you declared. function translateTo(DayOfWeek $day) { ...
  • You can get a list of all the possible values Enum::getAvailableValues()

Installation

composer require grifart/enum

This library uses semantic versioning 2.0. You can safely use ^ constrain in you composer.json.

Requirements

This library requires PHP 7.1 and later.

Project status & release process

While this library is still under development, it is well tested and should be stable enough to use in production environments.

The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), y is incremented.

When a breaking change is introduced, a new 0.x version cycle is always started.

It is therefore safe to lock your project to a given release cycle, such as 0.1.*.

If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.

Overview

Static methods

  • fromScalar() - returns enum instance (value) for given scalar
  • getAvailableValues() - returns all values for given type
  • provideInstances() - implement to return enum instances or automatically implemented by Grifart\Enum\AutoInstances trait.

Instance methods

  • toScalar() - return scalar value identifier
  • equals() - returns true if the same enum value is passed
  • scalarEquals() - returns true if passed scalar value is equal to current value

Simplest enumeration

/**
 * @method static DayOfWeek MONDAY()
 * @method static DayOfWeek TUESDAY()
 */
final class DayOfWeek extends \Grifart\Enum\Enum
{
    use Grifart\Enum\AutoInstances;
    private const MONDAY = 'monday';
    private const TUESDAY = 'tuesday';
}

$monday = DayOfWeek::MONDAY();
function process(DayOfWeek $day): void { /* ... */ }

Values with behaviour

This way conditions can be replaced by composition.

This example originally comes from loyalty program domain, continue to full code sample with context.

/**
 * @method static ExpirationType ASSIGNMENT()
 * @method static ExpirationType FIXED()
 */
abstract class ExpirationType extends \Grifart\Enum\Enum
{
	protected const ASSIGNMENT = 'assignment';
	protected const FIXED = 'fixed';

	abstract public function computeExpiration(Offer $offer): \DateTimeImmutable;

	protected static function provideInstances() : array {
		return [
			new class(self::ASSIGNMENT) extends ExpirationType {
				public function computeExpiration(Offer $offer): \DateTimeImmutable {
					return /* behaviour A */;
				}
			},
			new class(self::FIXED) extends ExpirationType {
				public function computeExpiration(Offer $offer): \DateTimeImmutable {
					return /* behaviour B */;
				}
			},
		];
	}
}

Migrating from class constants [source code]

This guide show how to migrate from classes with constants to \Grifart\Enum in few simple steps. Continue to example

Adding behaviour to values [source code]

This guide show how to slowly add behaviour to enum values. Step by step. Continue to example

Complex showcase: order lifecycle tracking [source code]

This example contains 5 ways of implementing order state. Continue to example.

Big thanks

More reading

grifart/enum 适用场景与选型建议

grifart/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 294.89k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2018 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 22
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-20