承接 pulyaevskiy/enum 相关项目开发

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

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

pulyaevskiy/enum

最新稳定版本:v1.2.7

Composer 安装命令:

composer require pulyaevskiy/enum

包简介

Simple implementation of Enum type for PHP

关键字:

README 文档

README

Build Status

Enum component enables developers to define strict enumerated types based on standard PHP classes. Usage of this component is a little bit verbose but it allows you to get rid of constant validation of every enum field in your entities. This implementation is pretty much similar to all those you can find out there (including SplEnum) but pretends to be more accurate.

This is a fork from startuplabs/enum which is not maintained anymore.

Usage

1. Define an ancestor of the Enum class. For example:

<?php
namespace Acme\Example;

use Pulyaevskiy\Enum;

/**
 * @method static Shape triangle()
 * @method static Shape square()
 * @method static Shape pentagon()
 * @method static Shape hexagon()
 */
class Shape extends Enum 
{
    const TRIANGLE = 'triangle';
    const SQUARE = 'square';
    const PENTAGON = 'pentagon';
    const HEXAGON = 'hexagon';
}

2. Use it with type-hinting in your classes:

<?php
namespace Acme\Example;

class Model 
{
    /** @var Shape */
    private $shape;

    public function setShape(Shape $value)
    {
        // Here it is guaranteed that the $value has already been validated
        $this->shape = $value;
    }
}

3. Create new instances using static methods:

<?php
namespace Acme\Example;

class Application 
{
    public function changeShapeToSquare(Model $model)
    {
        // Enum class has __callStatic method that checks if there is constant with name of called function
        // and then create instance of Shape class with this value
        $shape = Shape::square();
        $model->setShape($shape);
    }
}

Contributors

  • Alexey Tihomirov
  • Anatoly Pulyaevskiy

License

This library is under MIT license. Please see the complete license in the LICENSE file provided with the library source code.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-14