Skip to content

Commit

Permalink
feat(chore) : update composer
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldinou committed Sep 25, 2024
1 parent 9aeb393 commit d573858
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 78 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `name` case property for SplEnum like [Basic enumerations]
- `value` property available for SplEnum like [Backed enumerations]
- Dynamic static creation of SplEnum object with magic `__callStatic`.
- Add `from` method from [Backed enumerations]
- Add `tryFrom` method from [Backed enumerations]

### Changed

Expand Down
2 changes: 2 additions & 0 deletions SplEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
abstract class SplEnum extends SplType
{
use SplEnumTrait;

/**
* {@inheritdoc}
*
Expand Down
78 changes: 78 additions & 0 deletions SplEnumTrait.php
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);

Check failure on line 31 in SplEnumTrait.php

View workflow job for this annotation

GitHub Actions / build

UndefinedClass

SplEnumTrait.php:31:25: UndefinedClass: Class, interface or enum named ReflectionEnum does not exist (see https://psalm.dev/019)
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;
}
}
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"require": {
"php": ">=7.4",
"ext-json": "*",
"ducks-project/spl-types-subpackage-replace": "self.version",
"symfony/polyfill-php80": "^1.31"
},
"require-dev": {
Expand All @@ -46,6 +47,12 @@
"squizlabs/php_codesniffer": "^3.10",
"vimeo/psalm": "^5.26"
},
"repositories": [
{
"type": "path",
"url": "packages/replace"
}
],
"autoload": {
"psr-4": {
"Ducks\\Component\\SplTypes\\": ""
Expand Down
120 changes: 42 additions & 78 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions packages/replace/composer.json
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": "*"
}
}

0 comments on commit d573858

Please sign in to comment.