2017 © Pedro Peláez
 

library php-enum

PHP 5.4+ Enum implementation

image

vjik/php-enum

PHP 5.4+ Enum implementation

  • Monday, February 26, 2018
  • by vjik
  • Repository
  • 1 Watchers
  • 3 Stars
  • 41 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 11 % Grown

The README.md

PHP Enum Implementation

Latest Stable Version Total Downloads Build status Mutation testing badge static analysis, (*1)

The package implement ideas from RFC Enumerations and provide abstract class Enum that intended to create enumerated objects with support extra data and auxiliary static functions values(), cases() and isValid()., (*2)

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with composer:, (*3)

composer require vjik/php-enum --prefer-dist

General usage

Declaration of class

use Vjik\Enum\Enum;

/**
 * @method static self NEW()
 * @method static self PROCESS()
 * @method static self DONE()
 */
final class Status extends Enum
{
    private const NEW = 'new';
    private const PROCESS = 'process';
    private const DONE = 'done';
}

Creating an object

By static method from()

$process = Status::from('process');

On create object with invalid value throws ValueError., (*4)

By static method tryFrom()

$process = Status::tryFrom('process'); // Status object with value "process"
$process = Status::tryFrom('not-exists'); // null

On create object with invalid value returns null., (*5)

By static method with a name identical to the constant name

Static methods are automatically implemented to provide quick access to an enum value., (*6)

$process = Status::PROCESS();

Getting value and name

Status::DONE()->getName(); // DONE
Status::DONE()->getValue(); // done

Class with extra data

Set data in the protected static function data() and create getters using the protected method getPropertyValue(). Also you can create getter using protected method match()., (*7)

use Vjik\Enum\Enum;

/**
 * @method static self CREATE()
 * @method static self UPDATE()
 */
final class Action extends Enum
{
    private const CREATE = 1;
    private const UPDATE = 2;

    protected static function data(): array
    {
        return [
            self::CREATE => [
                'tip' => 'Create document',
            ],
            self::UPDATE => [
                'tip' => 'Update document',
            ],
        ];
    }

    public function getTip(): string
    {
        /** @var string */
        return $this->getPropertyValue('tip');
    }

    public function getColor(): string
    {
        return $this->match([
            self::CREATE => 'red',
            self::UPDATE => 'blue',
        ]);
    }

    public function getCode(): int
    {
        return $this->match([
            self::CREATE => 1,
        ], 99);
    }
}

Usage:, (*8)

echo Action::CREATE()->getTip();
echo Action::CREATE()->getColor();
echo Action::CREATE()->getCode();

Auxiliary static functions

List of values values()

Returns list of values., (*9)

// [1, 2]
Action::values(); 

List of objects cases()

Returns list of objects:, (*10)

// [$createObject, $updateObject]
Action::cases();

Validate value isValid()

Check if value is valid on the enum set., (*11)

Action::isValid(1); // true
Action::isValid(99); // false

Casting to string

Enum support casting to string (using magic method __toString). The value is returned as a string., (*12)

echo Status::DONE(); // done

Testing

Unit testing

The package is tested with PHPUnit. To run tests:, (*13)

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework. To run it:, (*14)

./vendor/bin/infection

Static analysis

The code is statically analyzed with Psalm. To run static analysis:, (*15)

./vendor/bin/psalm

License

The PHP Enum implementation is free software. It is released under the terms of the BSD License. Please see LICENSE for more information., (*16)

Credits

Version 3 of this package is inspired by myclabs/php-enum., (*17)

The Versions

26/02 2018

dev-master

9999999-dev

PHP 5.4+ Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum

26/02 2018

2.1.0

2.1.0.0

PHP 5.4+ Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum

04/09 2017

2.0.0

2.0.0.0

PHP 5.4+ Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum

29/08 2017

1.2.0

1.2.0.0

PHP 5.4+ Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum

29/08 2017

1.1.1

1.1.1.0

PHP 5.4+ Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum

21/08 2017

1.1.0

1.1.0.0

PHP 5.4+ Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum

14/07 2017

1.0.0

1.0.0.0

PHP Enum implementation

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Sergey Predvoditelev

php enum