-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
472 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Helper functions | ||
|
||
To accommodate situations that haven't been solved (yet), a few helper | ||
functions. | ||
|
||
Each helper function that works with values has a lowercase version | ||
for easy usage. | ||
|
||
Each helper function has a full name version and a shortcut, just pick your | ||
personal favorite poison. | ||
|
||
Each function also accepts null and returns null when | ||
this happens. This eases usage with optional situations. | ||
|
||
## Backing | ||
|
||
In some situations, libraries or frameworks only accept backed enumerations | ||
and/or Stringable objects. Or you want to use the name/value of either backed or | ||
basic enums. This method gives you a proxy object with name and value | ||
properties. | ||
|
||
Note: an example of where this is useful is in Laravel's Query Builder. | ||
|
||
### Examples | ||
|
||
```php | ||
enum MyEnum { | ||
case Enum; | ||
case Other; | ||
} | ||
|
||
enum MyStringEnum: string { | ||
case String = 'My string'; | ||
} | ||
|
||
\Henzeb\Enumhancer\Functions\b(MyEnum::Enum)->name; // Enum | ||
\Henzeb\Enumhancer\Functions\b(MyEnum::Enum)->value; // Enum | ||
(string)\Henzeb\Enumhancer\Functions\b(MyEnum::Enum); // Enum | ||
|
||
\Henzeb\Enumhancer\Functions\b(MyEnum::Enum, false)->value; // enum | ||
(string)\Henzeb\Enumhancer\Functions\b(MyEnum::Enum, false); // enum | ||
|
||
\Henzeb\Enumhancer\Functions\b(MyStringEnum::String, false)->value; // My string | ||
(string)\Henzeb\Enumhancer\Functions\b(MyStringEnum::String, false); // My string | ||
|
||
\Henzeb\Enumhancer\Functions\backing(MyEnum::Enum)->name; // Enum | ||
\Henzeb\Enumhancer\Functions\backing(MyEnum::Enum)->value; // Enum | ||
(string)\Henzeb\Enumhancer\Functions\backing(MyEnum::Enum); // Enum | ||
|
||
\Henzeb\Enumhancer\Functions\backing(MyEnum::Enum, false)->value; // enum | ||
(string)\Henzeb\Enumhancer\Functions\backing(MyEnum::Enum, false); // enum | ||
|
||
\Henzeb\Enumhancer\Functions\backing(MyStringEnum::String, false)->value; // My string | ||
(string)\Henzeb\Enumhancer\Functions\backing(MyStringEnum::String, false); // My string | ||
|
||
# Lower case | ||
\Henzeb\Enumhancer\Functions\bl(MyEnum::Enum)->name; // Enum | ||
\Henzeb\Enumhancer\Functions\bl(MyEnum::Enum)->value; // enum | ||
(string)\Henzeb\Enumhancer\Functions\bl(MyEnum::Enum); // enum | ||
|
||
\Henzeb\Enumhancer\Functions\bl(MyStringEnum::String)->value; // My string | ||
(string)\Henzeb\Enumhancer\Functions\bl(MyStringEnum::String); // My string | ||
|
||
\Henzeb\Enumhancer\Functions\backingLowercase(MyEnum::Enum)->name; // Enum | ||
\Henzeb\Enumhancer\Functions\backingLowercase(MyEnum::Enum)->value; // enum | ||
(string)\Henzeb\Enumhancer\Functions\backingLowercase(MyEnum::Enum); // enum | ||
|
||
\Henzeb\Enumhancer\Functions\backingLowercase(MyStringEnum::String)->value; // My string | ||
(string)\Henzeb\Enumhancer\Functions\backingLowercase(MyStringEnum::String); // My string | ||
|
||
``` | ||
|
||
## Name | ||
|
||
This function is particular useful when you want to use it as an array key. | ||
|
||
```php | ||
enum MyEnum { | ||
case Enum; | ||
} | ||
|
||
\Henzeb\Enumhancer\Functions\n(MyEnum::Enum); // Enum | ||
\Henzeb\Enumhancer\Functions\name(MyEnum::Enum); // Enum | ||
``` | ||
|
||
## Value | ||
|
||
This function returns the value of your Enum. Works just like | ||
[Value](value.md), except for the lower case variants which return the | ||
lower case version of the enum name. | ||
|
||
```php | ||
enum MyEnum { | ||
case Enum; | ||
} | ||
|
||
enum MyStringEnum: string { | ||
case String = 'String'; | ||
} | ||
|
||
\Henzeb\Enumhancer\Functions\v(MyEnum::Enum); // Enum | ||
\Henzeb\Enumhancer\Functions\v(MyEnum::Enum, false); // enum | ||
\Henzeb\Enumhancer\Functions\v(MyStringEnum::String, false); // My string | ||
|
||
\Henzeb\Enumhancer\Functions\value(MyEnum::Enum); // Enum | ||
\Henzeb\Enumhancer\Functions\value(MyEnum::Enum, false); // enum | ||
\Henzeb\Enumhancer\Functions\value(MyStringEnum::String, false); // My string | ||
|
||
# Lower case | ||
\Henzeb\Enumhancer\Functions\vl(MyEnum::Enum); // enum | ||
\Henzeb\Enumhancer\Functions\vl(MyStringEnum::String); // My string | ||
|
||
\Henzeb\Enumhancer\Functions\valueLowercase(MyEnum::Enum); // enum | ||
\Henzeb\Enumhancer\Functions\valueLowercase(MyStringEnum::String); // My string | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Functions; | ||
|
||
use UnitEnum; | ||
use Henzeb\Enumhancer\Helpers\EnumProxy; | ||
use Henzeb\Enumhancer\Helpers\EnumValue; | ||
|
||
/** | ||
* returns a proxy object with a value property equal to it's name when given enum is not backed | ||
* @param UnitEnum|null $enum | ||
* @param bool $keepValueCase | ||
* @return EnumProxy|null | ||
*/ | ||
function b(?UnitEnum $enum, bool $keepValueCase = true): ?EnumProxy | ||
{ | ||
if(!$enum) { | ||
return null; | ||
} | ||
|
||
return new EnumProxy($enum, $keepValueCase); | ||
} | ||
|
||
function backing(?UnitEnum $enum, bool $keepValueCase = true): ?EnumProxy | ||
{ | ||
return b($enum, $keepValueCase); | ||
} | ||
|
||
/** | ||
* returns a proxy object with a lower cased value equal to it's name when given enum is not backed | ||
* | ||
* @param UnitEnum|null $enum | ||
* @return EnumProxy|null | ||
*/ | ||
function bl(?UnitEnum $enum): ?EnumProxy | ||
{ | ||
return b($enum, false); | ||
} | ||
|
||
function backingLowercase(?UnitEnum $enum): ?EnumProxy | ||
{ | ||
return bl($enum); | ||
} | ||
|
||
/** | ||
* returns the name of the given enum. Useful when using it as an array key. | ||
* | ||
* @param UnitEnum|null $enum | ||
* @return string|null | ||
*/ | ||
function n(?UnitEnum $enum): ?string | ||
{ | ||
return $enum?->name; | ||
} | ||
|
||
function name(?UnitEnum $enum): ?string | ||
{ | ||
return n($enum); | ||
} | ||
|
||
/** | ||
* Returns a value equal to it's name when given enum is not backed by default | ||
* | ||
* @param UnitEnum|null $enum | ||
* @param bool $keepValueCase returns a lower cased enum name when it's a UnitEnum | ||
* @return string|int|null | ||
*/ | ||
function v(?UnitEnum $enum, bool $keepValueCase = true): string|int|null | ||
{ | ||
if(!$enum) { | ||
return null; | ||
} | ||
return EnumValue::value($enum, $keepValueCase); | ||
} | ||
|
||
|
||
function value(?UnitEnum $enum, bool $keepValueCase = true): string|int|null | ||
{ | ||
return v($enum, $keepValueCase); | ||
} | ||
|
||
/** | ||
* Returns a lowercase value equal to it's name when given enum is not backed. | ||
* | ||
* @param UnitEnum|null $enum | ||
* @return string|int|null | ||
*/ | ||
function vl(?UnitEnum $enum): string|int|null | ||
{ | ||
return value($enum, false); | ||
} | ||
|
||
function valueLowercase(?UnitEnum $enum): string|int|null | ||
{ | ||
return vl($enum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Helpers; | ||
|
||
use UnitEnum; | ||
use Stringable; | ||
use function Henzeb\Enumhancer\Functions\v; | ||
|
||
class EnumProxy implements Stringable | ||
{ | ||
public readonly string $name; | ||
public readonly string $value; | ||
|
||
public function __construct(private readonly UnitEnum $enum, bool $keepValueCase) | ||
{ | ||
$this->name = $this->enum->name; | ||
$this->value = v($this->enum, $keepValueCase); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.