mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 20:04:03 +00:00
from(int|string), tryFrom(int|string)
This commit is contained in:
parent
bdd8de68ff
commit
859fb9a03c
13 changed files with 26 additions and 2 deletions
|
|
@ -15,7 +15,7 @@ trait From
|
||||||
*
|
*
|
||||||
* @throws ValueError
|
* @throws ValueError
|
||||||
*/
|
*/
|
||||||
public static function from(string $case): static
|
public static function from(int|string $case): static
|
||||||
{
|
{
|
||||||
return static::fromName($case);
|
return static::fromName($case);
|
||||||
}
|
}
|
||||||
|
|
@ -25,7 +25,7 @@ trait From
|
||||||
*
|
*
|
||||||
* This will not override the `tryFrom()` method on BackedEnums
|
* This will not override the `tryFrom()` method on BackedEnums
|
||||||
*/
|
*/
|
||||||
public static function tryFrom(string $case): ?static
|
public static function tryFrom(int|string $case): ?static
|
||||||
{
|
{
|
||||||
return static::tryFromName($case);
|
return static::tryFromName($case);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
|
namespace ArchTech\Enums\Tests\PHPStan\InvokableCases;
|
||||||
|
|
||||||
use PHPStan\Analyser\OutOfClassScope;
|
use PHPStan\Analyser\OutOfClassScope;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
|
use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
|
||||||
use PHPStan\Type\IntegerType;
|
use PHPStan\Type\IntegerType;
|
||||||
use PHPStan\Type\StringType;
|
use PHPStan\Type\StringType;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
use ArchTech\Enums\{Comparable, InvokableCases, Options, Names, Values, From, Metadata};
|
use ArchTech\Enums\{Comparable, InvokableCases, Options, Names, Values, From, Metadata};
|
||||||
use ArchTech\Enums\Meta\Meta;
|
use ArchTech\Enums\Meta\Meta;
|
||||||
use ArchTech\Enums\Meta\MetaProperty;
|
use ArchTech\Enums\Meta\MetaProperty;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
test('the is method checks for equality', function () {
|
test('the is method checks for equality', function () {
|
||||||
expect(Status::PENDING->is(Status::PENDING))->toBeTrue();
|
expect(Status::PENDING->is(Status::PENDING))->toBeTrue();
|
||||||
expect(Status::PENDING->is(Status::DONE))->toBeFalse();
|
expect(Status::PENDING->is(Status::DONE))->toBeFalse();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
it('does not override the default BackedEnum from method')
|
it('does not override the default BackedEnum from method')
|
||||||
->expect(Status::from(0))
|
->expect(Status::from(0))
|
||||||
->toBe(Status::PENDING);
|
->toBe(Status::PENDING);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
use ArchTech\Enums\Exceptions\UndefinedCaseError;
|
use ArchTech\Enums\Exceptions\UndefinedCaseError;
|
||||||
|
|
||||||
it('can be used as a static method with backed enums', function () {
|
it('can be used as a static method with backed enums', function () {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
test('pure enums can have metadata on cases', function () {
|
test('pure enums can have metadata on cases', function () {
|
||||||
expect(Role::ADMIN->color())->toBe('indigo');
|
expect(Role::ADMIN->color())->toBe('indigo');
|
||||||
expect(Role::GUEST->color())->toBe('gray');
|
expect(Role::GUEST->color())->toBe('gray');
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
it('can return an array of case names from backed enums')
|
it('can return an array of case names from backed enums')
|
||||||
->expect(Status::names())
|
->expect(Status::names())
|
||||||
->toBe(['PENDING', 'DONE']);
|
->toBe(['PENDING', 'DONE']);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
it('can return an associative array of options from a backed enum')
|
it('can return an associative array of options from a backed enum')
|
||||||
->expect(Status::options())->toBe([
|
->expect(Status::options())->toBe([
|
||||||
'PENDING' => 0,
|
'PENDING' => 0,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
use ArchTech\Enums\Meta\Meta;
|
use ArchTech\Enums\Meta\Meta;
|
||||||
use ArchTech\Enums\Meta\MetaProperty;
|
use ArchTech\Enums\Meta\MetaProperty;
|
||||||
use ArchTech\Enums\Metadata;
|
use ArchTech\Enums\Metadata;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
it('can return an array of case values from a backed enum')
|
it('can return an array of case values from a backed enum')
|
||||||
->expect(Status::values())
|
->expect(Status::values())
|
||||||
->toBe([0, 1]);
|
->toBe([0, 1]);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ArchTech\Enums\Tests;
|
namespace ArchTech\Enums\Tests;
|
||||||
|
|
||||||
use Orchestra\Testbench\TestCase as TestbenchTestCase;
|
use Orchestra\Testbench\TestCase as TestbenchTestCase;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue