knplabs / dictionary-bundle
你是否经常在应用程序中重复使用诸如性别或礼貌等静态选项感到疲倦?
Requires
- php: >=8.1
- symfony/config: ^5.4 || ^6.4 || ^7.0@dev
- symfony/dependency-injection: ^5.4 || ^6.4 || ^7.0@dev
- symfony/form: ^5.4 || ^6.4 || ^7.0@dev
- symfony/http-foundation: ^5.4 || ^6.4 || ^7.0@dev
- symfony/http-kernel: ^5.4 || ^6.4 || ^7.0@dev
- symfony/validator: ^5.4 || ^6.4 || ^7.0@dev
- twig/twig: ^2.15.3 || ^3.4.3
Requires (Dev)
- fakerphp/faker: 1.23.1
- friends-of-phpspec/phpspec-code-coverage: 6.3.0
- friendsofphp/php-cs-fixer: 3.49.0
- pedrotroller/php-cs-custom-fixer: 2.33.0
- phpspec/phpspec: 7.5.0
- phpspec/prophecy: 1.18.0
- phpstan/phpstan: 1.10.59
- rector/rector: 1.0.1
- symfony/twig-bridge: ^5.4 || ^6.4 || ^7.0@dev
- symfony/var-dumper: ^5.4 || ^6.4 || ^7.0@dev
- webmozart/assert: 1.11.0
- dev-master / 3.5.x-dev
- v3.5.0
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.0
- 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/phpstan/phpstan-1.12.1
- dev-dependabot/composer/friendsofphp/php-cs-fixer-3.64.0
- dev-dependabot/composer/phpstan/phpstan-1.12.0
- dev-dependabot/composer/friends-of-phpspec/phpspec-code-coverage-6.4.1
- dev-dependabot/composer/rector/rector-1.2.2
- dev-rel/php83-symfony7
- dev-refactor/phpstan-max
- dev-release
- dev-refactor/circleci-config
This package is auto-updated.
Last update: 2024-09-04 00:50:10 UTC
README
你是否经常在应用程序中重复使用诸如性别或礼貌等静态选项感到疲倦?
需求
- PHP >= 8.1
- Symfony 5.4, 6.4 或 7.*
安装
运行以下命令
composer require knplabs/dictionary-bundle
在 config/bundles.php
中注册包
$bundles = array( // ... Knp\DictionaryBundle\KnpDictionaryBundle::class => ['all' => true], );
维护者
如果您需要审查/评论/帮助,可以联系我们
基本用法
在您的 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 provider,可以用于从字典中提供随机条目。
Alice
要在 nelmio/alice 中注册提供程序,您可以按照 官方文档 进行操作
App\Entity\User: john_doe: firstname: John latnale: Doe city: <dictionary('cities')>
创建您自己的字典实现
字典
您的字典实现必须实现Dictionary接口。
它将自动通过DIC特性的autoconfigure: true
进行注册。
否则,您可以自行注册。
services: App\Dictionary\MyCustomDictionary: tags: - knp_dictionary.dictionary
字典工厂
您必须创建一个字典工厂,该工厂将负责实例化您的字典。
它将自动通过DIC特性的autoconfigure: true
进行注册。
否则,您可以自行注册。
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/