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

Conversation

lioneaglesolutions
Copy link

Problem

When defining groups and subgroups, it's nice to have a clear idea of what groups are available to use. PHP Backed Enums are a perfect way of achieving this. Otherwise, developers could mis-type group names easily such as;

#[Group('Users')];
#[Group('User')];  // typo

Solution

By allowing the usage of Backed Enums, it is very clean to be able to define your API Endpoint Groups as an enum;

enum EndpointGroup: string
{
    case Users = 'Users';
    case Invoices = 'Invoices';
}

Then in your codebase you can simply do;

#[Group(EndpointGroup::Invoices)]
class InvoiceController
{
   // ...
}

Alternative

An alternative could be to just add ->value to each enum which would not require any codebase changes however this is not visually pleasing and not as nice of a DX as being able to pass an enum directly.

#[Group(EndpointGroup::Invoices->value)]
class InvoiceController
{
   // ...
}

@shalvah
Copy link
Contributor

shalvah commented Jan 18, 2025

Nice convenience feature!

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.)

@shalvah shalvah changed the base branch from master to vNext January 18, 2025 20:18
@shalvah shalvah merged commit 9f01a1a into knuckleswtf:vNext Jan 18, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants