├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src └── AbstractEnum.php └── tests ├── AbstractEnumTest.php └── DummyEnum.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | install: 10 | - composer self-update 11 | - composer install 12 | 13 | script: 14 | - ./vendor/bin/phpunit -c ./phpunit.xml --coverage-text --strict 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Commerce Guys 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | enum 2 | ===== 3 | 4 | [![Build Status](https://travis-ci.org/commerceguys/enum.svg?branch=master)](https://travis-ci.org/commerceguys/enum) 5 | 6 | A PHP 5.4+ enumeration library. 7 | 8 | Class constants are frequently used to denote sets of allowed values. 9 | By grouping them in an enumeration class, we gain the ability to add helper methods, 10 | list all possible values and validate values against them. 11 | 12 | A [commerceguys/addressing](https://github.com/commerceguys/addressing) example: 13 | ```php 14 | namespace CommerceGuys\Addressing\Enum; 15 | 16 | use CommerceGuys\Enum\AbstractEnum; 17 | 18 | /** 19 | * Enumerates available locality types. 20 | */ 21 | final class LocalityType extends AbstractEnum 22 | { 23 | const CITY = 'city'; 24 | const DISTRICT = 'district'; 25 | 26 | // We can provide a getDefault() method here, or anything else. 27 | } 28 | 29 | LocalityType::getAll(); // ['CITY' => 'city', 'DISTRICT' => 'district'] 30 | LocalityType::getKey('city'); // 'CITY' 31 | LocalityType::exists('city'); // true 32 | LocalityType::assertExists('invalid value'); // InvalidArgumentException 33 | LocalityType::assertAllExist(['district', 'invalid value']); // InvalidArgumentException 34 | ``` 35 | 36 | Meanwhile, on the AddressFormat: 37 | ```php 38 | // The AddressFormatInterface is now free of LOCALITY_TYPE_ constants. 39 | class AdressFormat implements AddressFormatInterface 40 | { 41 | public function setLocalityType($localityType) 42 | { 43 | LocalityType::assertExists($localityType); 44 | $this->localityType = $localityType; 45 | } 46 | } 47 | ``` 48 | 49 | The reason why this library was made instead of reusing [myclabs/php-enum](https://github.com/myclabs/php-enum) 50 | was that we didn't want to allow enumerations to be instantiated. 51 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "commerceguys/enum", 3 | "description": "A PHP 5.4+ enumeration library.", 4 | "license": "MIT", 5 | "require": { 6 | "php": ">=5.4.0" 7 | }, 8 | "require-dev": { 9 | "phpunit/phpunit": "~4.0" 10 | }, 11 | "autoload": { 12 | "psr-4": { 13 | "CommerceGuys\\Enum\\": "src" 14 | } 15 | }, 16 | "autoload-dev": { 17 | "psr-4": { 18 | "CommerceGuys\\Enum\\Tests\\": "tests" 19 | } 20 | }, 21 | "authors": [ 22 | { 23 | "name": "Bojan Zivanovic" 24 | } 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | 20 | ./src/ 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/AbstractEnum.php: -------------------------------------------------------------------------------- 1 | getConstants(); 32 | } 33 | 34 | return static::$values[$class]; 35 | } 36 | 37 | /** 38 | * Gets the key of the provided value. 39 | * 40 | * @param string $value The value. 41 | * 42 | * @return bool The key if found, false otherwise. 43 | */ 44 | public static function getKey($value) 45 | { 46 | return array_search($value, static::getAll(), true); 47 | } 48 | 49 | /** 50 | * Checks whether the provided value is defined. 51 | * 52 | * @param string $value The value. 53 | * 54 | * @return bool True if the value is defined, false otherwise. 55 | */ 56 | public static function exists($value) 57 | { 58 | return in_array($value, static::getAll(), true); 59 | } 60 | 61 | /** 62 | * Asserts that the provided value is defined. 63 | * 64 | * @param string $value The value. 65 | * 66 | * @throws \InvalidArgumentException 67 | */ 68 | public static function assertExists($value) 69 | { 70 | if (static::exists($value) == false) { 71 | $class = substr(strrchr(get_called_class(), '\\'), 1); 72 | throw new \InvalidArgumentException(sprintf('"%s" is not a valid %s value.', $value, $class)); 73 | } 74 | } 75 | 76 | /** 77 | * Asserts that all provided valus are defined. 78 | * 79 | * @param array $values The values. 80 | */ 81 | public static function assertAllExist(array $values) 82 | { 83 | foreach ($values as $value) { 84 | static::assertExists($value); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /tests/AbstractEnumTest.php: -------------------------------------------------------------------------------- 1 | 'first', 'SECOND' => 'second']; 16 | $values = DummyEnum::getAll(); 17 | $this->assertEquals($expectedValues, $values); 18 | } 19 | 20 | /** 21 | * @covers ::getKey 22 | * 23 | * @uses \CommerceGuys\Enum\AbstractEnum::getAll 24 | */ 25 | public function testGetKey() 26 | { 27 | $key = DummyEnum::getKey('first'); 28 | $this->assertEquals('FIRST', $key); 29 | 30 | $key = DummyEnum::getKey('invalid'); 31 | $this->assertEquals(false, $key); 32 | } 33 | 34 | /** 35 | * @covers ::exists 36 | * 37 | * @uses \CommerceGuys\Enum\AbstractEnum::getAll 38 | */ 39 | public function testExists() 40 | { 41 | $result = DummyEnum::exists('second'); 42 | $this->assertEquals(true, $result); 43 | 44 | $result = DummyEnum::exists('invalid'); 45 | $this->assertEquals(false, $result); 46 | } 47 | 48 | /** 49 | * @covers ::assertExists 50 | * 51 | * @uses \CommerceGuys\Enum\AbstractEnum::getAll 52 | * @uses \CommerceGuys\Enum\AbstractEnum::exists 53 | * @expectedException \InvalidArgumentException 54 | * @expectedExceptionMessage "invalid" is not a valid DummyEnum value. 55 | */ 56 | public function testAssertExists() 57 | { 58 | $result = DummyEnum::assertExists('invalid'); 59 | } 60 | 61 | /** 62 | * @covers ::assertAllExist 63 | * 64 | * @uses \CommerceGuys\Enum\AbstractEnum::getAll 65 | * @uses \CommerceGuys\Enum\AbstractEnum::exists 66 | * @uses \CommerceGuys\Enum\AbstractEnum::assertExists 67 | * @expectedException \InvalidArgumentException 68 | * @expectedExceptionMessage "invalid" is not a valid DummyEnum value. 69 | */ 70 | public function testAssertAllExist() 71 | { 72 | $result = DummyEnum::assertAllExist(['second', 'invalid']); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/DummyEnum.php: -------------------------------------------------------------------------------- 1 |