PHP Classes

PHP Enum Value: Create enumerated value objects from constants

Recommend this page to a friend!

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


  Detailed description   Download Download .zip .tar.gz  
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

Details

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

  Classes of Matous Nemec  >  PHP Enum Value  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Enum Value
Base name: enum-class
Description: Create enumerated value objects from constants
Version: -
PHP version: 5
License: Custom (specified in a license file)
All time users: 28 users
All time rank: 10659
Week users: 0 users
Week rank: 189 Equal
 
  Groups   Rate classes User ratings   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Data types Modeling and manipulating data types View top rated classes


  User ratings  
Not yet rated by the users

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  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

Download Download all files: enum-class.tar.gz enum-class.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.