From d5738583b85ff782f0a2d759888bbba19c80aa43 Mon Sep 17 00:00:00 2001 From: donaldinou Date: Wed, 25 Sep 2024 10:47:54 +0200 Subject: [PATCH] feat(chore) : update composer --- CHANGELOG.md | 2 + SplEnum.php | 2 + SplEnumTrait.php | 78 +++++++++++++++++++++ composer.json | 7 ++ composer.lock | 120 ++++++++++++--------------------- packages/replace/composer.json | 27 ++++++++ 6 files changed, 158 insertions(+), 78 deletions(-) create mode 100644 SplEnumTrait.php create mode 100644 packages/replace/composer.json diff --git a/CHANGELOG.md b/CHANGELOG.md index ff28314..8f84ec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/SplEnum.php b/SplEnum.php index 2a98ab8..c6e866a 100644 --- a/SplEnum.php +++ b/SplEnum.php @@ -20,6 +20,8 @@ */ abstract class SplEnum extends SplType { + use SplEnumTrait; + /** * {@inheritdoc} * diff --git a/SplEnumTrait.php b/SplEnumTrait.php new file mode 100644 index 0000000..bc21040 --- /dev/null +++ b/SplEnumTrait.php @@ -0,0 +1,78 @@ + + * + * 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; + } +} diff --git a/composer.json b/composer.json index 8578268..534edfe 100755 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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\\": "" diff --git a/composer.lock b/composer.lock index d9d4bb0..6478a70 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,46 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ee027b0cf93e3bda23863e18b6b2aad8", + "content-hash": "1b497eaf28f4b3d0356997e476f45958", "packages": [ + { + "name": "ducks-project/spl-types-subpackage-replace", + "version": "7.x-dev", + "dist": { + "type": "path", + "url": "packages/replace", + "reference": "cde5b6044e97b55195e6cc60cd73f7e58c864a38" + }, + "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": "*" + }, + "type": "metapackage", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Adrien Loyant", + "email": "donald_duck@team-df.org", + "homepage": "http://adrien.loyant.net", + "role": "Owner" + } + ], + "homepage": "https://github.com/ducks-project/spl-types/", + "support": { + "email": "adrien.loyant@gmail.com" + }, + "transport-options": { + "relative": true + } + }, { "name": "symfony/polyfill-php80", "version": "v1.31.0", @@ -5506,82 +5544,6 @@ ], "time": "2024-09-09T11:45:10+00:00" }, - { - "name": "symfony/polyfill-php73", - "version": "v1.31.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-php73.git", - "reference": "0f68c03565dcaaf25a890667542e8bd75fe7e5bb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/0f68c03565dcaaf25a890667542e8bd75fe7e5bb", - "reference": "0f68c03565dcaaf25a890667542e8bd75fe7e5bb", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "type": "library", - "extra": { - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Php73\\": "" - }, - "classmap": [ - "Resources/stubs" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-php73/tree/v1.31.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-09-09T11:45:10+00:00" - }, { "name": "symfony/polyfill-php81", "version": "v1.31.0", @@ -6221,7 +6183,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "ducks-project/spl-types-subpackage-replace": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/packages/replace/composer.json b/packages/replace/composer.json new file mode 100644 index 0000000..2f49ce8 --- /dev/null +++ b/packages/replace/composer.json @@ -0,0 +1,27 @@ +{ + "name": "ducks-project/spl-types-subpackage-replace", + "type": "metapackage", + "license": "MIT", + "authors": [ + { + "name": "Adrien Loyant", + "email": "donald_duck@team-df.org", + "homepage": "http://adrien.loyant.net", + "role": "Owner" + } + ], + "homepage": "https://github.com/ducks-project/spl-types/", + "support": { + "email": "adrien.loyant@gmail.com" + }, + "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": "*" + } +}