{"id":18547805,"url":"https://github.com/neutron-pro/enum-php","last_synced_at":"2025-05-15T07:34:29.978Z","repository":{"id":48858430,"uuid":"374199187","full_name":"Neutron-Pro/enum-php","owner":"Neutron-Pro","description":"Added an enumeration system as close as possible to PHP 8.1 for earlier versions.","archived":false,"fork":false,"pushed_at":"2021-07-09T16:22:00.000Z","size":71,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-02-17T09:43:24.384Z","etag":null,"topics":["enum","enumeration","php","php-library","php7","php8","reflection"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Neutron-Pro.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-05T19:44:19.000Z","updated_at":"2021-10-05T08:01:20.000Z","dependencies_parsed_at":"2022-09-03T14:10:36.903Z","dependency_job_id":null,"html_url":"https://github.com/Neutron-Pro/enum-php","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neutron-Pro%2Fenum-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neutron-Pro%2Fenum-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neutron-Pro%2Fenum-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Neutron-Pro%2Fenum-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Neutron-Pro","download_url":"https://codeload.github.com/Neutron-Pro/enum-php/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254297608,"owners_count":22047532,"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","enumeration","php","php-library","php7","php8","reflection"],"created_at":"2024-11-06T20:31:41.717Z","updated_at":"2025-05-15T07:34:29.916Z","avatar_url":"https://github.com/Neutron-Pro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Enum library for PHP 7.1 or more.\n\n## Installation:\n\n---\n\n### Only Enum PHP whiteout Doctrine and Symfony:\n\n```\ncomposer require neutronstars/enum\n```\n\n---\n\n### For Doctrine\n\n```\ncomposer require neutronstars/doctrine-enum-php-type\n```\n\n\u003e Docs: https://github.com/Neutron-Pro/doctrine-enum-php-type\n\n---\n\n### For Symfony\n\n```\ncomposer require neutronstars/symfony-normalizer-enum-php\n```\n\n\u003e Docs: https://github.com/Neutron-Pro/symfony-normalizer-enum-php\n\n### For Symfony and Doctrine\n\n```\ncomposer require neutronstars/doctrine-enum-php-type\ncomposer require neutronstars/symfony-normalizer-enum-php\n```\n\n---\n\n## How create an Enum class:\n\n### Method 1\n\n```php\n/**\n * @method static self ONE()\n * @method static self TWO()\n * @method static self THREE()\n */\nclass MyEnum extends \\NeutronStars\\Enum\\Enum\n{\n   public const ONE = null;\n   public const TWO = null;\n   public const THREE = null;\n}\n```\n\nThis enum does not require a constructor. We simply create a list of constants that have no value other than their name.\n\n### Method 2\n\n```php\n/**\n * @method static self ONE()\n * @method static self TWO()\n * @method static self THREE()\n */\nclass MyStringEnum extends \\NeutronStars\\Enum\\Enum\n{\n   public const ONE   = 'One';\n   public const TWO   = 'Two';\n   public const THREE = 'Three';\n}\n```\n\n```php\n/**\n * @method static self ONE()\n * @method static self TWO()\n * @method static self THREE()\n */\nclass MyIntEnum extends \\NeutronStars\\Enum\\Enum\n{\n   public const ONE   = 1;\n   public const TWO   = 2;\n   public const THREE = 3;\n}\n```\n\n---\n\n## Use an Enum class:\n\n\n### Get instance of an enum\n\n```php\n$two = MyStringEnum::TWO();\n\necho $two; // return TWO\necho $two-\u003ekey; // return TWO\necho $two-\u003evalue; // return Two\n```\n\n### Get instance of an enum from a string\n\n```php\n$three = MyStringEnum::from('THREE');\n\necho $three; // THREE\necho $three-\u003ekey; // return THREE\necho $three-\u003evalue; // return Three\n```\n\n### Compare the instances of an enum\n\n```php\n$value = MyStringEnum::tryFrom($_GET['number']);\nif ($value === MyStringEnum::ONE()) {\n  echo 'The number is ONE !';\n}\n```\nResult:\n\n```\nif $_GET['number] is ONE or One or 1 then 'The number is ONE !'\nelse nothing.\n```\n\n---\n\n### Difference between `from` and `tryFrom`.\n\nIf you use \"tryFrom\" and the parameter value is not found, the method returns null.\n\nIf you use \"from\" and the value of the parameter is not found, an error of type ValueError is raised.\n\n---\n\n### Retrieve all keys of the enum.\n\n```php\n$cases = MyIntEnum::cases();\n```\n\nResult:\n\n```\n[\n  MyIntEnum::ONE(),\n  MyIntEnum::TWO(),\n  MyIntEnum::THREE() \n]\n```\n\n```php\nforeach (MyIntEnum::cases() as $case) {\n    echo '- ' . $case-\u003evalue;\n}\n```\n\nResult:\n\n```\n- 1\n- 2\n- 3\n```\n\n---\n\n### Serialization and Deserialization\n\nYou can serialize an enum with the serialize function of PHP.\n\n```php\n$serialized = serialize(MyEnum::THREE());\n```\n\nBut for deserialization, there will be a small difference, You must use the `from` or `tryFrom` method:\n\n```php\n$deserialized = MyEnum::from(unserialize($deserialized));\n// $deserialized = MyEnum::THREE()\n```\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneutron-pro%2Fenum-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneutron-pro%2Fenum-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneutron-pro%2Fenum-php/lists"}