2017 © Pedro PelĆ”ez
 

library php-enumeration

This small package can represent a enumeration field. For example in a database or an api.

image

freshp/php-enumeration

This small package can represent a enumeration field. For example in a database or an api.

  • Tuesday, July 17, 2018
  • by freshp
  • Repository
  • 2 Watchers
  • 1 Stars
  • 805 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 8 % Grown

The README.md

Build Status Software License Latest Stable Version Total Downloads, (*1)

php-enumeration

This small package can represent a enumeration field. For example in a database or an api., (*2)

Featureslist: * inspired and mostly used for MySQL enum-fields or API enum-fields * it is a simple string representation * no other values are allow besides the defined in the class * simple usage and easy to read * no features a enum do not have to deal with * with a default fallback value which will be used if no matching value exists in const“s * you can harden this object by throwing an exception in the getDefault-method, (*3)

Install

Basic install via composer, (*4)

composer require freshp/php-enumeration

Usage

Take a look in tests/fixtures to see a executable example., (*5)

Create a new object with: 1. public const“s which represent strings 2. implement the getDefault-method, (*6)

Create an enumeration object

use FreshP\PhpEnumeration\Enum;

class EnumExample extends Enum
{
    public const TEST_CONSTANT = 'constant';
    public const TEST_DEFAULT = 'default';

    protected function getDefault(): string
    {
        return self::TEST_DEFAULT;
    }
}

use annotations for better ide support, (*7)

use FreshP\PhpEnumeration\Enum;

/**
 * @method static self TEST_CONSTANT()
 * @method static self TEST_DEFAULT()
 */
class EnumExample extends Enum
...

make the data of the parent toArray-method public if you need to iterate over all options, (*8)

class EnumExample extends Enum
{
...
    public static function listAllOptions(): array
    {
        return self::toArray();
    }
...

Use the enumeration object

  1. create the object by static call, (*9)

    $enum = EnumExample::TEST_CONSTANT();
    
  2. create the object by normal initialization, (*10)

    $enum = new EnumExample(EnumExample::TEST_CONSTANT);
    
  3. create a default object (the value from the getDefault-method will be called), (*11)

    $enum = new EnumExample();
    
  4. compare the object by using the __toString-method, (*12)

    $enum->__toString() === EnumExample::TEST_CONSTANT
    

    or, (*13)

    $enum->isEqual(EnumExample::TEST_CONSTANT())
    

Checks

Run each command in the project root directory., (*14)

run all check with composer script, (*15)

composer quickcheck

Execute PHPUnit tests

./vendor/bin/phpunit.phar --testdox

Execute fix PHPCS problems

./vendor/bin/phpcbf.phar

Execute PHPCS checks

./vendor/bin/phpcs.phar

Execute PHPSTAN checks

./vendor/bin/phpstan.phar analyse -l max ./src

The Versions

17/07 2018

dev-master

9999999-dev https://github.com/freshp/php-enumeration

This small package can represent a enumeration field. For example in a database or an api.

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

17/07 2018

1.0.0

1.0.0.0 https://github.com/freshp/php-enumeration

This small package can represent a enumeration field. For example in a database or an api.

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

17/07 2018

dev-feature/php-and-requirement-update

dev-feature/php-and-requirement-update https://github.com/freshp/php-enumeration

This small package can represent a enumeration field. For example in a database or an api.

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

29/12 2016
29/12 2016
24/11 2016
24/11 2016

0.0.1

0.0.1.0

  Sources   Download

The Requires

  • php >=7.0

 

The Development Requires