diff --git a/README.md b/README.md index 8aece8c9..e2f3df04 100644 --- a/README.md +++ b/README.md @@ -131,3 +131,23 @@ But you can also access directly to a value by using the same function (or filte ```twig {{ 'my_key'|dictionary('dictionary_name') }} ``` + +## Faker provider + +The KnpDictionaryBundle comes with a [faker provider](https://github.com/fzaninotto/Faker) that can be used to provide a random entry from a dictionary. + +### 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) + +or ... + +if you use the awesome [knplabs/rad-fixtures-load](https://github.com/knplabs/rad-fixtures-load) library, the dictionary provider will be automaticaly loaded for you :) + +```yaml +App\Entity\User: + john_doe: + firstname: John + latnale: Doe + city: <dictionary('cities')> +``` diff --git a/composer.json b/composer.json index 3d3f9884..b8a30f18 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ }, "require-dev": { "phpspec/phpspec": "~2.0", - "bossa/phpspec2-expect": "~1.0" + "bossa/phpspec2-expect": "~1.0", + "fzaninotto/faker": "^1.5" }, "config": { "bin-dir": "bin" @@ -39,5 +40,8 @@ "branch-alias": { "dev-master": "1.3.x-dev" } + }, + "suggest": { + "knplabs/rad-fixtures-load": "Allows to autoregister the dictionary provider as an alice provider" } } diff --git a/src/Knp/DictionaryBundle/DataFixtures/DictionaryProvider.php b/src/Knp/DictionaryBundle/DataFixtures/DictionaryProvider.php new file mode 100644 index 00000000..baf298df --- /dev/null +++ b/src/Knp/DictionaryBundle/DataFixtures/DictionaryProvider.php @@ -0,0 +1,35 @@ +<?php + +namespace Knp\DictionaryBundle\DataFixtures; + +use Faker\Provider\Base as BaseProvider; +use Knp\DictionaryBundle\Dictionary\DictionaryRegistry; + +class DictionaryProvider extends BaseProvider +{ + /** + * @var DictionaryRegistry + */ + private $registry; + + /** + * @param DictionaryRegistry $registry + */ + public function __construct(DictionaryRegistry $registry) + { + $this->registry = $registry; + } + + /** + * @param string $name + * + * @return mixed + */ + public function dictionary($name) + { + $dictionary = $this->registry->get($name); + + return self::randomElement($dictionary->getKeys()); + } +} + diff --git a/src/Knp/DictionaryBundle/Dictionary/Dictionary.php b/src/Knp/DictionaryBundle/Dictionary/Dictionary.php index 24ee8b6f..d2a330c5 100644 --- a/src/Knp/DictionaryBundle/Dictionary/Dictionary.php +++ b/src/Knp/DictionaryBundle/Dictionary/Dictionary.php @@ -46,6 +46,14 @@ public function getValues() return $this->values; } + /** + * @return mixed[] + */ + public function getKeys() + { + return array_keys($this->values); + } + /** * {@inheritdoc} */ diff --git a/src/Knp/DictionaryBundle/Resources/config/dictionary.yml b/src/Knp/DictionaryBundle/Resources/config/dictionary.yml index bd2f42c8..1571b313 100644 --- a/src/Knp/DictionaryBundle/Resources/config/dictionary.yml +++ b/src/Knp/DictionaryBundle/Resources/config/dictionary.yml @@ -7,6 +7,7 @@ parameters: knp_dictionary.form.type.dictionary_type.class: Knp\DictionaryBundle\Form\Type\DictionaryType knp_dictionary.validator.constraints.dictionary_validator.class: Knp\DictionaryBundle\Validator\Constraints\DictionaryValidator knp_dictionary.twig.dictionary_extension.class: Knp\DictionaryBundle\Twig\DictionaryExtension + knp_dictionary.data_fixtures.dictionary_provider.class: Knp\DictionaryBundle\DataFixtures\DictionaryProvider services: knp_dictionary.data_collector.dictionary_data_collector: class: %knp_dictionary.data_collector.dictionary_data_collector.class% @@ -49,3 +50,10 @@ services: - @knp_dictionary.registry tags: - { name: twig.extension } + + knp_dictionary.data_fixtures.dictionary_provider: + class: %knp_dictionary.data_fixtures.dictionary_provider.class% + arguments: + - @knp_dictionary.registry + tags: + - { name: knp_rad_fixtures_load.provider }