- Issue created by @phenaproxima
- π¦πΊAustralia sime Melbourne
Would you like a separate enum for the IDs and one for the LABELs?
- πΊπΈUnited States phenaproxima Massachusetts
No need -- enums can do __toString()!
- π¦πΊAustralia sime Melbourne
I believe that means this...
enum Status: string { case Maintained = 'Maintained'; case Covered = 'Covered by a security policy'; case Active = 'Active'; case All = 'Show all'; }
.. or ...
enum Status: string { case maintained = 'Maintained'; case covered = 'Covered by a security policy'; case active = 'Active'; case all = 'Show all'; }
- πΊπΈUnited States phenaproxima Massachusetts
It doesn't.
The enum could be string-backed to have a string value, and implement
__toString()
to produce a full human-readable label. - π¦πΊAustralia sime Melbourne
Nah it's explicitly not supported. But looks like you can add public methods and have this:
echo Status::Covered->name; // Covered echo Status::Covered->value; // covered echo Status::Covered->label(); // Covered by a security policy
I did a sandbox for this which includes a __toString() commented out. If you uncomment it you get.
Fatal error: Enum Status cannot include magic method __toString in /home/user/scripts/code.php on line 4
- πΊπΈUnited States phenaproxima Massachusetts
Hmmmβ¦maybe we just add a regular toString(), then, like the Url object has.
- Status changed to Needs review
8 months ago 2:42pm 1 June 2024 - π¦πΊAustralia sime Melbourne
Ok what do you think of this? toString() felt very ambiguous, and it really would be nice to have magic __toString() support here. Either way I think it's at 95% and I'm not offended if you change the implementation.
- πΊπΈUnited States phenaproxima Massachusetts
I propose a slightly different approach: https://git.drupalcode.org/project/project_browser/-/merge_requests/493
Backed enums don't need a specialized id() method, they already have ->value to access the backing value. They really just need two things, as far as I can tell: the ability to be represented as a keyed array of translatable labels, and, well, a translatable label per-case. That's what I've implemented. Thoughts?
- π¦πΊAustralia sime Melbourne
I rebased and added one commit to your new branch. Just a few values not replaced.
Backed enums don't need a specialized id() method, they already have ->value to access the backing value.
One way or another you would have to overload the enum. I don't think id() was any better or worse than toString() or label(). __toString() would have hidden this away for sure.
- πΊπΈUnited States chrisfromredfin Portland, Maine
chrisfromredfin β changed the visibility of the branch 3447420-enum-options to hidden.
- πΊπΈUnited States phenaproxima Massachusetts
phenaproxima β changed the visibility of the branch 3447420-enum-options to active.
- πΊπΈUnited States phenaproxima Massachusetts
phenaproxima β changed the visibility of the branch 3447420-enum-options to hidden.
-
chrisfromredfin β
committed 01f96fe3 on 1.0.x authored by
phenaproxima β
Issue #3447420 by sime, phenaproxima, chrisfromredfin: Change...
-
chrisfromredfin β
committed 01f96fe3 on 1.0.x authored by
phenaproxima β
- Status changed to Fixed
7 months ago 1:43pm 6 June 2024 Automatically closed - issue fixed for 2 weeks with no activity.