-
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.
introduced PHP 8.0 Attribute enumerations
- Loading branch information
Showing
10 changed files
with
168 additions
and
28 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
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Thunder\Platenum\Enum; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
abstract class AbstractAttributeEnum implements \JsonSerializable | ||
{ | ||
use AttributeEnumTrait; | ||
} |
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,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Thunder\Platenum\Enum; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
trait AttributeEnumTrait | ||
{ | ||
use EnumTrait; | ||
|
||
/** @psalm-suppress UndefinedDocblockClass, UndefinedMethod because there is no ReflectionAttribute on PHP <8.0 */ | ||
private static function resolve(): array | ||
{ | ||
/** @var \ReflectionAttribute[] $attributes */ | ||
$attributes = (new \ReflectionClass(static::class))->getAttributes(Member::class); | ||
$members = []; | ||
foreach($attributes as $attribute) { | ||
/** @var Member $member */ | ||
$member = $attribute->newInstance(); | ||
$members[$member->member] = $member->value; | ||
} | ||
|
||
return $members; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Thunder\Platenum\Enum; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
* @psalm-immutable | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS|\Attribute::IS_REPEATABLE)] | ||
final class Member | ||
{ | ||
/** @var string */ | ||
public $member; | ||
/** @var int|string */ | ||
public $value; | ||
|
||
/** | ||
* @param string $member | ||
* @param int|string $value | ||
*/ | ||
public function __construct($member, $value) | ||
{ | ||
$this->member = $member; | ||
$this->value = $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
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,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Thunder\Platenum\Tests; | ||
|
||
use Thunder\Platenum\Enum\CallbackEnumTrait; | ||
use Thunder\Platenum\Exception\PlatenumException; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
final class AttributeEnumTest extends AbstractTestCase | ||
{ | ||
public function testMembers(): void | ||
{ | ||
PHP_VERSION_ID < 80000 && $this->markTestSkipped('Requires PHP 8.0'); | ||
|
||
$members = ['FIRST' => 1, 'SECOND' => 2]; | ||
$trait = $this->makeAttributeTraitEnum($members); | ||
$extends = $this->makeAttributeExtendsEnum($members); | ||
|
||
$expected = ['FIRST' => 1, 'SECOND' => 2]; | ||
$this->assertSame($expected, $trait::getMembersAndValues()); | ||
$this->assertSame($expected, $extends::getMembersAndValues()); | ||
} | ||
} |