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

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

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

vcn/enum

最新稳定版本:v1.5.2

Composer 安装命令:

composer require vcn/enum

包简介

[Deprecated] Enumeration data types with pattern match operations. This package is obsolete because PHP has native enums now.

README 文档

README

Enumeration data types with pattern match operations.

Quickstart

composer require vcn/enum

<?php use Vcn\Lib\Enum; /**  * @method static Fruit APPLE()  * @method static Fruit BANANA()  */ class Fruit extends Enum { protected const APPLE = 0; protected const BANANA = 0; } Fruit::APPLE()->equals(Fruit::BANANA()); // false Fruit::APPLE()->equals(Fruit::APPLE()); // true Fruit::APPLE() ->when(Fruit::APPLE(), 'apple') ->when(Fruit::BANANA(), 'banana') ->get(); // 'apple'

Motivation

An enum (or enumeration), is a data construct describing a value that can be exactly one of a distinct, predefined set of values.

We could for instance model fruit - where we only consider apples and bananas as fruit - as follows (using ADTs):

data Fruit = Apple | Banana

A fruit is either an apple or banana - not both, nor neither.

Here Fruit is the enumerable type and Apple and Banana are its labels (its possible values, or instances).

This implementation tries to come close to the expressive power above.

Usage

data Fruit = Apple | Banana

To express the above using this implementation, firstly extend Enum to define Fruit:

<?php final class Fruit extends Enum {}

(Making the class final is recommended, as further inheritance is not supported and will produce warnings.)

Then define the labels as constant members of Fruit:

<?php final class Fruit extends Enum { protected const APPLE = 0; protected const BANANA = 0; }

(The constant values (0s) are meaningless, but required by PHP.)

This will expose the labels as magic static methods Fruit::APPLE() and Fruit::BANANA(). They serve as the constructors of the corresponding labels. It is recommended to annotate them as class members in the docblock:

<?php /**  * @method static Fruit APPLE()  * @method static Fruit BANANA()  */ final class Fruit extends Enum { protected const APPLE = 0; protected const BANANA = 0; }

Now you can instantiate either label:

<?php $banana = Fruit::APPLE(); $apple = Fruit::BANANA();

You can test for equality:

<?php Fruit::APPLE()->equals(Fruit::BANANA()); // false Fruit::APPLE()->equals(Fruit::APPLE()); // true

You can pattern match on labels:

<?php Fruit::APPLE() ->when(Fruit::APPLE(), 'apple') ->when(Fruit::BANANA(), 'banana') ->get(); // 'apple'

You can stringify and unstringify label names:

<?php Fruit::APPLE()->getName(); // 'APPLE' Fruit::byName('APPLE'); // Fruit::APPLE()

You can check a collection for exhaustiveness:

<?php Fruit::isExhaustive([Fruit::APPLE()]); // false Fruit::isExhaustive([ Fruit::APPLE(), Fruit::BANANA() ]); // true

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固