webinarium/php-dictionary
Composer 安装命令:
composer require webinarium/php-dictionary
包简介
Static dictionary implementation for PHP
关键字:
README 文档
README
Static dictionary implementation for PHP
Requirements
PHP needs to be a minimum version of PHP 7.4.
Installation
The recommended way to install is via Composer:
composer require webinarium/php-dictionary
Usage
To create custom dictionary you have to extend StaticDictionary class and override the $dictionary static array.
After that you can use StaticDictionaryInterface to work with your dictionary.
Example dictionary:
namespace Dictionary; class Color extends StaticDictionary { public const BLACK = 'Black'; public const BLUE = 'Blue'; public const GREEN = 'Green'; public const CYAN = 'Cyan'; public const RED = 'Red'; public const MAGENTA = 'Magenta'; public const YELLOW = 'Yellow'; public const WHITE = 'White'; protected static array $dictionary = [ self::BLACK => '#000000', self::BLUE => '#0000FF', self::GREEN => '#00FF00', self::CYAN => '#00FFFF', self::RED => '#FF0000', self::MAGENTA => '#FF00FF', self::YELLOW => '#FFFF00', self::WHITE => '#FFFFFF', ]; }
Input sanitizing:
public function setColor($color) { if (Dictionary\Color::has($color)) { $this->color = $color; } }
Symfony validation:
use Symfony\Component\Validator\Constraints; class Settings { /** * @Constraints\NotNull() * @Constraints\Choice(callback = {"Dictionary\Color", "keys"}) */ public $color; }
Symfony form:
class ColorType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('color', ChoiceType::class, [ 'label' => 'color', 'choices' => array_flip(Dictionary\Color::all()), ]); } }
Please note, if you try to get a value from your dictionary using non-existing key, you will get NULL without any failures or warnings.
Sometimes it's useful to have a default fallback value to be returned instead of NULL.
This can be done by defining a FALLBACK constant in your dictionary class:
class Shell extends StaticDictionary { public const FALLBACK = self::UNITY; public const XFCE = 1; public const KDE = 2; public const GNOME = 3; public const LXDE = 4; public const UNITY = 5; public const MATE = 6; protected static array $dictionary = [ self::UNITY => 'Unity', self::GNOME => 'Gnome', self::KDE => 'KDE', self::LXDE => 'LXDE', self::XFCE => 'Xfce', self::MATE => 'MATE', ]; } // This returns 'Gnome' Shell::get(Shell::GNOME); // This returns 'Unity' Shell::get(Color::BLACK);
If your dictionary should be built in run-time, you may skip the $dictionary static array and overload dictionary() static function instead of that:
class Timezone extends StaticDictionary { const FALLBACK = 'UTC'; protected static function dictionary(): array { return timezone_identifiers_list(); } }
Development
./bin/php-cs-fixer fix XDEBUG_MODE=coverage ./bin/phpunit --coverage-text
webinarium/php-dictionary 适用场景与选型建议
webinarium/php-dictionary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.35k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dictionary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webinarium/php-dictionary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webinarium/php-dictionary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webinarium/php-dictionary 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
Dictionary implementation for PHP
Tresholds Governor, aims to facilitate the protection of authentication against brute force and dictionary attacks
A PHP/Laravel Wrapper for oxford dictionary API
Adds support for template PHPDoc tags
Yii2 Dictionary to create, manage, and delete Multilanguage Dictionary in a Yii2 site.
统计信息
- 总下载量: 19.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-23