Comments (9)
Its is project governance that should decide:
-
what to do about "any type" of value defined inside
static::$map
or only "stringables".
Current state is "any type". -
compare values as equality
==
or identity===
Current state is mixed,__constructor()
is usingin_array
without strict comparison,equals
uses strict comparison.
I'm just pointing out the issues I found, reporting findings is a way to contribute.
from enum.
Thanks @eclipxe13 for the thorough report!
First of all I would like to ask @brendt if the bug is a real bug or if it's intended to allow only string values.
I'd say it's an oversight. However, I believe the way we'll handle values and indices in v2 will make the issue obsolete. As for fixing it in v1. I think a note in the README is sufficient. I added it here: https://github.com/spatie/enum#override-enum-values
we are for strict types and weak comparison would be against this.
This is true and we'll keep doing it. Strong types all the way.
Regarding the question about making $map
a function: if we want to get equality right, we cannot allow returning objects as an enum's value or index. Remember that an enum's value is used to store it in a database. You cannot store its value as an object. The right solution here is to add an abstract method on your enum class, and return anonymous classes implementing that method on a per value basis, as shown here: https://github.com/spatie/enum#enum-specific-methods
I believe that, with the addition to the README, all problems are addressed here, so I'll be closing this issue. Feel free however to keep the conversation going if you've got any more questions.
from enum.
Let's keep v2 feature requests in the PR discussion: #18
from enum.
If the conversation leads to a BC and the intention is to allow any type of value.
Could you consider to move static::$map to a protected method? Currently it can only contain scalar values and not objects. I'm thinking about allowing Value Objects in map.
from enum.
Ok, I read several parts here which belong to different types of issues.
- bug
$map
can contain string and int values but constructors only support strings - enhancement allow value objects as value in map
First of all I would like to ask @brendt if the bug is a real bug or if it's intended to allow only string values.
Related to the enhancement: do they have a method in common - like __toString()
?
And can you describe an usecase? We work already on a v2 release with a looot of changes. One of the biggest is the instantiation bug fix (string & int) and the drop of $map
which is replaced by getValue()
and getIndex()
methods (current state of development).
Both have a defined return type which we will not drop. If you want to get an instance of any object I would recommend to add a custom getValueObject()
method. This could be done once in the Enum or for every value in it's static method like the getNumericIndex()
method described in the readme.
My perspective: if the bug is a real bug I would fix it in v1. Objects implementing a __toString()
method are possible via the new getValue()
method in v2. Every other object is 100% against what an enumerable is and I won't want to support this out of the box.
Like I described in #10 my plan for v2 is to make this library more compatible with the most common native Enum implementations in other languages and the general word and programming definition. So for me it would be a step back to support not stringable objects. Even the separated value is candy on the top.
And last back to the bug and your solution ideas: we are for strict types and weak comparison would be against this. So if we fix this it will be in the way it's done for v2 to add specific checks if something is an index or value.
from enum.
As a base for this and following discussions about what this/an Enum should support or not I would like to use the enumerable in programming Wikipedia article also referenced by @brendt in #10 .
https://en.m.wikipedia.org/wiki/Enumerated_type
For the upcoming v2 and the related docs at docs.spatie.be I will try to copy this definition into a page to fix it in the package documentation.
from enum.
It is a current bug, as of the class in README:
$draft = new StatusEnum('1');
$draft->equals(StatusEnum::draft()); // return false, expected true
from enum.
👍 great.
If static::$map
is dropped then would be nice to release version 2 (at least preview) and some information about what is missing to release it.
While this happens:
- could you add third parameter to
in_array
? - could you cast to
string
values obtained fromstatic::map
Thanks again for your work and attention.
from enum.
@eclipxe13 the v2
branch is mostly finished. It requires review(s) and some real-world testing. It also doesn't have a change/upgrade-log or documentation atm and will not have until release. But the unittests are reworked in total and should show what's possible and what's not. We've also added a new interface which describes all public methods including param types and everything else.
Because it's easy and also done for v2 I will add the in_array(string, array, true)
to v1
while casting the values could happen - I will have to check first how easy this would be.
And feel free to use the dev-ft-v2 version to try it out. But beware that this is a new major release with BC and it could be that we introduce new BC during further development.
If you want to I can release a real v2.0.0-dev.1
to allow you to fix the version more precise.
from enum.
Related Issues (20)
- Enum serialized as empty array when using spatie/laravel-event-sourcing and spatie/data-transfer-object HOT 3
- Doc link is broken HOT 2
- Strong typed enums problems HOT 2
- Unit test overridden values HOT 3
- Enums with Laravel Octane HOT 2
- initialize Enum with values coming from DB? HOT 2
- PHPUnit assertions trait changed to class HOT 6
- Too few arguments to function Spatie\Enum\Enum::__construct(), 0 passed HOT 3
- OPcache strip comments problem HOT 2
- Missing documentation for ->isXy() HOT 1
- Deprecate values HOT 1
- Localised labels aren't update after locale is changed HOT 2
- can php7.2 (or even 7.3) be supported? HOT 2
- Extend already extended HOT 2
- Creating enum using method call from within enum throws UnknownEnumMethod HOT 6
- Consider using generics for better IDE support HOT 8
- Future of this package after PHP 8.1 HOT 7
- Support standard strict comparison HOT 5
- PHP8.1 Compatibility: check properties HOT 1
- Fix "Creating an enum from a value" example in documentation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enum.