承接 patchlevel/enum 相关项目开发

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

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

patchlevel/enum

最新稳定版本:1.0.0

Composer 安装命令:

composer require patchlevel/enum

包简介

Small lightweight library to create enum in PHP without SPLEnum and strict comparisons allowed

关键字:

README 文档

README

Mutation testing badge Type Coverage Latest Stable Version License

enum

Small lightweight library to create enum in PHP without SPLEnum and strict comparisons allowed.

dislcaimer

For project that are build on PHP >=8.1 please use the language build in Enums described in this rfc: https://wiki.php.net/rfc/enumerations This library should not be needed anymore and will not support PHP >8.1.

installation

composer require patchlevel/enum

declaration

First of all you have to define your enum. To do this, you have to inherit from the Enum class, create a few constants (the value must be unique and a string) and define methods that return an enum representation. Here is an example:

<?php

declare(strict_types=1);

namespace Patchlevel\Enum\Example;

use Patchlevel\Enum\Enum;

/**
 * @psalm-immutable
 */
final class Status extends Enum
{
    private const CREATED = 'created';
    private const PENDING = 'pending';
    private const RUNNING = 'running';
    private const COMPLETED = 'completed';

    public static function created(): self
    {
        return self::get(self::CREATED);
    }

    public static function pending(): self
    {
        return self::get(self::PENDING);
    }

    public static function running(): self
    {
        return self::get(self::RUNNING);
    }

    public static function completed(): self
    {
        return self::get(self::COMPLETED);
    }
}

self::get() ensures that exactly one instance of a representation really exists so that strict comparisons can be used without problems.

Alternatively, you can inherit the ExtendedEnum, which comes with a few conveniences, more about this under ExtendedEnum.

api

work with enum

<?php 

declare(strict_types=1);

namespace Patchlevel\Enum\Example;

$status = Status::completed();

if ($status === Status::completed()) {
    echo "That's working";
}

// or use as typehint

function isFinished(Status $status): bool {
    return $status === Status::completed();
}

echo isFinished($status) ? 'yes' : 'no'; 

// or with the new php8.0 match feature:

$message = match ($status) {
    Status::created() => 'Process created',
    Status::pending() => 'Process pending',
    Status::running() => 'Process running',
    Status::completed() => 'Process completed',
    default => 'unknown status',
};

echo $message; // Process completed

Strict comparisons are not a problem as it ensures that there is only one instance of a certain value.

fromString

You can create an enum from a string. It is checked internally whether the value is valid, otherwise an InvalidValue exception is thrown.

$status = Status::fromString('pending');

if ($status === Status::pending()) {
    echo 'it works';
}

toString

The other way around, an enum can also be converted back into a string.

$status::completed();
echo $status->toString(); // completed

equals

The Equals method is just a wrapper for $a === $b.

$status = Status::completed();
$status->equals(Status::pending()); // false

isValid

isValid can be used to check whether the transferred value is valid. The return value is a boolean.

Status::isValid('foo'); // false
Status::isValid('completed'); // true

values

You can also get all Enum instances.

$instances = Status::values();

foreach ($instances as $instance) {
    echo $instance->toString(); // completed, pending, ...
}

keys

Or all keys.

$keys = Status::keys();

foreach ($keys as $key) {
    echo $key; // completed, pending, ...
}

extended enum

Alternatively, it can also extend from ExtendedEnum. This implementation also provides other features mostly magic methods: __toString, __callStatic and implementing \JsonSerializable.

<?php

declare(strict_types=1);

namespace Patchlevel\Enum\Example;

use Patchlevel\Enum\ExtendedEnum;

/**
 * @psalm-immutable
 * @method static self CREATED()
 * @method static self PENDING()
 * @method static self RUNNING()
 * @method static self COMPLETED()
 */
final class Status extends ExtendedEnum
{
    private const CREATED = 'created';
    private const PENDING = 'pending';
    private const RUNNING = 'running';
    private const COMPLETED = 'completed';
}

__callStatic

With the magic method __callStatic it is possible to create an Enum instance based only on the constant names. So no extra method is needed to be defined. If the constant name doesn't exist in the enum a BadMethodCall exception is thrown.

$status = Status::CREATED();

__toString

Just the magic method implementation of toString.

$status = Status::CREATED();
echo (string)$status; // created

JsonSerializable

The ExtendedEnum already implements the method jsonSerialize from the interface \JsonSerializable. This means that \json_encode will automatically serialize the value in the right manner. Beware that \json_decode won't automatically decode it back into the enum. This job must be tackled manually. See this example:

$status = Status::CREATED();
echo json_encode($status, JSON_THROW_ON_ERROR); // "created"

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

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

它主要适用于以下技术方向: 「enum」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 19.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-12