diff --git a/DependencyInjection/BazingaFakerExtension.php b/DependencyInjection/BazingaFakerExtension.php index f7f5544..aae8217 100644 --- a/DependencyInjection/BazingaFakerExtension.php +++ b/DependencyInjection/BazingaFakerExtension.php @@ -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( diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 714ef37..8e2a48d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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') diff --git a/Factory/FormatterFactory.php b/Factory/FormatterFactory.php index 8f0cabf..b72618c 100644 --- a/Factory/FormatterFactory.php +++ b/Factory/FormatterFactory.php @@ -10,13 +10,23 @@ namespace Bazinga\Bundle\FakerBundle\Factory; +use Faker\Generator; + /** * @author William Durand */ 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(); }; }