vendor/nelmio/alice/src/Bridge/Symfony/DependencyInjection/Configuration.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Alice package.
  4.  *
  5.  * (c) Nelmio <[email protected]>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Nelmio\Alice\Bridge\Symfony\DependencyInjection;
  12. use Nelmio\Alice\Throwable\Exception\InvalidArgumentExceptionFactory;
  13. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  14. use Symfony\Component\Config\Definition\ConfigurationInterface;
  15. /**
  16.  * @private
  17.  */
  18. final class Configuration implements ConfigurationInterface
  19. {
  20.     /**
  21.      * @inheritdoc
  22.      */
  23.     public function getConfigTreeBuilder()
  24.     {
  25.         $treeBuilder = new TreeBuilder();
  26.         $rootNode $treeBuilder->root('nelmio_alice');
  27.         $rootNode
  28.             ->children()
  29.                 ->scalarNode('locale')
  30.                     ->defaultValue('en_US')
  31.                     ->info('Default locale for the Faker Generator')
  32.                     ->validate()
  33.                         ->always($this->createStringValidatorClosure('nelmio_alice.locale'))
  34.                     ->end()
  35.                 ->end()
  36.                 ->scalarNode('seed')
  37.                     ->defaultValue(1)
  38.                     ->info('Value used make sure Faker generates data consistently across runs, set to null to disable.')
  39.                     ->validate()
  40.                         ->always(
  41.                             function ($seed) {
  42.                                 if (null === $seed || (is_int($seed) && $seed 0)) {
  43.                                     return $seed;
  44.                                 }
  45.                                 throw InvalidArgumentExceptionFactory::createForInvalidSeedConfigurationValue($seed);
  46.                             }
  47.                         )
  48.                     ->end()
  49.                 ->end()
  50.                 ->arrayNode('functions_blacklist')
  51.                     ->prototype('scalar')
  52.                     ->end()
  53.                     ->defaultValue(['current'])
  54.                     ->info(
  55.                         'Some PHP native functions may conflict with Faker formatters. By default, PHP native '
  56.                         .'functions are used over Faker formatters. If you want to change that, simply blacklist the '
  57.                         .'PHP function.'
  58.                     )
  59.                     ->validate()
  60.                         ->always(
  61.                             function (array $value) {
  62.                                 foreach ($value as $item) {
  63.                                     if (false === is_string($item)) {
  64.                                         throw InvalidArgumentExceptionFactory::createForExpectedConfigurationArrayOfStringValue($item);
  65.                                     }
  66.                                 }
  67.                                 return $value;
  68.                             }
  69.                         )
  70.                     ->end()
  71.                 ->end()
  72.                 ->integerNode('loading_limit')
  73.                     ->defaultValue(5)
  74.                     ->info(
  75.                         'Alice may do some recursion to resolve certain values. This parameter defines a limit which '
  76.                         .'will stop the resolution once reached.'
  77.                     )
  78.                     ->validate()
  79.                         ->always($this->createPositiveIntegerValidatorClosure())
  80.                     ->end()
  81.                 ->end()
  82.                 ->integerNode('max_unique_values_retry')
  83.                     ->defaultValue(150)
  84.                     ->info('Maximum number of time Alice can try to generate a unique value before stopping and failing.')
  85.                     ->validate()
  86.                         ->always($this->createPositiveIntegerValidatorClosure())
  87.                     ->end()
  88.                 ->end()
  89.             ->end()
  90.         ;
  91.         return $treeBuilder;
  92.     }
  93.     private function createStringValidatorClosure(): \Closure
  94.     {
  95.         return function ($value) {
  96.             if (is_string($value)) {
  97.                 return $value;
  98.             }
  99.             throw InvalidArgumentExceptionFactory::createForExpectedConfigurationStringValue($value);
  100.         };
  101.     }
  102.     private function createPositiveIntegerValidatorClosure(): \Closure
  103.     {
  104.         return function ($value) {
  105.             if (is_int($value) && $value) {
  106.                 return $value;
  107.             }
  108.             throw InvalidArgumentExceptionFactory::createForExpectedConfigurationPositiveIntegerValue($value);
  109.         };
  110.     }
  111. }