定制 commerceguys/enum 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

commerceguys/enum

最新稳定版本:v1.0

Composer 安装命令:

composer require commerceguys/enum

包简介

A PHP 5.4+ enumeration library.

README 文档

README

Build Status

A PHP 5.4+ enumeration library.

Class constants are frequently used to denote sets of allowed values. By grouping them in an enumeration class, we gain the ability to add helper methods, list all possible values and validate values against them.

A commerceguys/addressing example:

namespace CommerceGuys\Addressing\Enum;

use CommerceGuys\Enum\AbstractEnum;

/**
 * Enumerates available locality types.
 */
final class LocalityType extends AbstractEnum
{
    const CITY = 'city';
    const DISTRICT = 'district';

    // We can provide a getDefault() method here, or anything else.
}

LocalityType::getAll(); // ['CITY' => 'city', 'DISTRICT' => 'district']
LocalityType::getKey('city'); // 'CITY'
LocalityType::exists('city'); // true
LocalityType::assertExists('invalid value'); // InvalidArgumentException
LocalityType::assertAllExist(['district', 'invalid value']); // InvalidArgumentException

Meanwhile, on the AddressFormat:

// The AddressFormatInterface is now free of LOCALITY_TYPE_ constants.
class AdressFormat implements AddressFormatInterface
{
    public function setLocalityType($localityType)
    {
        LocalityType::assertExists($localityType);
        $this->localityType = $localityType;
    }
}

The reason why this library was made instead of reusing myclabs/php-enum was that we didn't want to allow enumerations to be instantiated.

统计信息

  • 总下载量: 1.9M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 97
  • 点击次数: 1
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 96
  • Watchers: 6
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-27