Skip to content

Commit

Permalink
Add support for defining Groups and Subgroups as enums (#932)
Browse files Browse the repository at this point in the history
* add support for enum groups

* wording

---------

Co-authored-by: Shalvah <[email protected]>
  • Loading branch information
lioneaglesolutions and shalvah authored Jan 18, 2025
1 parent d5e61eb commit 9f01a1a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/Attributes/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand Down
19 changes: 17 additions & 2 deletions src/Attributes/Subgroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/TestGroupBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Knuckles\Scribe\Tests\Fixtures;

enum TestGroupBackedEnum: string
{
case Users = 'Users';
case Admins = 'Admins';
}
23 changes: 23 additions & 0 deletions tests/Strategies/Metadata/GetFromMetadataAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Knuckles\Scribe\Attributes\Subgroup;
use Knuckles\Scribe\Attributes\Unauthenticated;
use Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromMetadataAttributes;
use Knuckles\Scribe\Tests\Fixtures\TestGroupBackedEnum;
use Knuckles\Scribe\Tools\DocumentationConfig;
use PHPUnit\Framework\TestCase;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
Expand Down Expand Up @@ -84,6 +85,21 @@ public function can_fetch_from_authenticated_attribute_or_authenticated_paramete
"authenticated" => 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');
Expand Down Expand Up @@ -159,6 +175,13 @@ public function a3()
public function b1()
{
}

#[Group(TestGroupBackedEnum::Users)]
#[Subgroup(TestGroupBackedEnum::Admins)]
#[Endpoint("Endpoint B2")]
public function b2()
{
}
}

#[Authenticated]
Expand Down

0 comments on commit 9f01a1a

Please sign in to comment.