{"id":19240930,"url":"https://github.com/webdevcave/enum-index-accessor-php","last_synced_at":"2025-02-23T14:27:26.416Z","repository":{"id":235645692,"uuid":"791056026","full_name":"WebdevCave/enum-index-accessor-php","owner":"WebdevCave","description":"Dynamic index accessing made simple","archived":false,"fork":false,"pushed_at":"2024-04-24T04:13:56.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-05T06:19:04.098Z","etag":null,"topics":["accessor","enum","enums","extension","php","php-library","php8"],"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/WebdevCave.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-24T02:35:38.000Z","updated_at":"2024-04-24T22:05:45.000Z","dependencies_parsed_at":"2024-04-24T05:08:59.308Z","dependency_job_id":"1c955159-cc96-46fe-9157-1e786a4cc992","html_url":"https://github.com/WebdevCave/enum-index-accessor-php","commit_stats":null,"previous_names":["webdevcave/enum-index-accessor"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebdevCave%2Fenum-index-accessor-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebdevCave%2Fenum-index-accessor-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebdevCave%2Fenum-index-accessor-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebdevCave%2Fenum-index-accessor-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebdevCave","download_url":"https://codeload.github.com/WebdevCave/enum-index-accessor-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240326601,"owners_count":19783877,"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":["accessor","enum","enums","extension","php","php-library","php8"],"created_at":"2024-11-09T17:09:37.064Z","updated_at":"2025-02-23T14:27:26.388Z","avatar_url":"https://github.com/WebdevCave.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# enum-index-accessor\n\n## Install with composer:\n\n```\ncomposer install webdevcave/enum-index-accessor\n```\n\n## Why?\n\nFrom php 8.1 or newer, the possibility organized enumerators was made real.\nIt works just fine until we have to read those values dynamically. Here is an example:\n\n```php\nenum HexColors: string {\n    case RED = '#FF0000';\n    case GREEN = '#00FF00';\n    case BLUE = '#0000FF';\n    case WHITE = '#FFFFFF';\n    case BLACK = '#000000';\n    // and so on...\n}\n\n$index = 'BLUE'; // Imagine this is a dynamic value\n$constant = HexColors::class.\"::$index\"; // In a real-world application HexColors\n                                         // will most probably be declared under a namespace\n$color = null;\n\n// Before we proceed, we have to ensure the specified index exists or our code will break\nif (defined($constant)) {\n    $color = constant($constant)-\u003evalue; // Now we read its value\n}\n\n// You will probably want to assign a default value in case something went wrong\nif (is_null($color)) {\n    $color = HexColors::RED-\u003evalue;\n}\n\n// Now we can finally proceed with our task...\n```\n\nDoes this looks too verbose and/or messy for your taste? Now imagine if your application have to read multiple values\nlike this.\n\nThat's why we created this package! Now you can do the same task this way:\n\n```php\n$color = HexColors::tryValue($index) ?? HexColors::value('RED');\n```\n\nHow?\n\n```php\nuse \\WebdevCave\\EnumIndexAccessor\\BackedEnumIndexAccessor; // step 1: Import the trait\n\nenum HexColors: string {\n    case RED = '#FF0000';\n    case GREEN = '#00FF00';\n    case BLUE = '#0000FF';\n    case WHITE = '#FFFFFF';\n    case BLACK = '#000000';\n    // and so on...\n    \n    use BackedEnumIndexAccessor; // step 2: use it\n}\n```\n\n~ And voila! Magic 🪄! \n\n---\n\n## Other use cases...\n\nWe followed the php team standards for naming the methods for ease of use. Here is a list of all of them:\n\n```php\nHexColors::hasIndex($index); // Checks if a case statement was set in the enumerator (boolean)\nHexColors::index($index); // Read the object from given index (skips index check)\nHexColors::tryIndex($index); // Read the object from given index (null on non-existent)\nHexColors::value($index); // Read the value from given index (skips index check)\nHexColors::tryValue($index); // Read the value from given index (null on non-existent)\n```\n\nFor pure enumerators (without backing values), use the pure enumerator trait as follows:\n\n```php\nuse \\WebdevCave\\EnumIndexAccessor\\PureEnumIndexAccessor; // step 1: Import the trait\n\nenum Fruits {\n    case ORANGE;\n    case PEAR;\n    case APPLE;\n    // and so on...\n    \n    use PureEnumIndexAccessor; // step 2: use it\n}\n```\n\n**Important note:** The methods `value` and `tryValue` are not available as pure enumerators doesn't carry any values on them\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebdevcave%2Fenum-index-accessor-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebdevcave%2Fenum-index-accessor-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebdevcave%2Fenum-index-accessor-php/lists"}