Skip to content

Commit

Permalink
Merge pull request #24 from KnpLabs/feature/dictionary-provider
Browse files Browse the repository at this point in the history
Add dictionary provider that can be used with nelmio/alice
  • Loading branch information
Sylvain Chevalier committed Oct 22, 2015
2 parents 003639d + b479d25 commit 9b71465
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')>
```
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
35 changes: 35 additions & 0 deletions src/Knp/DictionaryBundle/DataFixtures/DictionaryProvider.php
Original file line number Diff line number Diff line change
@@ -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());
}
}

8 changes: 8 additions & 0 deletions src/Knp/DictionaryBundle/Dictionary/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function getValues()
return $this->values;
}

/**
* @return mixed[]
*/
public function getKeys()
{
return array_keys($this->values);
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Knp/DictionaryBundle/Resources/config/dictionary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down Expand Up @@ -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 }

0 comments on commit 9b71465

Please sign in to comment.