2017 © Pedro Peláez
 

symfony-bundle dictionary-bundle

Are you often tired to repeat static choices like gender or civility in your apps ?

image

biig/dictionary-bundle

Are you often tired to repeat static choices like gender or civility in your apps ?

  • Friday, July 6, 2018
  • by Nek
  • Repository
  • 10 Watchers
  • 8 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 13 Forks
  • 3 Open issues
  • 24 Versions
  • 0 % Grown

The README.md

DictionaryBundle

Build Status Scrutinizer Code Quality, (*1)

Are you often tired to repeat static choices like gender or civility in your apps ?, (*2)

Notice: This bundle is a fork from KnpLabs/DictionaryBundle. We are working on releasing a 3.0 version but for now the released version is 2.x. You should refere to the right branch to have a good documentation., (*3)

Requirements

  • Symfony >= 3.4
  • PHP >= 7.1

Installation

Add the DictionaryBundle to your composer.json:, (*4)

{
    "require": {
        "biig/dictionary-bundle": "^3.0"
    }
}

Or install it directly with composer command, (*5)

composer require biig/dictionary-bundle

Register the bundle in app/AppKernel.php, (*6)

$bundles = array(
    // ...
    new Knp\DictionaryBundle\KnpDictionaryBundle(),
);

Basic usage

Define dictionaries in your config.yaml file:, (*7)

knp_dictionary:
    dictionaries:
        my_dictionary:      # your dictionary name
            - Foo           # your dictionary content
            - Bar
            - Baz

You will be able to retrieve it through the dictionary registry service:, (*8)

$container->get('knp_dictionary.registry')->get('my_dictionary');

Dictionary form type

Now, use them in your forms:, (*9)

use Knp\DictionaryBundle\Form\Type\DictionaryType;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('civility', DictionaryType::class, [
            'name' => 'my_dictionary'
        ])
    ;
}

The dictionary form type extends the symfony's choice type and its options., (*10)

Validation constraint

You can also use the constraint for validation. The value have to be set., (*11)

use Knp\DictionaryBundle\Validator\Constraints\Dictionary;

class User
{
    /**
     * @ORM\Column
     * @Dictionary(name="my_dictionary", multiple=false)
     */
    private $civility;
}

Advanced usage

You can specify the indexation mode of each dictionary, (*12)

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 indexation
  • value_as_key: Keys are defined from their value
  • key_value: Define your own keys
  • callable: Build a dictionary from a callable

Callable dictionary

You can create a callable dictionary:, (*13)

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., (*14)

Categories in dictionaries

If you want to retrieve some groups of dictionaries from the registry, then you can specify a category to your dictionary., (*15)

knp_dictionary:
    dictionaries:
        my_dictionary:                  # your dictionary name
            type: 'key_value'           # your dictionary type
            category: 'product.toy'     # your dictionary category
            content:                    # your dictionary content
                foo: foo_value
                bar: bar_value
                baz: baz_value

Get a registry for this category later:, (*16)

// Retrieve all dictionaries of a special category
$dictionaries = $registry->filterByCategory('product.toy')->all();

Transformers

For now, this bundle is only able to resolve your class constants:, (*17)

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 !, (*18)

Add your own transformers

Create your class that implements TransformerInterface. Load your transformer and tag it as knp_dictionary.value_transformer., (*19)

services:
    my_bundle.my_namespace.my_transformer:
        class: %my_transformer_class%
        tags:
            - { name: 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), (*20)

{% for example in dictionary('examples') %}
    {{ example }}
{% endfor %}

But you can also access directly to a value by using the same function (or filter), (*21)

{{ '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., (*22)

Alice

To register the provider in nelmio/alice, you can follow the official documentation, (*23)

or ..., (*24)

if you use the awesome knplabs/rad-fixtures-load library, the dictionary provider will be automaticaly loaded for you :), (*25)

App\Entity\User:
    john_doe:
        firstname: John
        latnale: Doe
        city: <dictionary('cities')>

Use dictionary command

You can use the following command to show your app dictionaries easily:, (*26)

php bin/console knp:dictionary:dump

If you want to display only one dictionary, you can set it name in argument, (*27)

php bin/console knp:dictionary:dump my_dictionary

Create your own dictionary implementation

Dictionary

Your dictionary implementation must implements the interface Dictionary. In Symfony >= 3.3, your class will be automatically register as dictionary service., (*28)

Dictionary Factory

You must create a dictionary factory that will be responsible to instanciate your dictionary., (*29)

services:
    # Syntax Symfony >= 3.3
    App\Dictionary\Factory\MyCustomFactory:
        tags:
            { name: 'knp_dictionary.factory' }

The Versions

31/07 2017

1.x-dev

1.9999999.9999999.9999999-dev

Are you often tired to repeat static choices like gender or civility in your apps ?

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

20/06 2016

dev-feature/valuetransformer-refactoring

dev-feature/valuetransformer-refactoring

Are you often tired to repeat static choices like gender or civility in your apps ?

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

05/10 2015
05/10 2015
12/08 2015

v1.3.1

1.3.1.0

Are you often tired to repeat static choices like gender or civility in your apps ?

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

05/06 2015

v1.3.0

1.3.0.0

Are you often tired to repeat static choices like gender or civility in your apps ?

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

14/04 2015

v1.2.0

1.2.0.0

Are you often tired to repeat static choices like gender or civility in your apps ?

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

18/03 2015

v1.1.1

1.1.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

23/02 2015

v1.1.0

1.1.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary

21/11 2014

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

dictionary