{"id":20945092,"url":"https://github.com/vjik/php-enum","last_synced_at":"2025-05-14T01:31:35.042Z","repository":{"id":50943080,"uuid":"97271834","full_name":"vjik/php-enum","owner":"vjik","description":"PHP Enum Implementation","archived":false,"fork":false,"pushed_at":"2021-05-27T06:50:39.000Z","size":41,"stargazers_count":21,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-28T22:47:55.662Z","etag":null,"topics":["enum","php","php-enum"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vjik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-14T20:51:31.000Z","updated_at":"2021-12-12T18:45:02.000Z","dependencies_parsed_at":"2022-08-25T13:01:39.286Z","dependency_job_id":null,"html_url":"https://github.com/vjik/php-enum","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fphp-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fphp-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fphp-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fphp-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vjik","download_url":"https://codeload.github.com/vjik/php-enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225269316,"owners_count":17447507,"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","php","php-enum"],"created_at":"2024-11-18T23:46:39.364Z","updated_at":"2024-11-18T23:46:40.028Z","avatar_url":"https://github.com/vjik.png","language":"PHP","readme":"# PHP Enum Implementation\n\n[![Latest Stable Version](https://poser.pugx.org/vjik/php-enum/v/stable.png)](https://packagist.org/packages/vjik/php-enum)\n[![Total Downloads](https://poser.pugx.org/vjik/php-enum/downloads.png)](https://packagist.org/packages/vjik/php-enum)\n[![Build status](https://github.com/vjik/php-enum/workflows/build/badge.svg)](https://github.com/vjik/php-enum/actions?query=workflow%3Abuild)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fvjik%2Fphp-enum%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/vjik/php-enum/master)\n[![static analysis](https://github.com/vjik/php-enum/workflows/static%20analysis/badge.svg)](https://github.com/vjik/php-enum/actions?query=workflow%3A%22static+analysis%22)\n\nThe package implement ideas from [RFC Enumerations](https://wiki.php.net/rfc/enumerations) and provide abstract class `Enum` that intended to create\n[enumerated objects](https://en.wikipedia.org/wiki/Enumerated_type) with support [extra data](#extradata) and auxiliary static functions [`values()`](#values), [`cases()`](#cases) and [`isValid()`](#isValid).\n\n## Requirements\n\n- PHP 8.0 or higher.\n\n## Installation\n\nThe package could be installed with composer:\n\n```shell\ncomposer require vjik/php-enum --prefer-dist\n```\n\n## General usage\n\n### Declaration of class\n\n```php\nuse Vjik\\Enum\\Enum;\n\n/**\n * @method static self NEW()\n * @method static self PROCESS()\n * @method static self DONE()\n */\nfinal class Status extends Enum\n{\n    private const NEW = 'new';\n    private const PROCESS = 'process';\n    private const DONE = 'done';\n}\n```\n\n### Creating an object\n\n#### By static method `from()`\n\n```php\n$process = Status::from('process');\n```\n\nOn create object with invalid value throws `ValueError`.\n\n#### By static method `tryFrom()`\n\n```php\n$process = Status::tryFrom('process'); // Status object with value \"process\"\n$process = Status::tryFrom('not-exists'); // null\n```\n\nOn create object with invalid value returns `null`.\n\n#### By static method with a name identical to the constant name\n\nStatic methods are automatically implemented to provide quick access to an enum value.\n\n```php\n$process = Status::PROCESS();\n```\n\n### Getting value and name\n\n```php\nStatus::DONE()-\u003egetName(); // DONE\nStatus::DONE()-\u003egetValue(); // done\n```\n\n### \u003ca name=\"extradata\"\u003e\u003c/a\u003eClass with extra data\n\nSet data in the protected static function `data()` and create getters using the protected method `getPropertyValue()`.\nAlso you can create getter using protected method `match()`.\n\n```php\nuse Vjik\\Enum\\Enum;\n\n/**\n * @method static self CREATE()\n * @method static self UPDATE()\n */\nfinal class Action extends Enum\n{\n    private const CREATE = 1;\n    private const UPDATE = 2;\n\n    protected static function data(): array\n    {\n        return [\n            self::CREATE =\u003e [\n                'tip' =\u003e 'Create document',\n            ],\n            self::UPDATE =\u003e [\n                'tip' =\u003e 'Update document',\n            ],\n        ];\n    }\n\n    public function getTip(): string\n    {\n        /** @var string */\n        return $this-\u003egetPropertyValue('tip');\n    }\n    \n    public function getColor(): string\n    {\n        return $this-\u003ematch([\n            self::CREATE =\u003e 'red',\n            self::UPDATE =\u003e 'blue',\n        ]);\n    }\n    \n    public function getCode(): int\n    {\n        return $this-\u003ematch([\n            self::CREATE =\u003e 1,\n        ], 99);\n    }\n}\n```\n\nUsage:\n\n```php\necho Action::CREATE()-\u003egetTip();\necho Action::CREATE()-\u003egetColor();\necho Action::CREATE()-\u003egetCode();\n```\n\n### Auxiliary static functions\n\n#### \u003ca name=\"values\"\u003e\u003c/a\u003e List of values `values()`\n\nReturns list of values.\n\n```php\n// [1, 2]\nAction::values(); \n```\n\n#### \u003ca name=\"cases\"\u003e\u003c/a\u003e List of objects `cases()`\n\nReturns list of objects:\n\n```php\n// [$createObject, $updateObject]\nAction::cases();\n```\n\n#### \u003ca name=\"isValid\"\u003e\u003c/a\u003e Validate value `isValid()`\n\nCheck if value is valid on the enum set.\n\n```php\nAction::isValid(1); // true\nAction::isValid(99); // false\n```\n\n### Casting to string\n\n`Enum` support casting to string (using magic method `__toString`). The value is returned as a string.\n\n```php\necho Status::DONE(); // done\n```\n\n## Testing\n\n### Unit testing\n\nThe package is tested with [PHPUnit](https://phpunit.de/). To run tests:\n\n```shell\n./vendor/bin/phpunit\n```\n\n### Mutation testing\n\nThe package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:\n\n```shell\n./vendor/bin/infection\n```\n\n### Static analysis\n\nThe code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:\n\n```shell\n./vendor/bin/psalm\n```\n\n## License\n\nThe PHP Enum implementation is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.\n\n## Credits\n\nVersion 3 of this package is inspired by [`myclabs/php-enum`](https://github.com/myclabs/php-enum). \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvjik%2Fphp-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvjik%2Fphp-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvjik%2Fphp-enum/lists"}