tacman/dictionary-bundle

你是否经常在应用中重复静态选择,如性别或礼貌?

安装: 816

依赖: 0

建议者: 0

安全性: 0

星星: 0

关注者: 2

分支: 24

类型:symfony-bundle

4.0.6 2024-01-05 00:27 UTC

README

这个分支与https://github.com/KnpLabs/DictionaryBundle几乎相同,但允许使用 Symfony 6.3 并添加一些返回类型以避免弃用警告。

由于 php 7.4 已过 EOL,因此将最低要求提升至 php8。

CircleCI Scrutinizer Code Quality

你是否经常在应用中重复静态选择,如性别或礼貌?

要求

  • 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_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 提供程序,可用于从字典中提供随机条目。

爱丽丝

要在 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/