2017 © Pedro Peláez
 

library enum

Simple implementation of Enum type for PHP

image

pulyaevskiy/enum

Simple implementation of Enum type for PHP

  • Friday, September 4, 2015
  • by pulyaevskiy
  • Repository
  • 1 Watchers
  • 2 Stars
  • 15,886 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 13 Versions
  • 3 % Grown

The README.md

Enum

Build Status, (*1)

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., (*2)

This is a fork from startuplabs/enum which is not maintained anymore., (*3)

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., (*4)

The Versions

04/09 2015

dev-master

9999999-dev https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

04/09 2015

v1.2.7

1.2.7.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

11/03 2015

v1.2.6

1.2.6.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

27/02 2015

v1.2.5

1.2.5.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

14/02 2015

v1.2.4

1.2.4.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

13/02 2015

v1.2.3

1.2.3.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

17/12 2014

v1.2.2

1.2.2.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

14/12 2014

v1.2.1

1.2.1.0 https://github.com/pulyaevskiy/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

enum

13/12 2014

v1.2.0

1.2.0.0 https://github.com/pulyaevsky/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

enum

13/12 2014

v1.1.0

1.1.0.0 https://github.com/pulyaevsky/Enum

Simple implementation of Enum type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

enum

30/04 2013

v1.0.2

1.0.2.0 https://github.com/StartupLabs/Enum

StartupLabs Enum Component

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

29/04 2013

v1.0.1

1.0.1.0 https://github.com/StartupLabs/Enum

StartupLabs Enum Component

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

28/04 2013

v1.0.0

1.0.0.0 https://github.com/StartupLabs/Enum

StartupLabs Enum Component

  Sources   Download

MIT

The Requires

  • php >=5.3.0