Coder Social home page Coder Social logo

enum's Introduction

Enumerations for PHP

Scrutinizer Code Quality Total Downloads Build Status Latest stable version License

The enumeration package provides easy usage of enumerations.

The enum features are similar to SplEnum, but without installation of an additional PHP package. It provides convenient features to speed up the implementation enumerations to your application.

Installation

composer require deltatag/enum

Object methods:

  • __construct() The constructor check if given value is in the enumeration or simply use defined default value. If value not allowed a EnumValueNotAllowedException will be thrown.
  • __toString() You can directly output the enum object or check against value
  • getValue() Get the current value of the enum object
  • isEqual() Compare one instance of enum to another

Static methods:

  • getConstants() Returns all possible values for enumeration with constant name as key
  • getDefault() Get default enum value is set. Constant key must be __default. If no default value was defined a EnumDefaultValueNotDefinedException will be thrown.
  • toArray() Wrapper for method getConstants()
  • isValid() Check if value is valid enum value

For general constant methods you can use the ConstantsTrait.

Usage

Simply define an enum class of your need and use it for validation.

// Create new enumeration class which extends Enum base class
class Fruits extends Deltatag\Enum\Enum
{
	// Default value
	const __default = self::APPLE;
	// Enumeration values
	const APPLE = '1';
	const ORANGE = '2';
	const PEAR = '3';
	const BANANA = '4';
}

// check for constant

Fruits::isValid(Fruits::BANANA); // returns true


// check for value

Fruits::isValid('1'); // returns true

Fruits::isValid('Potato') // returns false

// get all values of enumeration
$enumValues = Fruits::getConstants();

For object oriented usage you can also use the enum object itself for type-hinting.

// use enumeration class
$apple = new Fruits(Fruits::APPLE);

function checkMyFruit(Fruits $fruit)
{
	if ($fruit == Fruits::APPLE) {
		echo "I'm an apple!";
	} else {
		echo "I'm no apple";
	}
}

checkMyFruit($apple); // will output "I'm an apple!"

When facing other values then strings for constants the comparison must change to the following.

// use enumeration class
$apple = new Fruits(Fruits::APPLE);

function checkMyFruit(Fruits $fruit)
{
    // directly compare enumerations
	if (fruit == new Fruits(Fruits::APPLE)) {
		echo "I'm an apple!";
	} else {
		echo "I'm no apple";
	}
}

Contributing

Dependencies are managed through composer:

$ composer install

Tests can then be run via phpunit:

$ vendor/bin/phpunit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.