PHP Classes

PHP Enum Value: Create enumerated value objects from constants

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 30 All time: 11,160 This week: 560Up
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
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 (12)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:30
This week:0
All time:11,160
This week:560Up