Skip to content

Shivoham/DictionaryBundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DictionaryBundle

Build Status Scrutinizer Code Quality

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

Requirements

  • Symfony >= 2.4

Installation

Add the DictionaryBundle to your composer.json:

{
    "require": {
        "knplabs/dictionary-bundle": "dev-master"
    }
}

Register the bundle in app/AppKernel.php

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

Basic usage

Define dictionaries in your config.yml file:

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

You will be able to retreive it trough the dictionary registry service:

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

Dictionary form type

Now, use them in your forms:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        // ...
        ->add('civility', 'dictionary', array(
            'name' => 'my_dictionary'
        ));
    ;
}

The dictionary form type extends the symfony's choice type and its options.

Advanced usage

You can specify the indexation mode of each dictionary

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

Transformers

For now, this bundle is only able to resolve your class constants:

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 !

Add your own transformers

Create your class that implements TransformerInterface. Load your transformer and tag it as knp_dictionary.value_transformer.

services:
	my_bundle.my_namespace.my_transformer:
    	class: %my_transformer_class%
    	tags:
        	- { name: knp_dictionary.value_transformer }

About

Moved to https://github.com/KnpLabs/DictionaryBundle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages