Skip to content

Commit

Permalink
Merge pull request #29 from pavelgopanenko/master
Browse files Browse the repository at this point in the history
"unique" and "optional" formatter options
  • Loading branch information
willdurand committed Feb 19, 2014
2 parents 626531e + adde379 commit adead45
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion DependencyInjection/BazingaFakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($params['custom_formatters'] as $column => $formatter) {
$method = $formatter['method'];
$parameters = $formatter['parameters'];
$unique = $formatter['unique'];
$optional = $formatter['optional'];

if (null === $method) {
$formatters[$column] = null;
} else {
$container->setDefinition('faker.entities.' . $i . '.formatters.' . $j, new Definition(
'closure',
array(new Reference('faker.generator'), $method, $parameters)
array(new Reference('faker.generator'), $method, $parameters, $unique, $optional)
))->setFactoryService(
'faker.formatter_factory'
)->setFactoryMethod(
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function getConfigTreeBuilder()
->arrayNode('parameters')
->prototype('variable')->end()
->end()
->booleanNode('unique')->defaultFalse()->end()
->scalarNode('optional')->defaultNull()->end()
->end()
->end()->end()
->arrayNode('custom_modifiers')
Expand Down
12 changes: 11 additions & 1 deletion Factory/FormatterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@

namespace Bazinga\Bundle\FakerBundle\Factory;

use Faker\Generator;

/**
* @author William Durand <[email protected]>
*/
class FormatterFactory
{
public static function createClosure($generator, $method, array $parameters = array())
public static function createClosure($generator, $method, array $parameters = array(), $unique = false, $optional = null)
{
if ($unique && $generator instanceof Generator) {
$generator = $generator->unique();
}

if (null !== $optional && $generator instanceof Generator) {
$generator = $generator->optional((double) $optional);
}

if (0 === count($parameters)) {
return function () use ($generator, $method) { return $generator->$method(); };
}
Expand Down

0 comments on commit adead45

Please sign in to comment.