├── .github └── workflows │ └── run-tests.yml ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── src └── PoweredEnum.php └── tests └── PoweredEnumTest.php /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | test: 11 | runs-on: ${{ matrix.os }} 12 | strategy: 13 | fail-fast: true 14 | matrix: 15 | os: [ubuntu-latest] 16 | php: [8.1] 17 | stability: [prefer-lowest, prefer-stable] 18 | 19 | name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} 20 | 21 | steps: 22 | - name: Checkout code 23 | uses: actions/checkout@v3 24 | 25 | - name: Setup PHP 26 | uses: shivammathur/setup-php@v2 27 | with: 28 | php-version: ${{ matrix.php }} 29 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo 30 | coverage: none 31 | 32 | - name: Setup problem matchers 33 | run: | 34 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 35 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 36 | 37 | - name: Install dependencies 38 | run: composer install --no-progress --no-ansi --profile --no-interaction --no-scripts --prefer-dist 39 | 40 | - name: Execute tests 41 | run: vendor/bin/phpunit tests/*.php -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .phpunit.result.cache 2 | .idea 3 | vendor 4 | composer.lock -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## 0.0.1 - 2022-10-12 6 | - Initial release. -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Turan Karatuğ 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 | # Powered Enum 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/tkaratug/powered-enum.svg?style=flat-square)](https://packagist.org/packages/tkaratug/powered-enum) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/tkaratug/powered-enum/run-tests?label=tests)](https://github.com/tkaratug/powered-enum/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/tkaratug/powered-enum.svg?style=flat-square)](https://packagist.org/packages/tkaratug/powered-enum) 6 | 7 | This package offers a trait that contains some cool features for native PHP enums. 8 | 9 | ## Requirements 10 | - PHP `8.1` or higher. 11 | 12 | ## Installation 13 | 14 | ```bash 15 | composer require tkaratug/powered-enum 16 | ``` 17 | 18 | ## Declaration 19 | 20 | All you need to do is use the `PoweredEnum` trait in your native PHP enums. 21 | 22 | ```php 23 | use Tkaratug\PoweredEnum\PoweredEnum; 24 | 25 | enum MyEnum: int 26 | { 27 | use PoweredEnum; 28 | 29 | case ONE = 1; 30 | case TWO = 2; 31 | case THREE = 3; 32 | } 33 | ``` 34 | 35 | ## Jump To 36 | - [Methods](#Methods) 37 | - [is(), isNot()](#is-isnot) 38 | - [Static Methods](#static-methods) 39 | - [hasName()](#hasname) 40 | - [hasValue()](#hasvalue) 41 | - [getNames()](#getnames) 42 | - [getValues()](#getvalues) 43 | - [toArray()](#toarray) 44 | - [getNamesExcept()](#getnamesexcept) 45 | - [getValuesExcept()](#getvalueexcept) 46 | - [toArrayExcept()](#toarrayexcept) 47 | - [getRandomName()](#getrandomname) 48 | - [getRandomValue()](#getrandomvalue) 49 | - [getRandomCase()](#getrandomcase) 50 | 51 | ___ 52 | 53 | ## Methods 54 | ### is(), isNot() 55 | - You can check the equality of a case against any name by passing it to the `is()` and `isNot()` methods. 56 | 57 | ```php 58 | $myEnum = MyEnum::ONE; 59 | 60 | $myEnum->is(MyEnum::ONE); // true 61 | $myEnum->is(MyEnum::TWO); // false 62 | 63 | $myEnum->isNot(MyEnum::ONE); // false 64 | $myEnum->isNot(MyEnum::TWO); // true 65 | ``` 66 | 67 | --- 68 | 69 | ## Static Methods 70 | 71 | ### hasName() 72 | - You can check whether an enum has a case by given name via the `hasName()` method. 73 | 74 | ```php 75 | enum MyEnum: int { 76 | use PoweredEnum; 77 | 78 | case ONE = 1; 79 | case TWO = 2; 80 | } 81 | 82 | MyEnum::hasName('ONE'); // true 83 | MyEnum::hasName('THREE'); // false 84 | ``` 85 | 86 | ___ 87 | 88 | ### hasValue() 89 | - You can check whether an enum has a case by given value via the `hasValue()` method. 90 | 91 | ```php 92 | enum MyEnum: int { 93 | use PoweredEnum; 94 | 95 | case ONE = 1; 96 | case TWO = 2; 97 | } 98 | 99 | MyEnum::hasValue(1); // true 100 | MyEnum::hasValue(3); // false 101 | ``` 102 | 103 | ___ 104 | 105 | ### getNames() 106 | - You can get enum case names as an array by using the `getNames()` method. 107 | 108 | ```php 109 | enum MyEnum: int { 110 | use PoweredEnum; 111 | 112 | case ONE = 1; 113 | case TWO = 2; 114 | } 115 | 116 | MyEnum::getNames(); // ['ONE', 'TWO'] 117 | ``` 118 | 119 | ___ 120 | 121 | ### getValues() 122 | - You can get enum case values as an array by using the `getValues()` method. 123 | 124 | ```php 125 | enum MyEnum: int { 126 | use PoweredEnum; 127 | 128 | case ONE = 1; 129 | case TWO = 2; 130 | } 131 | 132 | MyEnum::getValues(); // [1, 2] 133 | ``` 134 | 135 | ___ 136 | 137 | ### toArray() 138 | - You can get a combined array of the enum cases as `value => name` by using the `toArray()` method. 139 | 140 | ```php 141 | enum MyEnum: int { 142 | use PoweredEnum; 143 | 144 | case ONE = 1; 145 | case TWO = 2; 146 | } 147 | 148 | MyEnum::toArray(); // [1 => 'ONE', 2 => 'TWO'] 149 | ``` 150 | 151 | ___ 152 | 153 | ### getNamesExcept() 154 | - You can get names of enum cases as an array except for given ones by using the `getNamesExcept()` method. 155 | 156 | ```php 157 | enum MyEnum: int { 158 | use PoweredEnum; 159 | 160 | case ONE = 1; 161 | case TWO = 2; 162 | case THREE = 3; 163 | } 164 | 165 | MyEnum::getNamesExcept([MyEnum::ONE]); // ['TWO', 'THREE'] 166 | ``` 167 | 168 | ___ 169 | 170 | ### getValuesExcept() 171 | - You can get values of enum cases as an array except for given ones by using the `getValuesExcept()` method. 172 | 173 | ```php 174 | enum MyEnum: int { 175 | use PoweredEnum; 176 | 177 | case ONE = 1; 178 | case TWO = 2; 179 | case THREE = 3; 180 | } 181 | 182 | MyEnum::getValuesExcept([MyEnum::ONE]); // [2, 3] 183 | ``` 184 | 185 | ___ 186 | 187 | ### toArrayExcept() 188 | - You can get a combined array of the enum cases as `value => name` except for given ones by using the `toArrayExcept()` method. 189 | 190 | ```php 191 | enum MyEnum: int { 192 | use PoweredEnum; 193 | 194 | case ONE = 1; 195 | case TWO = 2; 196 | case THREE = 3; 197 | } 198 | 199 | MyEnum::toArrayExcept([MyEnum::ONE]); // [2 => 'TWO', 3 => 'THREE] 200 | ``` 201 | 202 | ___ 203 | 204 | ### getRandomName() 205 | - You can get a random name of the enum cases by using the `getRandomName()` method. 206 | 207 | ```php 208 | enum MyEnum: int { 209 | use PoweredEnum; 210 | 211 | case ONE = 1; 212 | } 213 | 214 | MyEnum::getRandomName(); // ['ONE'] 215 | ``` 216 | 217 | ___ 218 | 219 | ### getRandomValue() 220 | - You can get a random value of the enum cases by using the `getRandomValue()` method. 221 | 222 | ```php 223 | enum MyEnum: int { 224 | use PoweredEnum; 225 | 226 | case ONE = 1; 227 | } 228 | 229 | MyEnum::getRandomValue(); // [1] 230 | ``` 231 | 232 | ___ 233 | 234 | ### getRandomCase() 235 | - You can get a random case of the enum by using the `getRandomCase()` method. 236 | 237 | ```php 238 | enum MyEnum: int { 239 | use PoweredEnum; 240 | 241 | case ONE = 1; 242 | } 243 | 244 | MyEnum::getRandomCase(); // MyEnum::ONE 245 | ``` 246 | 247 | ## Testing 248 | 249 | ```bash 250 | composer test 251 | ``` 252 | 253 | ## Changelog 254 | 255 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 256 | 257 | ## Contributing 258 | 259 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 260 | 261 | ## Security Vulnerabilities 262 | 263 | If you've found a bug regarding security please mail [tkaratug@hotmail.com.tr](mailto:tkaratug@hotmail.com.tr) instead of using the issue tracker. 264 | 265 | ## Credits 266 | 267 | - [Turan Karatuğ](https://github.com/tkaratug) 268 | - [All Contributors](../../contributors) 269 | 270 | ## License 271 | 272 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tkaratug/powered-enum", 3 | "description": "Some cool add-ons to PHP native enums.", 4 | "homepage": "https://github.com/tkaratug/powered-enum", 5 | "license": "MIT", 6 | "type": "library", 7 | "authors": [ 8 | { 9 | "name": "Turan Karatuğ", 10 | "email": "tkaratug@hotmail.com.tr" 11 | } 12 | ], 13 | "require": { 14 | "php": "^8.1" 15 | }, 16 | "require-dev": { 17 | "phpunit/phpunit": "^9.5" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Tkaratug\\PoweredEnum\\": "src/" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "Tkaratug\\PoweredEnum\\Tests\\": "tests/" 27 | } 28 | }, 29 | "scripts": { 30 | "test": "vendor/bin/phpunit tests/*.php", 31 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 32 | }, 33 | "config": { 34 | "sort-packages": true 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/PoweredEnum.php: -------------------------------------------------------------------------------- 1 | $case->name, $cases)) 48 | ); 49 | } 50 | 51 | public static function getValuesExcept(array $values): array 52 | { 53 | return array_values( 54 | array_diff(self::getValues(), array_map(fn ($value) => $value->value, $values)) 55 | ); 56 | } 57 | 58 | public static function toArrayExcept(array $cases): array 59 | { 60 | return array_diff(self::toArray(), self::toArray($cases)); 61 | } 62 | 63 | public static function getRandomName(): string 64 | { 65 | return self::getNames()[array_rand(self::getNames())]; 66 | } 67 | 68 | public static function getRandomValue(): mixed 69 | { 70 | return self::getValues()[array_rand(self::getValues())]; 71 | } 72 | 73 | public static function getRandomCase(): self 74 | { 75 | return self::cases()[array_rand(self::cases())]; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /tests/PoweredEnumTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($enum->is(PoweredEnumTestStub::ONE)); 20 | $this->assertFalse($enum->is(PoweredEnumTestStub::TWO)); 21 | } 22 | 23 | /** 24 | * @test 25 | */ 26 | public function it_checks_if_enum_value_does_not_equal_to_given_value(): void 27 | { 28 | // Arrange 29 | $enum = PoweredEnumTestStub::ONE; 30 | 31 | // Assert 32 | $this->assertFalse($enum->isNot(PoweredEnumTestStub::ONE)); 33 | $this->assertTrue($enum->isNot(PoweredEnumTestStub::TWO)); 34 | } 35 | 36 | /** 37 | * @test 38 | */ 39 | public function it_checks_if_the_enum_has_given_name(): void 40 | { 41 | // Assert 42 | $this->assertTrue(PoweredEnumTestStub::hasName('ONE')); 43 | $this->assertFalse(PoweredEnumTestStub::hasName('FOUR')); 44 | } 45 | 46 | /** 47 | * @test 48 | */ 49 | public function it_checks_if_the_enum_has_given_value(): void 50 | { 51 | // Assert 52 | $this->assertTrue(PoweredEnumTestStub::hasValue(1)); 53 | $this->assertFalse(PoweredEnumTestStub::hasValue(4)); 54 | } 55 | 56 | /** 57 | * @test 58 | */ 59 | public function it_returns_an_array_of_names_of_the_enum_cases(): void 60 | { 61 | // Act 62 | $response = PoweredEnumTestStub::getNames(); 63 | $filteredResponse = PoweredEnumTestStub::getNames([ 64 | PoweredEnumTestStub::ONE, 65 | PoweredEnumTestStub::TWO, 66 | ]); 67 | 68 | // Assert 69 | $this->assertIsArray($response); 70 | $this->assertSame( 71 | ['ONE', 'TWO', 'THREE'], 72 | $response 73 | ); 74 | 75 | $this->assertIsArray($filteredResponse); 76 | $this->assertSame( 77 | ['ONE', 'TWO'], 78 | $filteredResponse 79 | ); 80 | } 81 | 82 | /** 83 | * @test 84 | */ 85 | public function it_returns_an_array_of_values_of_the_enum_cases(): void 86 | { 87 | // Act 88 | $response = PoweredEnumTestStub::getValues(); 89 | $filteredResponse = PoweredEnumTestStub::getValues([ 90 | PoweredEnumTestStub::ONE, 91 | PoweredEnumTestStub::TWO, 92 | ]); 93 | 94 | // Assert 95 | $this->assertIsArray($response); 96 | $this->assertSame( 97 | [1, 2, 3], 98 | $response 99 | ); 100 | 101 | $this->assertIsArray($filteredResponse); 102 | $this->assertSame( 103 | [1, 2], 104 | $filteredResponse 105 | ); 106 | } 107 | 108 | /** 109 | * @test 110 | */ 111 | public function it_returns_a_combined_array_of_values_and_names_of_the_enum_cases(): void 112 | { 113 | // Act 114 | $response = PoweredEnumTestStub::toArray(); 115 | $filteredResponse = PoweredEnumTestStub::toArray([ 116 | PoweredEnumTestStub::ONE, 117 | PoweredEnumTestStub::TWO, 118 | ]); 119 | 120 | // Assert 121 | $this->assertIsArray($response); 122 | $this->assertSame( 123 | [1 => 'ONE', 2 => 'TWO', 3 => 'THREE'], 124 | $response 125 | ); 126 | 127 | $this->assertIsArray($filteredResponse); 128 | $this->assertSame( 129 | [1 => 'ONE', 2 => 'TWO'], 130 | $filteredResponse 131 | ); 132 | } 133 | 134 | /** 135 | * @test 136 | */ 137 | public function it_returns_an_array_of_names_of_the_enum_cases_except_given_ones(): void 138 | { 139 | // Act 140 | $response = PoweredEnumTestStub::getNamesExcept([PoweredEnumTestStub::ONE]); 141 | 142 | // Assert 143 | $this->assertIsArray($response); 144 | $this->assertSame( 145 | ['TWO', 'THREE'], 146 | $response 147 | ); 148 | } 149 | 150 | /** 151 | * @test 152 | */ 153 | public function it_returns_an_array_of_values_of_the_enum_cases_except_given_ones(): void 154 | { 155 | // Act 156 | $response = PoweredEnumTestStub::getValuesExcept([PoweredEnumTestStub::ONE]); 157 | 158 | // Assert 159 | $this->assertIsArray($response); 160 | $this->assertSame( 161 | [2, 3], 162 | $response 163 | ); 164 | } 165 | 166 | /** 167 | * @test 168 | */ 169 | public function it_returns_a_combined_array_of_values_and_names_of_the_enum_cases_except_given_ones(): void 170 | { 171 | // Act 172 | $response = PoweredEnumTestStub::toArrayExcept([PoweredEnumTestStub::ONE]); 173 | 174 | // Assert 175 | $this->assertIsArray($response); 176 | $this->assertSame( 177 | [2 => 'TWO', 3 => 'THREE'], 178 | $response 179 | ); 180 | } 181 | 182 | /** 183 | * @test 184 | */ 185 | public function it_returns_a_random_name_of_enum_cases(): void 186 | { 187 | // Assert 188 | $this->assertSame('ONE', SingleCaseEnumTestStub::getRandomName()); 189 | } 190 | 191 | /** 192 | * @test 193 | */ 194 | public function it_returns_a_random_value_of_enum_cases(): void 195 | { 196 | // Assert 197 | $this->assertSame(1, SingleCaseEnumTestStub::getRandomValue()); 198 | } 199 | 200 | /** 201 | * @test 202 | */ 203 | public function it_returns_a_random_case_of_enum_cases(): void 204 | { 205 | // Assert 206 | $this->assertSame(SingleCaseEnumTestStub::ONE, SingleCaseEnumTestStub::getRandomCase()); 207 | } 208 | } 209 | 210 | enum PoweredEnumTestStub: int 211 | { 212 | use PoweredEnum; 213 | 214 | case ONE = 1; 215 | case TWO = 2; 216 | case THREE = 3; 217 | } 218 | 219 | enum SingleCaseEnumTestStub: int 220 | { 221 | use PoweredEnum; 222 | 223 | case ONE = 1; 224 | } 225 | --------------------------------------------------------------------------------