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

[v6] Add BackedEnum support #2391

Merged
merged 3 commits into from
Apr 12, 2023
Merged

[v6] Add BackedEnum support #2391

merged 3 commits into from
Apr 12, 2023

Conversation

drbyte
Copy link
Collaborator

@drbyte drbyte commented Apr 11, 2023

Fixes #1991

Note: Requires PHP 8.1+

/cc @ceejayoz
/cc @edalzell

@drbyte
Copy link
Collaborator Author

drbyte commented Apr 11, 2023

Feedback welcome. I know there are lots of varying ways people like to implement enums. This covers the basics, but if there are some clear use-cases that aren't supported here, this is a good time to make adjustments.

@drbyte
Copy link
Collaborator Author

drbyte commented Apr 11, 2023

/cc @boumanb

@drbyte drbyte changed the title Add BackedEnum support [v6] Add BackedEnum support Apr 11, 2023
@drbyte drbyte force-pushed the backed-enum branch 2 times, most recently from 4d7b9ee to fcdd577 Compare April 11, 2023 07:57
@MarcusOhman91
Copy link

MarcusOhman91 commented Apr 11, 2023

Great job, this should really be the recommended way to implement permissions. I did it so far by overriding methods in the HasRole and HasPermission traits, not having to do this would be great.

Having all permissions defined in an Enum is so much cleaner and sustainable rather than using plain strings. Really neat with helpers as in your example to tie presentational data with each permission which I've been using as well to tie a description and category to each permission

@edalzell
Copy link
Contributor

Why is the interface needed?

@boumanb
Copy link

boumanb commented Apr 11, 2023

@drbyte to me this looks like a perfect PR example. It's specific and well-documented.

Why is the interface needed?

If I may answer; to assure it's a BackedEnum. To quote from the PHP documentation:

The BackedEnum interface is automatically applied to backed enumerations by the engine. It may not be implemented by user-defined classes. Enumerations may not override its methods, as default implementations are provided by the engine. It is available only for type checks.

The approach of @drbyte follows the example of the comment in the BackedEnum PHP documentation. Although the BackedEnum interface may not be implemented it CAN BE extended.

Nevertheless, I think the solution works without extending the BackedEnum and creating a BackedEnumInterface too. Because the BackedEnum interface is automatically applied to backed enumeration by the engine.

@edalzell
Copy link
Contributor

Why is the interface needed?

If I may answer; to assure it's a BackedEnum. To quote from the PHP documentation:

The BackedEnum interface is automatically applied to backed enumerations by the engine. It may not be implemented by user-defined classes. Enumerations may not override its methods, as default implementations are provided by the engine. It is available only for type checks.

The approach of @drbyte follows the example of the comment in the BackedEnum PHP documentation. Although the BackedEnum interface may not be implemented it CAN BE extended.

Sure but you can do this to get a BackedEnum:

CleanShot 2023-04-11 at 11 11 51@2x

...and that interface is never checked anywhere, only BackedEnum. So it seems like it's not used anywhere?

Sorry for my confusion if I'm way off track here, trying to learn!

@edalzell
Copy link
Contributor

To be super clear, I'm talking about this:

CleanShot 2023-04-11 at 11 13 41@2x

@boumanb
Copy link

boumanb commented Apr 11, 2023

Sorry for my confusion if I'm way off track here, trying to learn!

I've edited my comment. I think you're right and the BackedEnumInterface is not necessary.

@drbyte
Copy link
Collaborator Author

drbyte commented Apr 11, 2023

the BackedEnumInterface is not necessary

Yes, you're correct.

I had tried extending BackedEnum, but that triggered errors. I didn't test Enum directly without extending or implementing an interface.

I'll update the code in a bit.

Fixes #1991

Note: Requires PHP 8.1+

/cc @ceejayoz
/cc @edalzell
@drbyte
Copy link
Collaborator Author

drbyte commented Apr 11, 2023

I'll update the code in a bit.

Updated.
Also revised the docs a bit as well.

Question: For those of you who have been using Enums already, how have you handled can() and @can() situations? And route middleware parameters?

@edalzell
Copy link
Contributor

This is very awesome @drbyte and @erikn69, thanks for doing this!

@drbyte drbyte merged commit e4c291d into main Apr 12, 2023
@drbyte drbyte deleted the backed-enum branch April 12, 2023 21:09
drbyte added a commit that referenced this pull request Jul 16, 2023
@erikn69
Copy link
Contributor

erikn69 commented Dec 5, 2023

I think it was missing to add support for enum in the models
create, findByName, findOrCreate
Example: erikn69@fa01c59

@drbyte
Copy link
Collaborator Author

drbyte commented Dec 14, 2023

add support for enum in the models: create, findByName, findOrCreate

@erikn69 I'd support a PR for that addition.

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.

5 participants