{"id":24326879,"url":"https://github.com/webinarium/php-dictionary","last_synced_at":"2025-09-27T11:30:35.084Z","repository":{"id":62487710,"uuid":"56566328","full_name":"webinarium/php-dictionary","owner":"webinarium","description":"Static dictionary implementation for PHP.","archived":false,"fork":false,"pushed_at":"2021-07-03T11:31:16.000Z","size":27,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T05:36:42.335Z","etag":null,"topics":["dictionary","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/webinarium.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":"2016-04-19T05:10:10.000Z","updated_at":"2024-07-02T22:08:15.000Z","dependencies_parsed_at":"2022-11-02T10:46:35.241Z","dependency_job_id":null,"html_url":"https://github.com/webinarium/php-dictionary","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webinarium%2Fphp-dictionary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webinarium%2Fphp-dictionary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webinarium%2Fphp-dictionary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webinarium%2Fphp-dictionary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webinarium","download_url":"https://codeload.github.com/webinarium/php-dictionary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234429244,"owners_count":18831241,"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":["dictionary","php"],"created_at":"2025-01-17T21:14:51.751Z","updated_at":"2025-09-27T11:30:34.760Z","avatar_url":"https://github.com/webinarium.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PHP](https://img.shields.io/badge/PHP-7.4%2B-blue.svg)](https://secure.php.net/migration74)\n[![Latest Stable Version](https://poser.pugx.org/webinarium/php-dictionary/v/stable)](https://packagist.org/packages/webinarium/php-dictionary)\n[![Build Status](https://travis-ci.com/webinarium/php-dictionary.svg?branch=master)](https://travis-ci.com/webinarium/php-dictionary)\n[![Code Coverage](https://scrutinizer-ci.com/g/webinarium/php-dictionary/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/webinarium/php-dictionary/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/webinarium/php-dictionary/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/webinarium/php-dictionary/?branch=master)\n\n# Static dictionary implementation for PHP\n\n## Requirements\n\nPHP needs to be a minimum version of PHP 7.4.\n\n## Installation\n\nThe recommended way to install is via Composer:\n\n```bash\ncomposer require webinarium/php-dictionary\n```\n\n## Usage\n\nTo create custom dictionary you have to extend `StaticDictionary` class and override the `$dictionary` static array.\nAfter that you can use [`StaticDictionaryInterface`](https://github.com/webinarium/php-dictionary/blob/master/src/StaticDictionaryInterface.php) to work with your dictionary.\n\nExample dictionary:\n\n```php\nnamespace Dictionary;\n\nclass Color extends StaticDictionary\n{\n    public const BLACK   = 'Black';\n    public const BLUE    = 'Blue';\n    public const GREEN   = 'Green';\n    public const CYAN    = 'Cyan';\n    public const RED     = 'Red';\n    public const MAGENTA = 'Magenta';\n    public const YELLOW  = 'Yellow';\n    public const WHITE   = 'White';\n\n    protected static array $dictionary = [\n        self::BLACK   =\u003e '#000000',\n        self::BLUE    =\u003e '#0000FF',\n        self::GREEN   =\u003e '#00FF00',\n        self::CYAN    =\u003e '#00FFFF',\n        self::RED     =\u003e '#FF0000',\n        self::MAGENTA =\u003e '#FF00FF',\n        self::YELLOW  =\u003e '#FFFF00',\n        self::WHITE   =\u003e '#FFFFFF',\n    ];\n}\n```\n\nInput sanitizing:\n\n```php\npublic function setColor($color)\n{\n    if (Dictionary\\Color::has($color)) {\n        $this-\u003ecolor = $color;\n    }\n}\n```\n\nSymfony validation:\n\n```php\nuse Symfony\\Component\\Validator\\Constraints;\n\nclass Settings\n{\n    /**\n     * @Constraints\\NotNull()\n     * @Constraints\\Choice(callback = {\"Dictionary\\Color\", \"keys\"})\n     */\n    public $color;\n}\n```\n\nSymfony form:\n\n```php\nclass ColorType extends AbstractType\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder-\u003eadd('color', ChoiceType::class, [\n            'label'   =\u003e 'color',\n            'choices' =\u003e array_flip(Dictionary\\Color::all()),\n        ]);\n    }\n}\n```\n\nPlease note, if you try to get a value from your dictionary using non-existing key, you will get `NULL` without any failures or warnings.\nSometimes it's useful to have a default fallback value to be returned instead of `NULL`.\nThis can be done by defining a `FALLBACK` constant in your dictionary class:\n\n```php\nclass Shell extends StaticDictionary\n{\n    public const FALLBACK = self::UNITY;\n\n    public const XFCE  = 1;\n    public const KDE   = 2;\n    public const GNOME = 3;\n    public const LXDE  = 4;\n    public const UNITY = 5;\n    public const MATE  = 6;\n\n    protected static array $dictionary = [\n        self::UNITY =\u003e 'Unity',\n        self::GNOME =\u003e 'Gnome',\n        self::KDE   =\u003e 'KDE',\n        self::LXDE  =\u003e 'LXDE',\n        self::XFCE  =\u003e 'Xfce',\n        self::MATE  =\u003e 'MATE',\n    ];\n}\n\n// This returns 'Gnome'\nShell::get(Shell::GNOME);\n\n// This returns 'Unity'\nShell::get(Color::BLACK);\n```\n\nIf your dictionary should be built in run-time, you may skip the `$dictionary` static array and overload `dictionary()` static function instead of that:\n\n```php\nclass Timezone extends StaticDictionary\n{\n    const FALLBACK = 'UTC';\n\n    protected static function dictionary(): array\n    {\n        return timezone_identifiers_list();\n    }\n}\n```\n\n## Development\n\n```bash\n./bin/php-cs-fixer fix\nXDEBUG_MODE=coverage ./bin/phpunit --coverage-text\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebinarium%2Fphp-dictionary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebinarium%2Fphp-dictionary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebinarium%2Fphp-dictionary/lists"}