Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for defining Groups and Subgroups as enums #932

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@shalvah shalvah Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I really hate these property accessor methods.

Considering making the next major PHP 8.4 only so I can do this with property hooks.

(I won't do that.)

{
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
Loading