PHPackages                             tacman/dictionary-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tacman/dictionary-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

tacman/dictionary-bundle
========================

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

5.0.0(4mo ago)01.1kMITPHPPHP ^8.4CI failing

Since Nov 21Pushed 4mo agoCompare

[ Source](https://github.com/tacman/DictionaryBundle)[ Packagist](https://packagist.org/packages/tacman/dictionary-bundle)[ RSS](/packages/tacman-dictionary-bundle/feed)WikiDiscussions tac Synced 1mo ago

READMEChangelog (10)Dependencies (18)Versions (43)Used By (0)

DictionaryBundle
================

[](#dictionarybundle)

This fork is nearly identical to , but allows for Symfony 6.3 and adds some return types to avoid deprecation warnings.

It also bumps the minimum requirements to php8, since php 7.4 is past EOL.

[![CircleCI](https://camo.githubusercontent.com/253a2d2516bbbccaeee0122f02e9e111ba1a33a0fbdebf1ffd38542bfdef5137/68747470733a2f2f636972636c6563692e636f6d2f67682f4b6e704c6162732f44696374696f6e61727942756e646c652e7376673f7374796c653d737667)](https://circleci.com/gh/KnpLabs/DictionaryBundle)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4576301fbddb9a4e9136e300ca0719659bddbc42ac5d5e6393de1f3b6ade562e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4b6e704c6162732f44696374696f6e61727942756e646c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/KnpLabs/DictionaryBundle/?branch=master)

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

Requirements
------------

[](#requirements)

- PHP 8.3+
- Symfony ^7.1

Installation
------------

[](#installation)

```
composer require knplabs/dictionary-bundle
```

Maintainers
-----------

[](#maintainers)

You can ping us if need some reviews/comments/help:

- [@AntoineLelaisant](https://github.com/AntoineLelaisant)
- [@PedroTroller](https://github.com/PedroTroller)

Basic usage
-----------

[](#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 by injecting the Collection service and accessing the dictionary by its key

```
    private Dictionary $myDictionary;
    public function __construct(
        \Knp\DictionaryBundle\Dictionary\Collection $dictionaries)
    {
        $this->myDictionary = $dictionaries['my_dictionary'];
    }
```

### Dictionary form type

[](#dictionary-form-type)

Now, use them in your forms:

```
use Knp\DictionaryBundle\Form\Type\DictionaryType;

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

The dictionary form type extends the [symfony's choice type](http://symfony.com/fr/doc/current/reference/forms/types/choice.html) and its options.

### Validation constraint

[](#validation-constraint)

You can also use the constraint for validation. The `value` has to be set.

```
use Knp\DictionaryBundle\Validator\Constraints\Dictionary;

class User
{
    #[ORM\Column]
    #[Dictionary(name: "my_dictionary")]
    private $civility;
}
```

Advanced usage
--------------

[](#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

[](#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

[](#callable-dictionary)

You can create a callable dictionary:

```
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.

### Iterator based dictionary

[](#iterator-based-dictionary)

You can create a dictionary from an iterator:

```
knp_dictionary:
  dictionaries:
    my_iterator_dictionary:     # your dictionary name
      type: 'iterator'          # your dictionary type
      service: 'app.service.id' # a valid service from your application
```

Iterator based dictionaries are loaded with a lazy strategy. It means that the iterator will not be fetched if you do not use the dictionary.

### Combined dictionary

[](#combined-dictionary)

You can combine multiple dictionaries into a single one:

```
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
```

Now you have 3 dictionaries, `payment_mode` and `extra_payment_mode` contain their own values but `combined_payment_mode` contains all the values of the previous ones.

### Extended dictionary

[](#extended-dictionary)

You can create an extended dictionary:

```
knp_dictionary:
  dictionaries:
    europe:
      type: 'key_value'
      content:
        fr: France
        de: Germany

    world:
      type: 'key_value'
      extends: europe
      content:
        us: USA
        ca: Canada
```

The dictionary `world` will now contain its own values in addition to the `europe` values.

**Note**: You must define the initial dictionary **BEFORE** the extended one.

Transformers
------------

[](#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

[](#add-your-own-transformers)

Create your class that implements [TransformerInterface](src/Knp/DictionaryBundle/Dictionary/ValueTransformer/TransformerInterface.php). Load your transformer and tag it as `knp_dictionary.value_transformer`.

```
services:
  App\My\Transformer:
    tags:
      - knp_dictionary.value_transformer
```

Use your dictionary in twig
---------------------------

[](#use-your-dictionary-in-twig)

You can also use your dictionary in your Twig templates via calling `dictionary` function (or filter).

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

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

```
{{ 'my_key'|dictionary('dictionary_name') }}
```

Faker provider
--------------

[](#faker-provider)

The KnpDictionaryBundle comes with a [faker provider](https://github.com/FakerPHP/Faker) that can be used to provide a random entry from a dictionary.

### Alice

[](#alice)

To register the provider in [nelmio/alice](https://github.com/nelmio/alice), you can follow the [official documentation](https://github.com/nelmio/alice/blob/master/doc/customizing-data-generation.md#add-a-custom-faker-provider-class)

```
App\Entity\User:
  john_doe:
    firstname: John
    latnale: Doe
    city:
```

Create your own dictionary implementation
-----------------------------------------

[](#create-your-own-dictionary-implementation)

### Dictionary

[](#dictionary)

Your dictionary implementation must implements the interface [Dictionary](src/Knp/DictionaryBundle/Dictionary.php).

It is automaticaly registered with the `autoconfigure: true` DIC feature.

Else you can register it by your self:

```
services:
  App\Dictionary\MyCustomDictionary:
    tags:
      - knp_dictionary.dictionary
```

### Dictionary Factory

[](#dictionary-factory)

You must create a dictionary factory that will be responsible to instanciate your dictionary.

It is automaticaly registered with the `autoconfigure: true` DIC feature.

Else you can register it by your self:

```
services:
  App\Dictionary\Factory\MyCustomFactory:
    tags:
      - knp_dictionary.factory
```

Tests
-----

[](#tests)

### phpspec

[](#phpspec)

```
composer install
vendor/bin/phpspec run
```

### php-cs-fixer

[](#php-cs-fixer)

```
composer install
vendor/bin/php-cs-fixer fix
```

### phpstan

[](#phpstan)

First [install phive](https://github.com/phar-io/phive#getting-phive).

Then...

```
phive install
tools/phpstan process
```

### rector (*optional*)

[](#rector-optional)

```
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/
```

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance74

Regular maintenance activity

Popularity15

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity93

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~119 days

Recently: every ~180 days

Total

35

Last Release

142d ago

Major Versions

v1.5.2 → v2.02018-01-31

v2.3.0 → v3.0.02019-09-30

v3.2.x-dev → v4.02023-05-12

4.0.9 → 5.0.02025-12-27

PHP version history (8 changes)v1.2.0PHP &gt;=5.3

v2.0PHP &gt;=7.0

v2.3.0PHP &gt;=7.1

v3.1.0PHP &gt;=7.2

v4.0PHP &gt;=8.0

4.0.1PHP &gt;=8.1

4.0.7PHP ^8.3

5.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/21b39551f92ed4143772c622f9e571589c5a72c96ab3c53fe67489ce0d83e806?d=identicon)[tacman1123](/maintainers/tacman1123)

---

Top Contributors

[![PedroTroller](https://avatars.githubusercontent.com/u/1766827?v=4)](https://github.com/PedroTroller "PedroTroller (90 commits)")[![tacman](https://avatars.githubusercontent.com/u/619585?v=4)](https://github.com/tacman "tacman (44 commits)")[![Shivoham](https://avatars.githubusercontent.com/u/1434539?v=4)](https://github.com/Shivoham "Shivoham (27 commits)")[![akovalyov](https://avatars.githubusercontent.com/u/2339101?v=4)](https://github.com/akovalyov "akovalyov (10 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (4 commits)")[![eveyonline](https://avatars.githubusercontent.com/u/833006?v=4)](https://github.com/eveyonline "eveyonline (3 commits)")[![jvasseur](https://avatars.githubusercontent.com/u/849295?v=4)](https://github.com/jvasseur "jvasseur (2 commits)")[![NicolasNSSM](https://avatars.githubusercontent.com/u/2535764?v=4)](https://github.com/NicolasNSSM "NicolasNSSM (1 commits)")[![OwlyCode](https://avatars.githubusercontent.com/u/1631270?v=4)](https://github.com/OwlyCode "OwlyCode (1 commits)")[![Djeg](https://avatars.githubusercontent.com/u/1638230?v=4)](https://github.com/Djeg "Djeg (1 commits)")[![Pierstoval](https://avatars.githubusercontent.com/u/3369266?v=4)](https://github.com/Pierstoval "Pierstoval (1 commits)")[![sanpii](https://avatars.githubusercontent.com/u/113045?v=4)](https://github.com/sanpii "sanpii (1 commits)")[![szappacosta](https://avatars.githubusercontent.com/u/290092?v=4)](https://github.com/szappacosta "szappacosta (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![flug](https://avatars.githubusercontent.com/u/1810304?v=4)](https://github.com/flug "flug (1 commits)")[![janhohner](https://avatars.githubusercontent.com/u/649895?v=4)](https://github.com/janhohner "janhohner (1 commits)")

---

Tags

dictionary

###  Code Quality

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tacman-dictionary-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/tacman-dictionary-bundle/health.svg)](https://phpackages.com/packages/tacman-dictionary-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k16.7M310](/packages/easycorp-easyadmin-bundle)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[netgen/layouts-core

Netgen Layouts enables you to build and manage complex web pages in a simpler way and with less coding. This is the core of Netgen Layouts, its heart and soul.

3689.4k10](/packages/netgen-layouts-core)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
