-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from sunrise-php/release/v2.6.0
v2.6.0
- Loading branch information
Showing
12 changed files
with
457 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,30 @@ | ||
build: | ||
image: default-bionic | ||
environment: | ||
php: | ||
version: '8.1' | ||
nodes: | ||
analysis: | ||
environment: | ||
php: 8.1 | ||
tests: | ||
override: | ||
- php-scrutinizer-run | ||
coverage: | ||
environment: | ||
php: 8.1 | ||
tests: | ||
override: | ||
- command: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover coverage.xml | ||
coverage: | ||
file: coverage.xml | ||
format: clover | ||
php80: | ||
environment: | ||
php: 8.0 | ||
tests: | ||
override: | ||
- command: php vendor/bin/phpunit | ||
php74: | ||
environment: | ||
php: 7.4 | ||
tests: | ||
override: | ||
- command: php vendor/bin/phpunit |
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,171 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* It's free open-source software released under the MIT License. | ||
* | ||
* @author Anatoly Fenric <[email protected]> | ||
* @copyright Copyright (c) 2021, Anatoly Fenric | ||
* @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE | ||
* @link https://github.com/sunrise-php/hydrator | ||
*/ | ||
|
||
namespace Sunrise\Hydrator; | ||
|
||
/** | ||
* Import classes | ||
*/ | ||
use JsonSerializable; | ||
use ReflectionClass; | ||
use RuntimeException; | ||
|
||
/** | ||
* Import functions | ||
*/ | ||
use function is_int; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
/** | ||
* Abstract enum | ||
* | ||
* @since 2.6.0 | ||
*/ | ||
abstract class Enum implements JsonSerializable | ||
{ | ||
|
||
/** | ||
* Cached cases of the enum | ||
* | ||
* @var array<class-string<static>, list<static>> | ||
*/ | ||
private static array $cases = []; | ||
|
||
/** | ||
* The name of the enum's case | ||
* | ||
* @var string | ||
* | ||
* @readonly | ||
*/ | ||
private string $name; | ||
|
||
/** | ||
* The value of the enum's case | ||
* | ||
* @var int|string | ||
* | ||
* @readonly | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* Constructor of the class | ||
* | ||
* @param string $name | ||
* @param int|string $value | ||
*/ | ||
final protected function __construct(string $name, $value) | ||
{ | ||
$this->name = $name; | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* Gets the name of the enum's case | ||
* | ||
* @return string | ||
*/ | ||
final public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Gets the value of the enum's case | ||
* | ||
* @return int|string | ||
*/ | ||
final public function value() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* Gets all cases of the enum | ||
* | ||
* @return list<static> | ||
*/ | ||
final public static function cases(): array | ||
{ | ||
if (isset(self::$cases[static::class])) { | ||
return self::$cases[static::class]; | ||
} | ||
|
||
$class = new ReflectionClass(static::class); | ||
$constants = $class->getReflectionConstants(); | ||
foreach ($constants as $constant) { | ||
$owner = $constant->getDeclaringClass(); | ||
if ($owner->getName() === self::class) { | ||
continue; | ||
} | ||
|
||
$name = $constant->getName(); | ||
$value = $constant->getValue(); | ||
|
||
if (!is_int($value) && !is_string($value)) { | ||
continue; | ||
} | ||
|
||
self::$cases[static::class][] = new static($name, $value); | ||
} | ||
|
||
return self::$cases[static::class]; | ||
} | ||
|
||
/** | ||
* Tries to initialize the enum from the given case's value | ||
* | ||
* @param int|string $value | ||
* | ||
* @return static|null | ||
*/ | ||
final public static function tryFrom($value) | ||
{ | ||
foreach (self::cases() as $case) { | ||
if ($case->value == $value) { | ||
return $case; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Tries to initialize the enum from the given case's name | ||
* | ||
* @param string $name | ||
* | ||
* @return static | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
final public static function __callStatic(string $name, array $arguments = []) | ||
{ | ||
foreach (self::cases() as $case) { | ||
if ($case->name === $name) { | ||
return $case; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('Enum case %1$s::%2$s not found', static::class, $name)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
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
Oops, something went wrong.