tacman/dictionary-bundle
Composer 安装命令:
composer require tacman/dictionary-bundle
包简介
Are you often tired to repeat static choices like gender or civility in your apps ?
关键字:
README 文档
README
This fork is nearly identical to https://github.com/KnpLabs/DictionaryBundle, but allows for Symfony 6.3 and adds some return types to avoid deprecation warnings.
It also bumps the minimum requirements to php8, since php 7.4 is past EOL.
Are you often tired to repeat static choices like gender or civility in your apps ?
Requirements
- PHP 8.3+
- Symfony ^7.1
Installation
composer require knplabs/dictionary-bundle
Maintainers
You can ping us if need some reviews/comments/help:
Basic usage
Define dictionaries in your config.yml file:
knp_dictionary: dictionaries: my_dictionary: # your dictionary name - Foo # your dictionary content - Bar - Baz
You will be able to retreive it by injecting the Collection service and accessing the dictionary by its key
private Dictionary $myDictionary; public function __construct( \Knp\DictionaryBundle\Dictionary\Collection $dictionaries) { $this->myDictionary = $dictionaries['my_dictionary']; }
Dictionary form type
Now, use them in your forms:
use Knp\DictionaryBundle\Form\Type\DictionaryType; public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('civility', DictionaryType::class, array( 'name' => 'my_dictionary' )) ; }
The dictionary form type extends the symfony's choice type and its options.
Validation constraint
You can also use the constraint for validation. The value has to be set.
use Knp\DictionaryBundle\Validator\Constraints\Dictionary; class User { #[ORM\Column] #[Dictionary(name: "my_dictionary")] private $civility; }
Advanced usage
You can specify the indexation mode of each dictionary
knp_dictionary: dictionaries: my_dictionary: # your dictionary name type: 'key_value' # your dictionary type content: # your dictionary content "foo": "foo_value" "bar": "bar_value" "baz": "baz_value"
Available types
value(default) : Natural indexationvalue_as_key: Keys are defined from their valuekey_value: Define your own keyscallable: Build a dictionary from a callable
Callable dictionary
You can create a callable dictionary:
knp_dictionary: dictionaries: my_callable_dictionary: # your dictionary name type: 'callable' # your dictionary type service: 'app.service.id' # a valid service from your application method: 'getSomething' # the method name to execute
Callable dictionaries are loaded with a lazy strategy. It means that the callable will not be called if you do not use the dictionary.
Iterator based dictionary
You can create a dictionary from an iterator:
knp_dictionary: dictionaries: my_iterator_dictionary: # your dictionary name type: 'iterator' # your dictionary type service: 'app.service.id' # a valid service from your application
Iterator based dictionaries are loaded with a lazy strategy. It means that the iterator will not be fetched if you do not use the dictionary.
Combined dictionary
You can combine multiple dictionaries into a single one:
knp_dictionary: dictionaries: payment_mode: type: key_value content: card: "credit card" none: "none" extra_payment_mode: type: key_value content: bank_transfer: "Bank transfer" other: "Other" combined_payment_mode: type: combined dictionaries: - payment_mode - extra_payment_mode
Now you have 3 dictionaries, payment_mode and extra_payment_mode contain
their own values but combined_payment_mode contains all the values of the previous ones.
Extended dictionary
You can create an extended dictionary:
knp_dictionary: dictionaries: europe: type: 'key_value' content: fr: France de: Germany world: type: 'key_value' extends: europe content: us: USA ca: Canada
The dictionary world will now contain its own values in addition
to the europe values.
Note: You must define the initial dictionary BEFORE the extended one.
Transformers
For now, this bundle is only able to resolve your class constants:
my_dictionary: - MyClass::MY_CONSTANT - Foo - Bar
You want to add other kinds of transformations for your dictionary values ? Feel free to create your own transformer !
Add your own transformers
Create your class that implements TransformerInterface.
Load your transformer and tag it as knp_dictionary.value_transformer.
services: App\My\Transformer: tags: - knp_dictionary.value_transformer
Use your dictionary in twig
You can also use your dictionary in your Twig templates via calling dictionary function (or filter).
{% for example in dictionary('examples') %}
{{ example }}
{% endfor %}
But you can also access directly to a value by using the same function (or filter)
{{ 'my_key'|dictionary('dictionary_name') }}
Faker provider
The KnpDictionaryBundle comes with a faker provider that can be used to provide a random entry from a dictionary.
Alice
To register the provider in nelmio/alice, you can follow the official documentation
App\Entity\User: john_doe: firstname: John latnale: Doe city: <dictionary('cities')>
Create your own dictionary implementation
Dictionary
Your dictionary implementation must implements the interface Dictionary.
It is automaticaly registered with the autoconfigure: true DIC feature.
Else you can register it by your self:
services: App\Dictionary\MyCustomDictionary: tags: - knp_dictionary.dictionary
Dictionary Factory
You must create a dictionary factory that will be responsible to instanciate your dictionary.
It is automaticaly registered with the autoconfigure: true DIC feature.
Else you can register it by your self:
services: App\Dictionary\Factory\MyCustomFactory: tags: - knp_dictionary.factory
Tests
phpspec
composer install vendor/bin/phpspec run
php-cs-fixer
composer install vendor/bin/php-cs-fixer fix
phpstan
First install phive.
Then...
phive install tools/phpstan process
rector (optional)
rector process --set php70 --set php71 --set php72 --set code-quality --set coding-style --set symfony34 --set twig240 --set psr-4 --set solid src/ spec/
tacman/dictionary-bundle 适用场景与选型建议
tacman/dictionary-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.1k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dictionary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tacman/dictionary-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tacman/dictionary-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tacman/dictionary-bundle 相关的其它包
同方向 / 同关键字的高下载量 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.
统计信息
- 总下载量: 1.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-11