https://github.com/deltatag/enum
Easy & simple enumerations for PHP
https://github.com/deltatag/enum
database deltatag enumeration php validation
Last synced: 6 months ago
JSON representation
Easy & simple enumerations for PHP
- Host: GitHub
- URL: https://github.com/deltatag/enum
- Owner: deltatag
- License: mit
- Created: 2017-12-20T20:27:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-24T09:50:32.000Z (over 8 years ago)
- Last Synced: 2023-08-21T09:22:51.374Z (almost 3 years ago)
- Topics: database, deltatag, enumeration, php, validation
- Language: PHP
- Homepage:
- Size: 19.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Enumerations for PHP
[](https://scrutinizer-ci.com/g/deltatag/enum/?branch=master)
[](https://packagist.org/packages/deltatag/enum)
[](https://scrutinizer-ci.com/g/deltatag/enum/build-status/master)
[](https://packagist.org/packages/deltatag/enum)
[](https://packagist.org/packages/deltatag/enum)
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.
```php
// 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.
```php
// 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.
```php
// 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
```