knplabs/dictionary-bundle

你是否经常在应用程序中重复使用诸如性别或礼貌等静态选项感到疲倦?

安装量252,405

依赖项: 1

建议者: 0

安全性: 0

星级: 86

关注者: 22

分支: 24

开放问题: 8

类型:symfony-bundle


README

CircleCI Scrutinizer Code Quality

你是否经常在应用程序中重复使用诸如性别或礼貌等静态选项感到疲倦?

需求

  • 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_modeextra_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/