mirror of
https://codeberg.org/vlw/php-xenum.git
synced 2025-09-13 20:23:41 +02:00
Compare commits
No commits in common. "master" and "1.0.1" have entirely different histories.
4 changed files with 27 additions and 56 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,7 +5,6 @@
|
|||
/public/storage
|
||||
/storage/*.key
|
||||
/vendor
|
||||
bin
|
||||
.env
|
||||
.env.backup
|
||||
.phpunit.result.cache
|
||||
|
|
63
README.md
63
README.md
|
@ -7,7 +7,7 @@ This library adds a few useful traits to your PHP Enums that compliment existing
|
|||
For example,
|
||||
|
||||
```php
|
||||
use vlw\xEnum;
|
||||
use \victorwesterlund\xEnum;
|
||||
|
||||
enum HelloWorld: string {
|
||||
use xEnum;
|
||||
|
@ -22,28 +22,6 @@ HelloWorld::fromName("FOO"); // HelloWorld::FOO
|
|||
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
|
||||
|
||||
All methods implemented by this library
|
||||
|
@ -54,7 +32,6 @@ Method
|
|||
[Enum::tryFromName(int\|string\|null): static\|null](#enumtryfromname)
|
||||
[Enum::names(): array](#enumnames)
|
||||
[Enum::values(): array](#enumvalues)
|
||||
[Enum::entries(): array](#enumentries)
|
||||
|
||||
## Enum::fromName()
|
||||
|
||||
|
@ -76,7 +53,7 @@ enum HelloWorld: string {
|
|||
case BAZ = "QUX";
|
||||
}
|
||||
|
||||
HelloWorld::fromName("FOO"); // HelloWorld::FOO
|
||||
HelloWorld::fromName("BAR"); // HelloWorld::FOO
|
||||
HelloWorld::fromName("MOM") // ValueError: 'MOM' is not a valid case for HelloWorld
|
||||
```
|
||||
|
||||
|
@ -100,7 +77,7 @@ enum HelloWorld: string {
|
|||
case BAZ = "QUX";
|
||||
}
|
||||
|
||||
HelloWorld::tryFromName("FOO"); // HelloWorld::FOO
|
||||
HelloWorld::tryFromName("BAR"); // HelloWorld::FOO
|
||||
HelloWorld::tryFromName("MOM") // null
|
||||
```
|
||||
|
||||
|
@ -130,7 +107,7 @@ HelloWorld::names(); // ["FOO", "BAZ"]
|
|||
Return sequential array of Enum case values
|
||||
|
||||
```php
|
||||
Enum::entries(): array
|
||||
Enum::values(): array
|
||||
```
|
||||
|
||||
Example:
|
||||
|
@ -146,23 +123,23 @@ enum HelloWorld: string {
|
|||
HelloWorld::values(); // ["BAR", "QUX"]
|
||||
```
|
||||
|
||||
## Enum::entries()
|
||||
# How to use
|
||||
|
||||
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)
|
||||
Requires PHP 8.0 or newer
|
||||
|
||||
```php
|
||||
Enum::entries(): array
|
||||
```
|
||||
1. **Install from composer**
|
||||
```
|
||||
composer require victorwesterlund/xenum
|
||||
```
|
||||
|
||||
Example:
|
||||
2. **`use` in your project**
|
||||
```php
|
||||
use \victorwesterlund\xEnum;
|
||||
```
|
||||
|
||||
```php
|
||||
enum HelloWorld: string {
|
||||
use xEnum;
|
||||
|
||||
case FOO = "BAR";
|
||||
case BAZ = "QUX";
|
||||
}
|
||||
|
||||
HelloWorld::entries(); // ["FOO" => "BAR", "BAZ" => "QUX"]
|
||||
```
|
||||
3. **`use` with your Enums**
|
||||
```php
|
||||
enum HelloWorld {
|
||||
use xEnum;
|
||||
}
|
||||
```
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
{
|
||||
"name": "vlw/xenum",
|
||||
"name": "victorwesterlund/xenum",
|
||||
"description": "PHP eXtended Enums. The missing quality-of-life features from PHP 8+ Enums",
|
||||
"type": "library",
|
||||
"license": "GPL-3.0-only",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Victor Westerlund",
|
||||
"email": "victor@vlw.se"
|
||||
"email": "victor.vesterlund@gmail.com"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "dev",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"vlw\\": "src/"
|
||||
"victorwesterlund\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace vlw;
|
||||
namespace victorwesterlund;
|
||||
|
||||
/*
|
||||
PHP eXtended Enums.
|
||||
The missing quality-of-life features from PHP 8+ Enums.
|
||||
https://codeberg.org/vlw/php-xenum
|
||||
https://github.com/victorwesterlund/php-xenum
|
||||
*/
|
||||
trait xEnum {
|
||||
// Resolve enum case from enum name or return null
|
||||
|
@ -23,7 +23,7 @@
|
|||
// Throw a ValueError if Enum name is not found
|
||||
public static function fromName(?string $name): static {
|
||||
$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
|
||||
|
@ -35,9 +35,4 @@
|
|||
public static function values(): array {
|
||||
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