tacman / dictionary-bundle
你是否经常在应用中重复静态选择,如性别或礼貌?
Requires
- php: >=8.1
- symfony/config: ^6.3 || ^7.0
- symfony/dependency-injection: ^6.3 || ^7.0
- symfony/form: ^6.3 || ^7.0
- symfony/http-foundation: ^6.3 || ^7.0
- symfony/http-kernel: ^6.3 || ^7.0
- symfony/validator: ^6.3 || ^7.0
- twig/twig: ^3.4.3
Requires (Dev)
- beberlei/assert: ^3.3
- fakerphp/faker: ^1.20
- friends-of-phpspec/phpspec-code-coverage: ^6.3 || ^7.0
- friendsofphp/php-cs-fixer: ^3.13
- pedrotroller/php-cs-custom-fixer: ^2.28
- phpspec/phpspec: ^7.2
- phpspec/prophecy: ^1.15
- phpstan/phpstan: ^1.10
- rector/rector: ^0.18.1
- symfony/twig-bridge: ^6.3 || ^7.0
- symfony/var-dumper: ^6.3 || ^7.0
- webmozart/assert: ^1.11
- dev-tac
- dev-master / 4.x-dev
- 4.0.6
- 4.0.5
- 4.0.3
- 4.0.2
- 4.0.1
- v4.0
- v3.2.x-dev
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.0
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.0
- dev-dependabot/composer/rector/rector-tw-0.18.1
- dev-patch-6
- dev-patch-5
- dev-patch-4
- dev-symfony6
- dev-patch-2
- dev-patch-1
This package is auto-updated.
Last update: 2024-09-21 10:59:26 UTC
README
这个分支与https://github.com/KnpLabs/DictionaryBundle几乎相同,但允许使用 Symfony 6.3 并添加一些返回类型以避免弃用警告。
由于 php 7.4 已过 EOL,因此将最低要求提升至 php8。
你是否经常在应用中重复静态选择,如性别或礼貌?
要求
- PHP >= 7.4
- Symfony 5.4 或 >= 6.0
安装
运行以下命令
composer require knplabs/dictionary-bundle
在 app/AppKernel.php
中注册包
$bundles = array( // ... new Knp\DictionaryBundle\KnpDictionaryBundle(), );
维护者
如果需要审查/评论/帮助,可以联系我们
基本用法
在您的 config.yml 文件中定义字典
knp_dictionary: dictionaries: my_dictionary: # your dictionary name - Foo # your dictionary content - Bar - Baz
您可以通过注入 Collection 服务并按其键访问字典来检索它
private Dictionary $myDictionary; public function __construct( \Knp\DictionaryBundle\Dictionary\Collection $dictionaries) { $this->myDictionary = $dictionaries['my_dictionary']; }
字典表单类型
现在,在您的表单中使用它们
use Knp\DictionaryBundle\Form\Type\DictionaryType; public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('civility', DictionaryType::class, array( 'name' => 'my_dictionary' )) ; }
字典表单类型扩展了 symfony 的选择类型及其选项。
验证约束
您也可以使用约束进行验证。必须设置 value
。
use Knp\DictionaryBundle\Validator\Constraints\Dictionary; class User { /** * @ORM\Column * @Dictionary(name="my_dictionary") */ private $civility; }
高级用法
您可以指定每个字典的索引模式
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"
可用类型
value
(默认): 自然索引value_as_key
:键由其值定义key_value
:定义您自己的键callable
:从可调用对象构建字典
可调用字典
您可以创建一个可调用字典
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
可调用字典以懒加载策略加载。这意味着如果未使用字典,则不会调用可调用对象。
基于迭代器的字典
您可以从迭代器创建一个字典
knp_dictionary: dictionaries: my_iterator_dictionary: # your dictionary name type: 'iterator' # your dictionary type service: 'app.service.id' # a valid service from your application
基于迭代器的字典以懒加载策略加载。这意味着如果未使用字典,则不会获取迭代器。
组合字典
您可以将多个字典组合成一个
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
现在您有 3 个字典,payment_mode
和 extra_payment_mode
包含它们自己的值,但 combined_payment_mode
包含前面所有字典的值。
扩展字典
您可以创建一个扩展字典
knp_dictionary: dictionaries: europe: type: 'key_value' content: fr: France de: Germany world: type: 'key_value' extends: europe content: us: USA ca: Canada
字典 world
现在将包含其自己的值以及 europe
的值。
注意:您必须先定义初始字典,然后才能定义扩展字典。
转换器
目前,此包只能解析您的 类常量
my_dictionary: - MyClass::MY_CONSTANT - Foo - Bar
您想为字典值添加其他类型的转换吗?请随意创建您自己的转换器!
添加您自己的转换器
创建一个实现 TransformerInterface 的类。加载您的转换器并将其标记为 knp_dictionary.value_transformer
。
services: App\My\Transformer: tags: - knp_dictionary.value_transformer
在 Twig 中使用您的字典
您还可以通过调用 dictionary
函数(或过滤器)在您的 Twig 模板中使用您的字典。
{% for example in dictionary('examples') %} {{ example }} {% endfor %}
但您也可以使用相同的函数(或过滤器)直接访问值
{{ 'my_key'|dictionary('dictionary_name') }}
Faker 提供程序
KnpDictionaryBundle 附带一个 faker 提供程序,可用于从字典中提供随机条目。
爱丽丝
要在 nelmio/alice 中注册服务提供者,您可以参考官方文档
App\Entity\User: john_doe: firstname: John latnale: Doe city: <dictionary('cities')>
创建自己的字典实现
字典
您的字典实现必须实现 Dictionary 接口。
它将自动通过具有 autoconfigure: true
DIC 功能进行注册。
否则,您可以自己注册它
services: App\Dictionary\MyCustomDictionary: tags: - knp_dictionary.dictionary
字典工厂
您必须创建一个字典工厂,该工厂将负责实例化您的字典。
它将自动通过具有 autoconfigure: true
DIC 功能进行注册。
否则,您可以自己注册它
services: App\Dictionary\Factory\MyCustomFactory: tags: - knp_dictionary.factory
测试
phpspec
composer install vendor/bin/phpspec run
php-cs-fixer
composer install vendor/bin/php-cs-fixer fix
phpstan
首先 安装 phive。
然后...
phive install tools/phpstan process
rector (可选)
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/