PHP Classes

PHP Enum Value: Create enumerated value objects from constants

Recommend this page to a friend!
  Info   View files Documentation   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 29 All time: 11,014 This week: 171Up
Version License PHP version Categories
enum-class 1.0Custom (specified...5PHP 5, Data types
Description 

Author

This package can create enumerated value objects from constants.

It provides a base class that applications can extend to implement enumerated values classes.

The enumerated value classes define constants that values are acceptable as valid enumerated values.

The base enumerated value class provides several useful functions like:

- Get one or more valid enumerated values

- Check if a given value is a valid enumerated value

- Compare enumerated values

Picture of Matous Nemec
Name: Matous Nemec <contact>
Classes: 8 packages by
Country: Czech Republic Czech Republic
Age: ???
All time rank: 298032 in Czech Republic Czech Republic
Week rank: 332 Up2 in Czech Republic Czech Republic Up
Innovation award
Innovation award
Nominee: 2x

Documentation

Enums with properties

Documentation

Basic usage

You can found documentation for basic usage here: consistence/consistence docs.

Implementation

You can easily define properties for enum values.

<?php

class CardColor extends \Mesour\Enum\Enum
{

	public const BLACK = ['black', true];
	public const RED = ['red', false];

	/ @var bool */
	private $dashed;

	public function __construct(string $value, bool $dashed)
	{
		parent::__construct($value);
		$this->dashed = $dashed;
	}

	public function isDashed(): bool
	{
		return $this->dashed;
	}

}

$red = CardColor::get(CardColor::RED);
$red->getValue(); // 'red'
$red->isDashed(); // false

$black = CardColor::get(CardColor::BLACK);
$red->isDashed(); // true

$availableValues = CardColor::getAvailableValues(); // ['black', 'red']

Comparing

Once you have an enum instance, you can compare them in multiple ways:

<?php

$red = CardColor::get(CardColor::RED);
$red2 = CardColor::get(CardColor::RED);
$black = CardColor::get(CardColor::BLACK);

// by instance
$red === $red; // true
$red === $red2; // true
$red === $black; // false

// with method
$red->equals($red); // true
$red->equals($red2); // true
$red->equals($black); // false

// by value
$red->equalsValue(CardColor::RED); // true
$red->equalsValue('red'); // true
$red->equalsValue(CardColor::BLACK); // false

Default values

You can use default values in constructor and simplify code.

<?php

class CardColor extends \Mesour\Enum\Enum
{

	public const BLACK = ['black', true];
	public const RED = ['red'];
	public const BLUE = 'blue'; // same as ['blue']

	/ @var bool */
	private $dashed;

	public function __construct(string $value, bool $dashed = false)
	{
		parent::__construct($value);
		$this->dashed = $dashed;
	}

	// ...
}

$blue = CardColor::get(CardColor::BLUE);
$blue->isDashed(); // false

  Files folder image Files  
File Role Description
Files folder imagedocs (1 directory)
Files folder imagesrc (1 directory)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file license.md Lic. License text
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files  /  docs  
File Role Description
Files folder imageen (1 file)

  Files folder image Files  /  docs  /  en  
File Role Description
  Accessible without login Plain text file index.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageMesour (1 directory)

  Files folder image Files  /  src  /  Mesour  
File Role Description
Files folder imageEnum (1 file, 1 directory)

  Files folder image Files  /  src  /  Mesour  /  Enum  
File Role Description
Files folder imageexceptions (3 files)
  Plain text file Enum.php Class Class source

  Files folder image Files  /  src  /  Mesour  /  Enum  /  exceptions  
File Role Description
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file InvalidEnumValueException.php Class Class source
  Plain text file InvalidStateException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageMesour (1 directory)
  Accessible without login Plain text file bootstrap.php Example Example script
  Accessible without login Plain text file php.ini Data Auxiliary data

  Files folder image Files  /  tests  /  Mesour  
File Role Description
Files folder imageEnumTests (1 file, 1 directory)

  Files folder image Files  /  tests  /  Mesour  /  EnumTests  
File Role Description
Files folder imageEnums (1 file)
  Plain text file EnumTest.phpt Class Class source

  Files folder image Files  /  tests  /  Mesour  /  EnumTests  /  Enums  
File Role Description
  Plain text file TestEnum.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:29
This week:0
All time:11,014
This week:171Up