diff --git a/src/Attributes/Group.php b/src/Attributes/Group.php index 3421efd3..e0484831 100644 --- a/src/Attributes/Group.php +++ b/src/Attributes/Group.php @@ -8,17 +8,32 @@ class Group { public function __construct( - public string $name, + public mixed $name, public ?string $description = '', /** You can use the separate #[Authenticated] attribute, or pass authenticated: false to this. */ public ?bool $authenticated = null, ){ } + protected function getName(): string + { + if (is_string($this->name)) { + return $this->name; + } + + if (interface_exists('BackedEnum') && is_a($this->name, 'BackedEnum')) { + return $this->name->value; + } + + throw new \InvalidArgumentException( + 'The name property of a group must be either a PHP Backed Enum or a string' + ); + } + public function toArray() { $data = [ - "groupName" => $this->name, + "groupName" => $this->getName(), "groupDescription" => $this->description, ]; diff --git a/src/Attributes/Subgroup.php b/src/Attributes/Subgroup.php index a7edf1f2..86497220 100644 --- a/src/Attributes/Subgroup.php +++ b/src/Attributes/Subgroup.php @@ -8,15 +8,30 @@ class Subgroup { public function __construct( - public string $name, + public mixed $name, public ?string $description = '', ){ } + protected function getName(): string + { + if (is_string($this->name)) { + return $this->name; + } + + if (interface_exists('BackedEnum') && is_a($this->name, 'BackedEnum')) { + return $this->name->value; + } + + throw new \InvalidArgumentException( + 'The name property of a subgroup must be either a PHP Backed Enum or a string' + ); + } + public function toArray() { return [ - "subgroup" => $this->name, + "subgroup" => $this->getName(), "subgroupDescription" => $this->description, ]; } diff --git a/tests/Fixtures/TestGroupBackedEnum.php b/tests/Fixtures/TestGroupBackedEnum.php new file mode 100644 index 00000000..e5b64543 --- /dev/null +++ b/tests/Fixtures/TestGroupBackedEnum.php @@ -0,0 +1,9 @@ + false, ], $results); + $endpoint = $this->endpoint(function (ExtractedEndpointData $e) { + $e->controller = new ReflectionClass(MetadataAttributesTestController::class); + $e->method = $e->controller->getMethod('b2'); + }); + $results = $this->fetch($endpoint); + $this->assertArraySubset([ + "groupName" => "Users", + "groupDescription" => "", + "subgroup" => "Admins", + "subgroupDescription" => "", + "title" => "Endpoint B2", + "description" => "", + "authenticated" => false, + ], $results); + $endpoint = $this->endpoint(function (ExtractedEndpointData $e) { $e->controller = new ReflectionClass(MetadataAttributesTestController2::class); $e->method = $e->controller->getMethod('c1'); @@ -159,6 +175,13 @@ public function a3() public function b1() { } + + #[Group(TestGroupBackedEnum::Users)] + #[Subgroup(TestGroupBackedEnum::Admins)] + #[Endpoint("Endpoint B2")] + public function b2() + { + } } #[Authenticated]