|
| 1 | +# Platenum |
| 2 | + |
| 3 | +[](https://travis-ci.org/thunderer/Platenum) |
| 4 | +[](https://packagist.org/packages/thunderer/platenum) |
| 5 | +[](https://packagist.org/packages/thunderer/platenum) |
| 6 | + |
| 7 | +Platenum provides a flexible and feature-complete solution for [enumerations (enums)](https://en.wikipedia.org/wiki/Enumerated_type) in PHP with no external dependencies. The name comes from the Latin term for a [Platinum](https://en.wikipedia.org/wiki/Platinum) chemical element. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +This library is available on Packagist and can be installed with Composer in projects supporting PHP 7.1 and above: |
| 12 | + |
| 13 | +``` |
| 14 | +composer require thunderer/platenum |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Create a new class with members definition: |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | +declare(strict_types=1); |
| 24 | +namespace X; |
| 25 | + |
| 26 | +use Thunder\Platenum\Enum\ConstantsEnumTrait; |
| 27 | + |
| 28 | +/** |
| 29 | + * @method static self ACTIVE() |
| 30 | + * @method static self INACTIVE() |
| 31 | + * @method static self SUSPENDED() |
| 32 | + * @method static self DISABLED() |
| 33 | + */ |
| 34 | +final class AccountStatusEnum |
| 35 | +{ |
| 36 | + private const ACTIVE = 1; |
| 37 | + private const INACTIVE = 2; |
| 38 | + private const SUSPENDED = 3; |
| 39 | + private const DISABLED = 4; |
| 40 | + |
| 41 | + use ConstantsEnumTrait; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +> Tip: To enable autocomplete for the constant methods, include the [`@method` declarations](http://docs.phpdoc.org/references/phpdoc/tags/method.html) as shown in the listing above. |
| 46 | +
|
| 47 | +Members instances can be created using either constants methods, members names, or their values: |
| 48 | + |
| 49 | +```php |
| 50 | +$active = AccountStatusEnum::ACTIVE(); |
| 51 | +$alsoActive = AccountStatusEnum::fromMember('ACTIVE'); |
| 52 | +$stillActive = AccountStatusEnum::fromValue(1); |
| 53 | +``` |
| 54 | + |
| 55 | +Enums can be compared using strict `===` operator or an `equals()` method: |
| 56 | + |
| 57 | +```php |
| 58 | +assert($active === $alsoActive); |
| 59 | +assert(true === $active->equals($alsoActive)); |
| 60 | +``` |
| 61 | + |
| 62 | +> Note: Strict comparison `===` should be always preferred. Loose `==` comparison will also work correctly, but it has [loads of quirks](http://php.net/manual/en/language.oop5.object-comparison.php). |
| 63 | +
|
| 64 | +The `getValue()` method returns the raw value of given instance: |
| 65 | + |
| 66 | +```php |
| 67 | +assert(1 === $active->getValue()); |
| 68 | +``` |
| 69 | + |
| 70 | +**enum generator** |
| 71 | + |
| 72 | +Classes can be automatically generated using built-in `bin/generate` utility. It accepts three parameters: |
| 73 | +- location of its members (either `constants`, `docblock` or `static`), |
| 74 | +- (fully qualified) class name, Platenum matches the namespace to your autoloading configuration and puts the file in the proper directory, |
| 75 | +- members names with optional values where supported. |
| 76 | + |
| 77 | +Example: |
| 78 | + |
| 79 | +``` |
| 80 | +bin/generate constants Thunder\\Platenum\\YourEnum FOO=1,BAR=3 |
| 81 | +bin/generate docblock Thunder\\Platenum\\YourEnum FOO,BAR |
| 82 | +bin/generate static Thunder\\Platenum\\YourEnum FOO,BAR=3 |
| 83 | +``` |
| 84 | + |
| 85 | +## Sources |
| 86 | + |
| 87 | +There are multiple sources from which Platenum can read enumeration members. Base `EnumTrait` provides all enum functionality without any source, to be defined in a static `resolve()` method. Each source is available both as a `trait` which uses `EnumTrait` with concrete `resolve()` method implementation and an `abstract class` based on that trait. Usage of traits is recommended as target enum classes should not have any common type hint. |
| 88 | + |
| 89 | +In this section the `BooleanEnum` class with two members (`TRUE=true` and `FALSE=false`) will be used as an example. |
| 90 | + |
| 91 | +**class constants** |
| 92 | + |
| 93 | +```php |
| 94 | +final class BooleanEnum |
| 95 | +{ |
| 96 | + use ConstantsEnumTrait; |
| 97 | + |
| 98 | + private const TRUE = true; |
| 99 | + private const FALSE = false; |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +```php |
| 104 | +final class BooleanEnum extends AbstractConstantsEnum |
| 105 | +{ |
| 106 | + private const TRUE = true; |
| 107 | + private const FALSE = false; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +**class docblock** |
| 112 | + |
| 113 | +> Note: There is no way to specify members values inside docblock, therefore all members names are also their values - in this case `TRUE='TRUE'` and `FALSE='FALSE'`. |
| 114 | +
|
| 115 | +```php |
| 116 | +/** |
| 117 | + * @method static self TRUE() |
| 118 | + * @method static self FALSE() |
| 119 | + */ |
| 120 | +final class BooleanEnum |
| 121 | +{ |
| 122 | + use DocblockEnumTrait; |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +```php |
| 127 | +/** |
| 128 | + * @method static self TRUE() |
| 129 | + * @method static self FALSE() |
| 130 | + */ |
| 131 | +final class BooleanEnum extends AbstractDocblockEnum {} |
| 132 | +``` |
| 133 | + |
| 134 | +**static property** |
| 135 | + |
| 136 | +```php |
| 137 | +final class BooleanEnum |
| 138 | +{ |
| 139 | + use StaticEnumTrait; |
| 140 | + |
| 141 | + private static $mapping = [ |
| 142 | + 'TRUE' => true, |
| 143 | + 'FALSE' => false, |
| 144 | + ]; |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +```php |
| 149 | +final class BooleanEnum extends AbstractStaticEnum |
| 150 | +{ |
| 151 | + private static $mapping = [ |
| 152 | + 'TRUE' => true, |
| 153 | + 'FALSE' => false, |
| 154 | + ]; |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +**custom source** |
| 159 | + |
| 160 | +> Note: The `resolve` method will be called only once when the enumeration is used for the first time. |
| 161 | +
|
| 162 | +```php |
| 163 | +final class BooleanEnum |
| 164 | +{ |
| 165 | + use EnumTrait; |
| 166 | + |
| 167 | + private static function resolve(): array |
| 168 | + { |
| 169 | + return [ |
| 170 | + 'TRUE' => true, |
| 171 | + 'FALSE' => false, |
| 172 | + ]; |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +## Reasons |
| 178 | + |
| 179 | +There are already a few `enum` libraries in the PHP ecosystem. Why another one? There are several reasons to do so: |
| 180 | + |
| 181 | +**Sources** Platenum allows multiple sources for enumeration members. `EnumTrait` contains all enum functions - extend it with your custom `resolve()` method to create custom source. In fact, all enumeration sources in this repository are defined this way. |
| 182 | + |
| 183 | +**Features** Platenum provides complete feature set for all kinds of operations on enumeration members, values, comparison, transformation, and more. Look at PhpEnumerations project to see the feature matrix created during development of this library. |
| 184 | + |
| 185 | +**Inheritance** Existing solutions use inheritance for creating enum classes: |
| 186 | + |
| 187 | +```php |
| 188 | +class YourEnum extends LibraryEnum |
| 189 | +{ |
| 190 | + const ONE = 1; |
| 191 | + const TWO = 2; |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +Enumerations should be represented as different types without an ability to be used interchangeably. Platenum leverages traits to provide complete class body, therefore `instanceof` comparison will fail as it should and there is no possibility to typehint generic `LibraryEnum` class to allow any enum instance there. |
| 196 | + |
| 197 | +**Comparison** Creating more than one instance of certain enum value should not prohibit you from strictly comparing them like any other variable. Other solutions encourage using loose comparison (`==`) as the instances with the same values are not the same instances of their classes. This library guarantees that the same enum value instance will always be the same instance which can be strictly compared: |
| 198 | + |
| 199 | +```php |
| 200 | +final class YourEnum |
| 201 | +{ |
| 202 | + private const ONE = 1; |
| 203 | + private const TWO = 2; |
| 204 | + |
| 205 | + use EnumTrait; |
| 206 | +} |
| 207 | + |
| 208 | +YourEnum::ONE() === YourEnum::ONE() |
| 209 | +YourEnum::fromValue(1) === YourEnum::ONE() |
| 210 | +YourEnum::fromValue(1) === YourEnum::fromValue(1) |
| 211 | +``` |
| 212 | + |
| 213 | +> Note: If you want to prove me wrong by using reflection or other opcode modifying extensions like `uopz`, then save yourself that effort, you're right and I surrender. |
| 214 | +
|
| 215 | +**Serialization** |
| 216 | + |
| 217 | +Platenum provides correct (de)serialization solution which preserves its single member instance guarantees. |
| 218 | + |
| 219 | +The only exception to that guarantee is when an enum instance is `unserialize()`d inside another class as PHP always creates a new object there. This can be easily mitigated by `fromInstance` replacement helper method inside `__wakeup()` method which accepts its argument by reference and automatically swaps it to a correct instance: |
| 220 | + |
| 221 | +```php |
| 222 | +public function __wakeup() |
| 223 | +{ |
| 224 | + $this->enum->fromInstance($this->enum); |
| 225 | +} |
| 226 | +``` |
| 227 | + |
| 228 | +Note that `equals()` method is not affected as it does not rely on the same object instance but its class and actual value inside. |
| 229 | + |
| 230 | +# License |
| 231 | + |
| 232 | +See LICENSE file in the main directory of this library. |
0 commit comments