{"id":24016279,"url":"https://github.com/decodelabs/enumerable","last_synced_at":"2025-04-15T14:06:59.580Z","repository":{"id":244472732,"uuid":"815295744","full_name":"decodelabs/enumerable","owner":"decodelabs","description":"Helper traits for PHP enums","archived":false,"fork":false,"pushed_at":"2025-04-14T08:46:53.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-04-15T14:06:43.547Z","etag":null,"topics":["enum","language","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/decodelabs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-14T19:43:19.000Z","updated_at":"2025-04-14T08:46:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"d8691fe8-56cb-44b4-a5cd-abcf72999c86","html_url":"https://github.com/decodelabs/enumerable","commit_stats":null,"previous_names":["decodelabs/enumerable"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decodelabs%2Fenumerable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decodelabs%2Fenumerable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decodelabs%2Fenumerable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decodelabs%2Fenumerable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/decodelabs","download_url":"https://codeload.github.com/decodelabs/enumerable/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249085436,"owners_count":21210267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","language","php"],"created_at":"2025-01-08T08:48:46.061Z","updated_at":"2025-04-15T14:06:59.566Z","avatar_url":"https://github.com/decodelabs.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Enumerable\n\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/enumerable?style=flat)](https://packagist.org/packages/decodelabs/enumerable)\n[![Latest Version](https://img.shields.io/packagist/v/decodelabs/enumerable.svg?style=flat)](https://packagist.org/packages/decodelabs/enumerable)\n[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/enumerable.svg?style=flat)](https://packagist.org/packages/decodelabs/enumerable)\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/enumerable/integrate.yml?branch=develop)](https://github.com/decodelabs/enumerable/actions/workflows/integrate.yml)\n[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true\u0026style=flat)](https://github.com/phpstan/phpstan)\n[![License](https://img.shields.io/packagist/l/decodelabs/enumerable?style=flat)](https://packagist.org/packages/decodelabs/enumerable)\n\n### Helper traits for enums\n\nEnumerable provides a simple structure of interfaces and traits to unlock the full power of PHP enums.\n\n---\n\n## Installation\n\nInstall via Composer:\n\n```bash\ncomposer require decodelabs/enumerable\n```\n\n## Usage\n\nEnumerable defines a powerful top level Enum interface that expands the range of functionality enums provide whilst consolidating the same functionality across both \u003ccode\u003eUnitEnum\u003c/code\u003e and \u003ccode\u003eBackedEnum\u003c/code\u003e types.\n\nAll Enumerable enums implement a type specific interface and use an accompanying trait. Each form dictates a type for a \u003ccode\u003ekey\u003c/code\u003e, \u003ccode\u003evalue\u003c/code\u003e and \u003ccode\u003elabel\u003c/code\u003e property, where the key is used as the index in lists and the label is used for display purposes.\n\n\n## Unit enums\n\n### Named UnitEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Unit\\Named;\nuse DecodeLabs\\Enumerable\\Unit\\NamedTrait;\n\nenum MyNamedUnitEnum implements Named\n{\n    use NamedTrait;\n\n    const OptionOne;\n    const OptionTwo;\n    const OptionThree;\n}\n\nMyNamedUnitEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyNamedUnitEnum::OptionOne-\u003egetKey();   // 'OptionOne'\nMyNamedUnitEnum::OptionOne-\u003egetLabel(); // 'Option One'\nMyNamedUnitEnum::OptionOne-\u003egetValue(); // 'OptionOne'\n```\n\n### Indexed UnitEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Unit\\Indexed;\nuse DecodeLabs\\Enumerable\\Unit\\IndexedTrait;\n\nenum MyIndexedUnitEnum implements Indexed\n{\n    use IndexedTrait;\n\n    const OptionOne;\n    const OptionTwo;\n    const OptionThree;\n}\n\nMyNamedUnitEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyNamedUnitEnum::OptionOne-\u003egetKey();   // 0\nMyNamedUnitEnum::OptionOne-\u003egetLabel(); // 'Option One'\nMyNamedUnitEnum::OptionOne-\u003egetValue(); // 'OptionOne'\n```\n\n## Backed enums\n\n### Named String BackedEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Backed\\NamedString;\nuse DecodeLabs\\Enumerable\\Backed\\NamedStringTrait;\n\nenum MyNamedStringBackedEnum : string implements NamedString\n{\n    use NamedStringTrait;\n\n    const OptionOne = 'one';\n    const OptionTwo = 'two';\n    const OptionThree = 'three';\n}\n\nMyNamedStringBackedEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyNamedStringBackedEnum::OptionOne-\u003egetKey();   // 'OptionOne'\nMyNamedStringBackedEnum::OptionOne-\u003egetLabel(); // 'Option One'\nMyNamedStringBackedEnum::OptionOne-\u003egetValue(); // 'one'\n```\n\n### Labelled String BackedEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Backed\\LabelledString;\nuse DecodeLabs\\Enumerable\\Backed\\LabelledStringTrait;\n\nenum MyLabelledStringBackedEnum : string implements LabelledString\n{\n    use LabelledStringTrait;\n\n    const OptionOne = 'one';\n    const OptionTwo = 'two';\n    const OptionThree = 'three';\n}\n\nMyLabelledStringBackedEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyLabelledStringBackedEnum::OptionOne-\u003egetKey();   // 'OptionOne'\nMyLabelledStringBackedEnum::OptionOne-\u003egetLabel(); // 'one'\nMyLabelledStringBackedEnum::OptionOne-\u003egetValue(); // 'one'\n```\n\n### Value String BackedEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Backed\\ValueString;\nuse DecodeLabs\\Enumerable\\Backed\\ValueStringTrait;\n\nenum MyValueStringBackedEnum : string implements ValueString\n{\n    use ValueStringTrait;\n\n    const OptionOne = 'one';\n    const OptionTwo = 'two';\n    const OptionThree = 'three';\n}\n\nMyValueStringBackedEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyValueStringBackedEnum::OptionOne-\u003egetKey();   // 'one'\nMyValueStringBackedEnum::OptionOne-\u003egetLabel(); // 'Option One'\nMyValueStringBackedEnum::OptionOne-\u003egetValue(); // 'one'\n```\n\n### Named Int BackedEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Backed\\NamedInt;\nuse DecodeLabs\\Enumerable\\Backed\\NamedIntTrait;\n\nenum MyNamedIntBackedEnum : int implements NamedInt\n{\n    use NamedIntTrait;\n\n    const OptionOne = 1;\n    const OptionTwo = 2;\n    const OptionThree = 3;\n}\n\nMyNamedIntBackedEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyNamedIntBackedEnum::OptionOne-\u003egetKey();   // 'OptionOne'\nMyNamedIntBackedEnum::OptionOne-\u003egetLabel(); // 'Option One'\nMyNamedIntBackedEnum::OptionOne-\u003egetValue(); // 1\n```\n\n### Value Int BackedEnum\n\n```php\nuse DecodeLabs\\Enumerable\\Backed\\ValueInt;\nuse DecodeLabs\\Enumerable\\Backed\\ValueIntTrait;\n\nenum MyValueIntBackedEnum : int implements ValueInt\n{\n    use ValueIntTrait;\n\n    const OptionOne = 1;\n    const OptionTwo = 2;\n    const OptionThree = 3;\n}\n\nMyValueIntBackedEnum::OptionOne-\u003egetName();  // 'OptionOne'\nMyValueIntBackedEnum::OptionOne-\u003egetKey();   // 1\nMyValueIntBackedEnum::OptionOne-\u003egetLabel(); // 'Option One'\nMyValueIntBackedEnum::OptionOne-\u003egetValue(); // 1\n```\n\n## Instantiation\n\nAll enum types can be instantiaed with the following methods:\n\n```php\nMyEnum::fromKey('\u003ckey\u003e');\nMyEnum::fromValue('\u003cvalue\u003e');\nMyEnum::fromName('\u003cname\u003e');\nMyEnum::fromIndex('\u003cindex\u003e');\n// or\nMyEnum::tryFromKey('\u003ckey\u003e');\nMyEnum::tryFromValue('\u003cvalue\u003e');\nMyEnum::tryFromName('\u003cname\u003e');\nMyEnum::tryFromIndex('\u003cindex\u003e');\n```\n\n\n## Lists\n\nEnumerable provides three main ways of listing cases:\n\n```php\n// Key to label map\nMyEnum::getOptions() =\u003e [\n    '\u003ckey\u003e' =\u003e '\u003clabel\u003e',\n];\n\n// Key to value map\nMyEnum::getValues() =\u003e [\n    '\u003ckey\u003e' =\u003e '\u003cvalue\u003e',\n];\n\n// Alias to cases()\nMyEnum::getCases() =\u003e [\n    '\u003ckey\u003e' =\u003e '[MyEnum::\u003cname\u003e]',\n];\n```\n\n## Licensing\n\nEnumerable is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecodelabs%2Fenumerable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdecodelabs%2Fenumerable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecodelabs%2Fenumerable/lists"}