Closed prolic closed 11 years ago
Hi,
Thanks for you proposal.
About own constants and second argument for get() I don't like to add such constants as I feel it's not a good idea.
To create an enum by it's ordinal number an own getter (like getByOrdinal) would be more accurate and creating an enum by name is vary simple because of the constant names itself and the callable magic. So ignoring constants prefixed with "__" isn't needed.
About getConstants() getNames() and getValues() doesn't get my vote as it sounds it returns a simple list but what we need is a map. I also don't like toArray() as it is names to convert something into an array but the enum itself represents only one item and the class itself isn't convertable. What we need is a name to indicate the returned value is an information as name-value pairs.
This method is final as it should return only what it does - no more no less - and it is used internally.
About __toString() For me it's ok to remove the final behavior but than I would change it to return the name by default to make sure nobody use it for logical reasons. (like Java)
Am 15.09.2013 16:38, schrieb prolic:
1) Ignore all Constants beginning with "__" This is first at all used by the enum class itself (see point 2) and for extensions of an enum class, where an additional constant shouldn't belong to the enum behaviour.
2) at three constants: a) FROM_VALUE b) __FROM_NAME c) FROM_ORDINAL
3) change the get method: static public function get($value, self::__FROM_VALUE)
This way you can create enums by ordinal or by constant name.
4) About getConstants() the method name indicates an implementation detail of the enum class. This is not usefull in terms of an enum. Better we have: getNames() and getValues()
Additional: We could perhabs readd a toArray() method, where you get the name => value map again, which is currently what getConstants() does.
This again leave to two conclusions: a) toString() shoudldn't be final. An "Enum-like-Html-Object" perhabs could need to override the toString() method and rely an getValue() where needed instead. Having toString() returning the value is a good default behaviour. b) toArray() shoudln't be final, too. The output should be changeable. Rely on the other methods instead, if you need ensured behaviour. c) what should toArray return? Only what currently getConstants() does? Or perhabs also what the default value is and what the current value is?
What do you think?
— Reply to this email directly or view it on GitHub https://github.com/marc-mabe/php-enum/issues/18.
So let's agree on: a) add getByName($name) b) add getByOrdinal($ordinal)
1) Ignore all Constants beginning with "__" This is first at all used by the enum class itself (see point 2) and for extensions of an enum class, where an additional constant shouldn't belong to the enum behaviour.
2) add three constants: a) FROM_VALUE b) __FROM_NAME c) FROM_ORDINAL
3) change the get method: static public function get($value, self::__FROM_VALUE)
This way you can create enums by ordinal or by constant name.
4) About getConstants() the method name indicates an implementation detail of the enum class. This is not usefull in terms of an enum. Better we have: getNames() and getValues()
Additional: We could perhabs readd a toArray() method, where you get the name => value map again, which is currently what getConstants() does.
This again leave to two conclusions: a) toString() shoudldn't be final. An "Enum-like-Html-Object" perhabs could need to override the toString() method and rely an getValue() where needed instead. Having toString() returning the value is a good default behaviour. b) toArray() shoudln't be final, too. The output should be changeable. Rely on the other methods instead, if you need ensured behaviour. c) what should toArray return? Only what currently getConstants() does? Or perhabs also what the default value is and what the current value is?
What do you think?