-
Notifications
You must be signed in to change notification settings - Fork 21
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
Trait for string conversion #12
Comments
Wouldn't |
If I understand correctly, you would use ->name on a single enum value and ->names on the enum itself to get an array of names? I don't want either of those. I want a string that illustrates the entire associative array of a backed enum. |
So essentially something like this? array_combine(FooEnum::names(), FooEnum::values()); |
Yeah, that's the idea but it would be return a string instead of an array to be used like this in a create_table migration, for example: Personally, I have my own trait that implements this function like this right (could easily be improved upon):
I don't know how popular or useful this would be to other people, just an idea. Thanks for considering! |
This actually isn't a bad idea, think a more common use case for this might be |
So a summary:
With perhaps some optimizations for common use cases. I'll think about this, as well as other approaches for addressing the Blade templating specifically. |
See the linked commit. Ended up with this implementation: it('can return a string of options from a backed enum')
->expect(Status::stringOptions(fn ($name, $value) => "$name => $value", ', '))
->toBe("PENDING => 0, DONE => 1");
it('can return a string of options from a pure enum')
->expect(Role::stringOptions(fn ($name, $value) => "$name => $value", ', '))
->toBe("ADMIN => ADMIN, GUEST => GUEST");
it('returns default HTML options from backed enums')
->expect(Status::stringOptions())
->toBe('<option value="0">Pending</option>\n<option value="1">Done</option>');
it('returns default HTML options from pure enums')
->expect(Role::stringOptions())
->toBe('<option value="ADMIN">Admin</option>\n<option value="GUEST">Guest</option>'); Think that works really well. Thanks for the suggestion! |
Sweet!! Thanks for the ongoing support! |
Will be tagging v1 tomorrow. Make sure to update your composer dependency to |
For enum fields, I like to keep a comment in the database that has all the enum values and text. It might be useful to have a Trait with a toString() to get this? I personally also include a $nameFirst parameter to specify if I want:
1 => Admin, 2 => Instructor, 3 => Student
or
Admin => 1, Instructor => 2, Student =>3
Thanks for the great package!
The text was updated successfully, but these errors were encountered: