├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src └── Enum.php └── tests ├── GeneralTest.php └── SampleEnum.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | .idea/ 3 | composer.lock 4 | .phpunit.result.cache 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | language: php 4 | 5 | php: 6 | - 7.1 7 | - 7.2 8 | - 7.3 9 | - 7.4 10 | - 8.0 11 | 12 | branches: 13 | only: 14 | - master 15 | 16 | before_script: 17 | - composer install --no-interaction --no-suggest 18 | - composer require php-coveralls/php-coveralls 19 | 20 | script: 21 | - mkdir -p build/logs 22 | - vendor/bin/phpunit --coverage-clover build/logs/clover.xml tests 23 | 24 | after_success: 25 | - travis_retry php vendor/bin/php-coveralls -v 26 | 27 | notifications: 28 | email: 29 | recipients: 30 | - info@miladrahimi.com 31 | on_success: change 32 | on_failure: always -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Milad Rahimi 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 | [![Latest Stable Version](https://poser.pugx.org/miladrahimi/php-enum/v/stable)](https://packagist.org/packages/miladrahimi/php-enum) 2 | [![Total Downloads](https://poser.pugx.org/miladrahimi/php-enum/downloads)](https://packagist.org/packages/miladrahimi/php-enum) 3 | [![Build Status](https://travis-ci.org/miladrahimi/php-enum.svg?branch=master)](https://travis-ci.org/miladrahimi/php-enum) 4 | [![Coverage Status](https://coveralls.io/repos/github/miladrahimi/php-enum/badge.svg?branch=master)](https://coveralls.io/github/miladrahimi/php-enum?branch=master) 5 | [![License](https://poser.pugx.org/miladrahimi/php-enum/license)](https://packagist.org/packages/miladrahimi/php-enum) 6 | 7 | # PHP-Enum 8 | 9 | To make your Enums feel alive, just make them extend `Enum` abstract class! 10 | 11 | ## Installation 12 | 13 | Install [Composer](https://getcomposer.org) and run following command in your project's root directory: 14 | 15 | ``` 16 | composer require miladrahimi/php-enum "1.*" 17 | ``` 18 | 19 | ## Documentation 20 | 21 | Consider this enum class: 22 | 23 | 24 | ```php 25 | namespace MiladRahimi\Enum\Enum; 26 | 27 | class SampleEnum extends Enum 28 | { 29 | const UNO = 1; 30 | const ONE = 1; 31 | const TWO = 2; 32 | const STR = "sth"; 33 | } 34 | ``` 35 | 36 | Now you are able to use this methods: 37 | 38 | ```php 39 | SampleEnum::all(); // ['UNO' => 1, 'ONE' => 1, 'TWO' => 2, 'STR' => 'sth'] 40 | 41 | SampleEnum::keys(); // ['UNO', 'ONE', 'TWO', 'STR']; 42 | 43 | SampleEnum::values(); // [1, 1, 2, 'sth'] 44 | 45 | SampleEnum::hasKey('ONE'); // true 46 | 47 | SampleEnum::hasKey('xXx'); // false 48 | 49 | SampleEnum::hasValue(2); // true 50 | 51 | SampleEnum::hasValue('xXx'); // false 52 | 53 | SampleEnum::valueOf('ONE'); // 1 54 | 55 | SampleEnum::keysOf(1); // ['UNO', 'ONE'] 56 | 57 | SampleEnum::keyOf(1); // 'UNO' 58 | 59 | SampleEnum::randomKey(); // One of 'UNO', 'ONE', 'TWO', 'STR' 60 | 61 | SampleEnum::randomKeyExceptKeys(['ONE', 'TWO']); // One of 'UNO', 'STR' 62 | 63 | SampleEnum::randomKeyExceptValues([SampleEnum::STR, SampleEnum::TWO]); // One of 'ONE', 'UNO' 64 | 65 | SampleEnum::randomValue(); // One of 1, 2, 'sth' 66 | 67 | SampleEnum::randomValueExceptValues([SampleEnum::STR, SampleEnum::TWO]); // One of SampleEnum::ONE, SampleEnum::UNO 68 | 69 | SampleEnum::randomValueExceptKeys(['STR', 'TWO']); // One of SampleEnum::ONE, SampleEnum::UNO 70 | ``` 71 | 72 | ## License 73 | PHP-Enum is initially created by [Milad Rahimi](https://miladrahimi.com) 74 | and released under the [MIT License](http://opensource.org/licenses/mit-license.php). 75 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "miladrahimi/php-enum", 3 | "description": "A PHP Abstract Enum Class", 4 | "keywords": [ 5 | "Enum", 6 | "Abstract Enum", 7 | "Enumerated type" 8 | ], 9 | "type": "library", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Milad Rahimi", 14 | "email": "info@miladrahimi.com" 15 | } 16 | ], 17 | "autoload": { 18 | "psr-4": { 19 | "MiladRahimi\\Enum\\": "src/" 20 | } 21 | }, 22 | "autoload-dev": { 23 | "psr-4": { 24 | "MiladRahimi\\Enum\\Tests\\": "tests/" 25 | } 26 | }, 27 | "require": { 28 | "php": ">=7.1|^8.0" 29 | }, 30 | "require-dev": { 31 | "phpunit/phpunit": "^7|^8|^9" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | ./tests 6 | 7 | 8 | 9 | 10 | ./src 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Enum.php: -------------------------------------------------------------------------------- 1 | getConstants(); 30 | } catch (Throwable $e) { 31 | return []; 32 | } 33 | } 34 | 35 | /** 36 | * Get all the declared keys 37 | * 38 | * @return array 39 | */ 40 | public static function keys(): array 41 | { 42 | return array_keys(static::all()); 43 | } 44 | 45 | /** 46 | * Get all the declared values 47 | * 48 | * @return array 49 | */ 50 | public static function values(): array 51 | { 52 | return array_values(static::all()); 53 | } 54 | 55 | /** 56 | * Check if the given key declared in the enum or not 57 | * 58 | * @param string $key 59 | * @return bool 60 | */ 61 | public static function hasKey(string $key): bool 62 | { 63 | return array_key_exists($key, static::all()); 64 | } 65 | 66 | /** 67 | * Check if the given value declared in the enum or not 68 | * 69 | * @param mixed $value 70 | * @return bool 71 | */ 72 | public static function hasValue($value): bool 73 | { 74 | return in_array($value, static::all()); 75 | } 76 | 77 | /** 78 | * Get value of the given key 79 | * 80 | * @param $key 81 | * @param mixed|null $default 82 | * @return mixed|null 83 | */ 84 | public static function valueOf($key, $default = null) 85 | { 86 | return static::all()[$key] ?? $default; 87 | } 88 | 89 | /** 90 | * Get related keys of the given value 91 | * 92 | * @param $value 93 | * @return array 94 | */ 95 | public static function keysOf($value): array 96 | { 97 | $keys = []; 98 | 99 | foreach (static::all() as $k => $v) { 100 | $v == $value && ($keys[] = $k); 101 | } 102 | 103 | return $keys; 104 | } 105 | 106 | /** 107 | * Get only the first related key of the given value 108 | * 109 | * @param $value 110 | * @param null $default 111 | * @return mixed|null 112 | */ 113 | public static function keyOf($value, $default = null) 114 | { 115 | return static::keysOf($value)[0] ?? $default; 116 | } 117 | 118 | /** 119 | * Get a random key 120 | * 121 | * @return mixed 122 | */ 123 | public static function randomKey() 124 | { 125 | return array_rand(static::all()); 126 | } 127 | 128 | /** 129 | * Get a random key except given values 130 | * 131 | * @param array $values 132 | * @return array|int|string 133 | */ 134 | public static function randomKeyExceptValues(array $values = []) 135 | { 136 | do { 137 | $key = array_rand(static::all()); 138 | } while (in_array(static::all()[$key], $values)); 139 | 140 | return $key; 141 | } 142 | 143 | /** 144 | * Get a random key except given keys 145 | * 146 | * @param array $keys 147 | * @return array|int|string 148 | */ 149 | public static function randomKeyExceptKeys(array $keys = []) 150 | { 151 | do { 152 | $key = array_rand(static::all()); 153 | } while (in_array($key, $keys)); 154 | 155 | return $key; 156 | } 157 | 158 | /** 159 | * Get a random value 160 | * 161 | * @return mixed 162 | */ 163 | public static function randomValue() 164 | { 165 | return static::all()[array_rand(static::all())]; 166 | } 167 | 168 | /** 169 | * Get a random value except given values 170 | * 171 | * @param array $values 172 | * @return mixed 173 | */ 174 | public static function randomValueExceptValues(array $values = []) 175 | { 176 | do { 177 | $value = static::all()[array_rand(static::all())]; 178 | } while (in_array($value, $values)); 179 | 180 | return $value; 181 | } 182 | 183 | /** 184 | * Get a random value except given keys 185 | * 186 | * @param array $keys 187 | * @return mixed 188 | */ 189 | public static function randomValueExceptKeys(array $keys = []) 190 | { 191 | do { 192 | $key = array_rand(static::all()); 193 | } while (in_array($key, $keys)); 194 | 195 | return static::all()[$key]; 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /tests/GeneralTest.php: -------------------------------------------------------------------------------- 1 | 1, 11 | 'UNO' => 1, 12 | 'TWO' => 2, 13 | 'STR' => 'sth', 14 | ]; 15 | 16 | private $sample1Keys = ['ONE', 'UNO', 'TWO', 'STR']; 17 | 18 | private $sample1Values = [1, 1, 2, 'sth']; 19 | 20 | public function test_all_method() 21 | { 22 | $items = SampleEnum::all(); 23 | 24 | $this->assertSame($this->sample1, $items); 25 | } 26 | 27 | public function test_keys_method() 28 | { 29 | $keys = SampleEnum::keys(); 30 | 31 | $this->assertSame($this->sample1Keys, $keys); 32 | } 33 | 34 | public function test_values_method() 35 | { 36 | $values = SampleEnum::values(); 37 | 38 | $this->assertSame($this->sample1Values, $values); 39 | } 40 | 41 | public function test_hasKey_method() 42 | { 43 | $this->assertTrue(SampleEnum::hasKey($this->sample1Keys[0])); 44 | $this->assertFalse(SampleEnum::hasKey('')); 45 | } 46 | 47 | public function test_hasValue_method() 48 | { 49 | $this->assertTrue(SampleEnum::hasValue($this->sample1Values[0])); 50 | $this->assertFalse(SampleEnum::hasValue('')); 51 | } 52 | 53 | public function test_value_of_method() 54 | { 55 | $this->assertSame('sth', SampleEnum::valueOf('STR')); 56 | $this->assertSame(1, SampleEnum::valueOf('ONE')); 57 | $this->assertSame(1, SampleEnum::valueOf('UNO')); 58 | } 59 | 60 | public function test_keys_of_method() 61 | { 62 | $this->assertSame(['STR'], SampleEnum::keysOf('sth')); 63 | $this->assertSame(['ONE', 'UNO'], SampleEnum::keysOf(1)); 64 | } 65 | 66 | public function test_key_of_method() 67 | { 68 | $this->assertSame('STR', SampleEnum::keyOf('sth')); 69 | $this->assertSame('ONE', SampleEnum::keyOf(1)); 70 | } 71 | 72 | public function test_random_key() 73 | { 74 | $this->assertArrayHasKey(SampleEnum::randomKey(), SampleEnum::all()); 75 | } 76 | 77 | public function test_random_key_except_keys() 78 | { 79 | $this->assertContains(SampleEnum::randomKeyExceptKeys(['ONE', 'TWO']), ['UNO', 'STR']); 80 | } 81 | 82 | public function test_random_key_except_values() 83 | { 84 | $this->assertContains(SampleEnum::randomKeyExceptValues([SampleEnum::STR, SampleEnum::TWO]), ['ONE', 'UNO']); 85 | } 86 | 87 | public function test_random_value() 88 | { 89 | $this->assertContains(SampleEnum::randomValue(), SampleEnum::all()); 90 | } 91 | 92 | public function test_random_value_except_values() 93 | { 94 | $this->assertContains(SampleEnum::randomValueExceptValues([SampleEnum::STR, SampleEnum::TWO]), 95 | [SampleEnum::ONE] 96 | ); 97 | } 98 | 99 | public function test_random_value_except_keys() 100 | { 101 | $this->assertContains(SampleEnum::randomValueExceptKeys(['STR', 'TWO']), [SampleEnum::ONE]); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/SampleEnum.php: -------------------------------------------------------------------------------- 1 |