Skip to content

Conversation

@jodiedunlop
Copy link
Contributor

Refactored API as discussed in issue #3

  • Add Enum::instanceFromKey($key)
  • Breaking Change $instance->identifier() to $instance->name()
  • Breaking Change Enum::identifiers() to Enum::names()
  • Breaking Change Enum::getKeyForIdentfier() to Enum::keyForName()
  • Breaking Change Enum::valueFor() to Enum::valueForKey()
  • Add Enum::nameForKey() to get the constant name for a given key
  • Breaking Change Enum::exists() to Enum::isValidKey()
  • Breaking Change Enum::checkExists() to Enum::requireValidKey()
  • Fix $instance->key() to handle non-string keys
  • Fix $instance->is() to handle non-string keys
  • Fix late-static binding in some methods which referred to self::
  • Add Enum::instanceFromName($name) to get an instance via name (alternative to Enum::NAME())
  • Change implementation of Enum::instanceFromKey($key) to use array_search
  • Breaking Change: the default provided static map() method will return an array of constant keys mapped to null.
    Previously it returned an empty array [] when not overridden. In practice, this may not effect userland code.
  • Breaking Change: you can no longer provide a non-keyed array in an map() method implemented
    in your sub-class. This method should be used to map keys to values (if necessary). A default map() method is provided
    which maps keys to null values.
  • Breaking Change Enum::fromValue($val) has been renamed to Enum::keyForValue()
  • Breaking Change: removed Enum::flip()

Copy link
Contributor

@lachlankrautz lachlankrautz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with all the new names, much "clearer" which is important because it can be used in many contexts


use Rexlabs\Enum\Enum;

class Animal extends Enum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing from animals to cities is anthropocentric

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

* @throws InvalidKeyException
*/
public static function checkExists($key)
public static function requireValidKey($key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer

- **Breaking** Change `$instance->identifier` to `$instance->name()`
- **Breaking** Change `Enum::identifiers()` to `Enum::names()`
- **Breaking** Change `Enum::getKeyForIdentfier()` to `Enum::keyForName()`
- **Breaking** Change `Enum::valueFor()` to `Enum::valueForKey()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum::valueForName() for completeness?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants