PHP code example of knplabs / dictionary-bundle

1. Go to this page and download the library: Download knplabs/dictionary-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

knplabs / dictionary-bundle example snippets


$bundles = array(
    // ...
    Knp\DictionaryBundle\KnpDictionaryBundle::class => ['all' => true],
);

use Knp\DictionaryBundle\Dictionary;
use Symfony\Component\DependencyInjection\Attribute\Target;

final class UserManager
{
    public function __construct(
        #[Target('civility.dictionary')]
        private Dictionary $dictionary,
    ) {}
}

use Knp\DictionaryBundle\Dictionary;

final class UserManager
{
    public function __construct(
        private Dictionary $civilityDictionary,
    ) {}
}

use Knp\DictionaryBundle\Dictionary;

final class UserManager
{
    private Dictionary $dictionary;

    public function __construct(Dictionary\Collection $dictionaries)
    {
        $this->dictionary = $dictionaries['civility'];
    }
}

use Knp\DictionaryBundle\Form\Type\DictionaryType;

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

use Knp\DictionaryBundle\Validator\Constraints\Dictionary;

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



declare(strict_types=1);

namespace App\Dictionary;

use Knp\DictionaryBundle\Dictionary\Simple;
use Knp\DictionaryBundle\Dictionary\Wrapper;

final class MyCustomDictionary extends Wrapper
{
    public function __construct()
    {
        parent::__construct(new Simple('my_custom', [
            'foo' => 'Foo',
            'bar' => 'Bar',
        ]));
    }
}
bash
composer install
vendor/bin/php-cs-fixer fix