mirror of
https://codeberg.org/vlw/php-xenum.git
synced 2025-09-13 20:23:41 +02:00
Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
ba3f43a9e2 | |||
1c997a5574 | |||
8972f06f42 | |||
99b784841e | |||
166f8faf95 | |||
243abfc531 | |||
6644be84ae |
4 changed files with 61 additions and 32 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,6 +5,7 @@
|
||||||
/public/storage
|
/public/storage
|
||||||
/storage/*.key
|
/storage/*.key
|
||||||
/vendor
|
/vendor
|
||||||
|
bin
|
||||||
.env
|
.env
|
||||||
.env.backup
|
.env.backup
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
|
|
67
README.md
67
README.md
|
@ -7,7 +7,7 @@ This library adds a few useful traits to your PHP Enums that compliment existing
|
||||||
For example,
|
For example,
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use \victorwesterlund\xEnum;
|
use vlw\xEnum;
|
||||||
|
|
||||||
enum HelloWorld: string {
|
enum HelloWorld: string {
|
||||||
use xEnum;
|
use xEnum;
|
||||||
|
@ -16,12 +16,34 @@ enum HelloWorld: string {
|
||||||
case BAZ = "QUX";
|
case BAZ = "QUX";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Like Enum::tryFrom() but for Enum names instead of values
|
// Like Enum::from() but for Enum names instead of values
|
||||||
HelloWorld::fromName("FOO"); //
|
HelloWorld::fromName("FOO"); // HelloWorld::FOO
|
||||||
// .. and of course the non-throwing version
|
// And of course the non-throwing version similar to Enum::tryFrom()
|
||||||
HelloWorld::tryFromName("MOM"); // null
|
HelloWorld::tryFromName("MOM"); // null
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# How to use
|
||||||
|
|
||||||
|
Requires PHP 8.0 or newer
|
||||||
|
|
||||||
|
1. **Install from composer**
|
||||||
|
```
|
||||||
|
composer require vlw/xenum
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **`use` in your project**
|
||||||
|
```php
|
||||||
|
use vlw\xEnum;
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **`use` with your Enums**
|
||||||
|
```php
|
||||||
|
enum HelloWorld {
|
||||||
|
use xEnum;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
# Methods
|
# Methods
|
||||||
|
|
||||||
All methods implemented by this library
|
All methods implemented by this library
|
||||||
|
@ -32,6 +54,7 @@ Method
|
||||||
[Enum::tryFromName(int\|string\|null): static\|null](#enumtryfromname)
|
[Enum::tryFromName(int\|string\|null): static\|null](#enumtryfromname)
|
||||||
[Enum::names(): array](#enumnames)
|
[Enum::names(): array](#enumnames)
|
||||||
[Enum::values(): array](#enumvalues)
|
[Enum::values(): array](#enumvalues)
|
||||||
|
[Enum::entries(): array](#enumentries)
|
||||||
|
|
||||||
## Enum::fromName()
|
## Enum::fromName()
|
||||||
|
|
||||||
|
@ -53,7 +76,7 @@ enum HelloWorld: string {
|
||||||
case BAZ = "QUX";
|
case BAZ = "QUX";
|
||||||
}
|
}
|
||||||
|
|
||||||
HelloWorld::fromName("BAR"); // HelloWorld::FOO
|
HelloWorld::fromName("FOO"); // HelloWorld::FOO
|
||||||
HelloWorld::fromName("MOM") // ValueError: 'MOM' is not a valid case for HelloWorld
|
HelloWorld::fromName("MOM") // ValueError: 'MOM' is not a valid case for HelloWorld
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -77,7 +100,7 @@ enum HelloWorld: string {
|
||||||
case BAZ = "QUX";
|
case BAZ = "QUX";
|
||||||
}
|
}
|
||||||
|
|
||||||
HelloWorld::tryFromName("BAR"); // HelloWorld::FOO
|
HelloWorld::tryFromName("FOO"); // HelloWorld::FOO
|
||||||
HelloWorld::tryFromName("MOM") // null
|
HelloWorld::tryFromName("MOM") // null
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -107,7 +130,7 @@ HelloWorld::names(); // ["FOO", "BAZ"]
|
||||||
Return sequential array of Enum case values
|
Return sequential array of Enum case values
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Enum::values(): array
|
Enum::entries(): array
|
||||||
```
|
```
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
@ -123,23 +146,23 @@ enum HelloWorld: string {
|
||||||
HelloWorld::values(); // ["BAR", "QUX"]
|
HelloWorld::values(); // ["BAR", "QUX"]
|
||||||
```
|
```
|
||||||
|
|
||||||
# How to use
|
## Enum::entries()
|
||||||
|
|
||||||
Requires PHP 8.0 or newer
|
Return an associative array of Enum names and values. This method is similar to [JavaScript's Object.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
|
||||||
|
|
||||||
1. **Install from composer**
|
```php
|
||||||
```
|
Enum::entries(): array
|
||||||
composer require victorwesterlund/xenum
|
```
|
||||||
```
|
|
||||||
|
|
||||||
2. **`use` in your project**
|
Example:
|
||||||
```php
|
|
||||||
use \victorwesterlund\xEnum;
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **`use` with your Enums**
|
```php
|
||||||
```php
|
enum HelloWorld: string {
|
||||||
enum HelloWorld {
|
|
||||||
use xEnum;
|
use xEnum;
|
||||||
}
|
|
||||||
```
|
case FOO = "BAR";
|
||||||
|
case BAZ = "QUX";
|
||||||
|
}
|
||||||
|
|
||||||
|
HelloWorld::entries(); // ["FOO" => "BAR", "BAZ" => "QUX"]
|
||||||
|
```
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
{
|
{
|
||||||
"name": "victorwesterlund/xenum",
|
"name": "vlw/xenum",
|
||||||
"description": "PHP eXtended Enums. The missing quality-of-life features from PHP 8+ Enums",
|
"description": "PHP eXtended Enums. The missing quality-of-life features from PHP 8+ Enums",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "GPL-3.0-only",
|
"license": "GPL-3.0-only",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Victor Westerlund",
|
"name": "Victor Westerlund",
|
||||||
"email": "victor.vesterlund@gmail.com"
|
"email": "victor@vlw.se"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"victorwesterlund\\": "src/"
|
"vlw\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {}
|
"require": {}
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace victorwesterlund;
|
namespace vlw;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
PHP eXtended Enums.
|
PHP eXtended Enums.
|
||||||
The missing quality-of-life features from PHP 8+ Enums.
|
The missing quality-of-life features from PHP 8+ Enums.
|
||||||
https://github.com/victorwesterlund/php-xenum
|
https://codeberg.org/vlw/php-xenum
|
||||||
*/
|
*/
|
||||||
trait xEnum {
|
trait xEnum {
|
||||||
// Resolve enum case from enum name or return null
|
// Resolve enum case from enum name or return null
|
||||||
public static function tryFromName(string|null $name): static|null {
|
public static function tryFromName(?string $name): ?static {
|
||||||
foreach (self::cases() as $case) {
|
foreach (self::cases() as $case) {
|
||||||
if (strtoupper($name) === $case->name) {
|
if (strtoupper($name) === $case->name) {
|
||||||
return $case;
|
return $case;
|
||||||
|
@ -21,9 +21,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throw a ValueError if Enum name is not found
|
// Throw a ValueError if Enum name is not found
|
||||||
public static function fromName(string|null $name): static {
|
public static function fromName(?string $name): static {
|
||||||
$case = self::tryFromName($name);
|
$case = self::tryFromName($name);
|
||||||
return $case ? $case : throw new ValueError("'${name}' is not a valid case for enum " . self::class);
|
return $case ? $case : throw new \ValueError("'{$name}' is not a valid case for enum " . self::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return array of enum names
|
// Return array of enum names
|
||||||
|
@ -35,4 +35,9 @@
|
||||||
public static function values(): array {
|
public static function values(): array {
|
||||||
return array_column(self::cases(), "value");
|
return array_column(self::cases(), "value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return assoc array of enum names and values
|
||||||
|
public static function entries(): array {
|
||||||
|
return array_combine(self::names(), self::values());
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue