archtechx / enums Goto Github PK
View Code? Open in Web Editor NEWHelpers for making PHP enums more lovable.
License: MIT License
Helpers for making PHP enums more lovable.
License: MIT License
would it be good to implement the interface to identify if the enum has the trait Values, Names..?
Example
use ArchTech\Enums\Names;
use ArchTech\Enums\Traits\Nameable;
enum TaskStatus: int implements Nameable
{
use Names;
case INCOMPLETE = 0;
case COMPLETED = 1;
case CANCELED = 2;
}
```
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!
That way, users can create a trait such as:
/**
* @method string description()
*/
#[Meta(Description::class)
trait HasDescription
{}
And use this across their enums as:
enum Status
{
+ use HasDescription;
+ #[Description('Completed Task')]
case COMPLETED;
}
enum Role
{
+ use HasDescription;
+ #[Description('Administrator')]
case ADMIN;
+ #[Description('Read-only user')]
case GUEST;
}
Edit ArchTech\Enums\Meta\Reflection::metaProperties()
to also recursively walk through the enum's traits, and extract the enabled meta properties in their #[Meta]
attributes, if they're defined.
It would be great if a separate package is made for laravel.
e.g. Command for enum annotate method tag
/**
* @method static SUPER_ADMIN()
* @method static ADMIN()
*/
enum RoleName: string
{
use InvokableCases;
case SUPER_ADMIN = 'SUPER_ADMIN';
case ADMIN = 'ADMIN';
}
Very interesting library!
One question:
Have you considered using what appears to be the convention for enums: CamelCase?
We had similar discussions for CakePHP for example:
cakephp/bake#972
And in the end seemed to go with those, as the cases are different from the constants in the end.
This way it also seems easier to distinguish the two.
It also seems to be the way they were intended to be in terms of convention by the core PHP (RFC).
Also saw a bit of both in your example so far: fc428d5#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R210
What do you think?
For form options we usually use
<option value="MultiWord">Multi Word</option>
as CamelCase for key vs Humanized Form for text.
Side note:
I also added a specific trait to mine for sorting and custom filtering (for some forms only a subset sometimes is wanted):
https://github.com/dereuromark/cakephp-tools/blob/master/src/Model/Entity/EnumTrait.php
Could be yours with a custom fn can also already do that out of the box, just doesnt mention it yet.
This method would essentially map over each variant/case and return an associative array of ->name => ->value
. Super useful for creating dropdowns for enums, think status enums, role enums etc.
To instantiate a backed enum using the case name.
Perhaps passing a callback would also work, if transformations or comparisons are needed (e.g. I have a use case that does strtoupper()
to convert the readable version to the case name).
To get a list of all scalar values for an enum, you'd currently have to loop over all ::cases()
and get the value.
Having a helper for this would be sweet.
let's assume we have an enum for seat status:
enum SeatStatusEnum: string
{
case AVAILABLE = 'available';
case RESERVED = 'reserved';
}
if I want to get AVAILABLE case value, I have to call the value property like this:
SeatStatusEnum::AVAILABLE->value
if there is a treat that automatically retrieves case value when calling the case without calling the value property, it would be great.
Filterable options, values or names
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.