-
-
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.
- Loading branch information
1 parent
9aeb393
commit d573858
Showing
6 changed files
with
158 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
*/ | ||
abstract class SplEnum extends SplType | ||
{ | ||
use SplEnumTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
|
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,78 @@ | ||
<?php | ||
|
||
/** | ||
* Part of SplTypes package. | ||
* | ||
* (c) Adrien Loyant <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ducks\Component\SplTypes; | ||
|
||
/** | ||
* Trait used for enum emulation | ||
* | ||
* @psalm-api | ||
*/ | ||
trait SplEnumTrait | ||
{ | ||
/** | ||
* Generates a list of cases on an enum | ||
* | ||
* @return array An array of all defined cases of this enumeration, in order of declaration. | ||
*/ | ||
public static function cases(): array | ||
{ | ||
static $cases = null; | ||
|
||
if (!isset($cases)) { | ||
$enum = new \ReflectionEnum(static::class); | ||
foreach ($enum->getCases() as $case) { | ||
$cases[] = $case->getValue(); | ||
} | ||
} | ||
|
||
return $cases ?? []; | ||
} | ||
|
||
/** | ||
* Maps a scalar to an enum instance | ||
* | ||
* @param int|string $value The scalar value to map to an enum case. | ||
* | ||
* @return SplEnum A case instance of this enumeration. | ||
*/ | ||
final public static function from($value): self | ||
{ | ||
$case = static::tryFrom($value); | ||
|
||
if (null === $case) { | ||
throw new \ValueError( | ||
sprintf('%s is not a valid backing value for enum "%s"', \json_encode($value), static::class) | ||
); | ||
} | ||
|
||
return $case; | ||
} | ||
|
||
/** | ||
* Maps a scalar to an enum instance or null | ||
* | ||
* @param int|string $value e scalar value to map to an enum case. | ||
* | ||
* @return SplEnum|null A case instance of this enumeration, or null if not found. | ||
*/ | ||
final public static function tryFrom($value): ?self | ||
{ | ||
foreach (static::cases() as $case) { | ||
if ($case->value === $value) { | ||
$result = $case; | ||
break; | ||
} | ||
} | ||
|
||
return $result ?? null; | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
{ | ||
"name": "ducks-project/spl-types-subpackage-replace", | ||
"type": "metapackage", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Adrien Loyant", | ||
"email": "[email protected]", | ||
"homepage": "http://adrien.loyant.net", | ||
"role": "Owner" | ||
} | ||
], | ||
"homepage": "https://github.com/ducks-project/spl-types/", | ||
"support": { | ||
"email": "[email protected]" | ||
}, | ||
"replace": { | ||
"symfony/polyfill-php54": "*", | ||
"symfony/polyfill-php55": "*", | ||
"symfony/polyfill-php56": "*", | ||
"symfony/polyfill-php70": "*", | ||
"symfony/polyfill-php71": "*", | ||
"symfony/polyfill-php72": "*", | ||
"symfony/polyfill-php73": "*", | ||
"symfony/polyfill-php74": "*" | ||
} | ||
} |