Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change AutoDiscoveredValuesTrait to use constant values as keys #47

Closed
wants to merge 1 commit into from
Closed

Change AutoDiscoveredValuesTrait to use constant values as keys #47

wants to merge 1 commit into from

Conversation

ostrolucky
Copy link
Contributor

Hi, can I have this change? It aids with #46

For some enums, it's ok to have label same as value. In those cases, this allows to not having to define neither values() and neither readables(). Shouldn't pose too big risk of BC break.

@ogizanagi
Copy link
Member

This would be solved by #49 with no potential BC issue, in respect of the initial EnumInterface::values contract.

However, I'm asking you: would you prefer default labels being the value itself, or a human representation of the constant name as done in default implementation of the FlaggedEnum?

@ostrolucky
Copy link
Contributor Author

Thank you for welcoming similar idea. Let's continue in #49

@ostrolucky ostrolucky closed this Apr 8, 2018
@ostrolucky ostrolucky deleted the non-int-keys-in-autoDiscoveredValuesTrait branch April 8, 2018 16:50
ogizanagi added a commit that referenced this pull request Apr 13, 2018
This PR was merged into the 1.x-dev branch.

Discussion
----------

Add ChoiceEnumTrait & SimpleChoiceEnum

Fixes #46

```php
final class DummyEnum extends ReadableEnum
{
    use ChoiceEnumTrait;

    const FOO = 'foo';
    const BAR = 'bar';
    const BAZ = 'baz';

    protected static function choices(): array
    {
        return [
            static::FOO => 'Foo label',
            static::BAR => 'Bar label',
            static::BAZ => 'Baz label',
        ];
    }
}
```

or with autodiscovery & enum values as default labels:

```php
final class DummyEnum extends SimpleChoiceEnum
{
    const FOO = 'foo';
    const BAR = 'bar';
    const BAZ = 'baz';
}
```

### TODO

- [x] Update docs
- [x] Change default implem for readables? (see #47 (comment))
- [ ] ~~Reverse `choices()` returned map? (labels as keys instead of values as in #46 descr). It requires extra work, deduplication and can lead to confusion, so I personally don't see the benefit.~~ No

Commits
-------

1d816bd Add ChoiceEnumTrait & SimpleChoiceEnum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants