Skip to content

Commit

Permalink
add locale support to faker placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Oct 31, 2012
1 parent 14e394d commit 19d6200
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ Nelmio\Entity\User:
As you see in the last line, you can also pass arguments to those just as if
you were calling a function.

#### Localized Fake Data ####

Faker can create localized data for adresses, phone numbers and so on. You can
set the default locale to use by passing a `locale` value in the `$options`
array of Fixtures::load.

Additionally, you can mix locales by adding a locale prefix to the faker key,
i.e. `<fr_FR:phoneNumber>` or `<de_DE:firstName>`.

### Optional Data ###

Some fields do not have to be filled-in, like the `favoriteNumber` in this
Expand Down
64 changes: 57 additions & 7 deletions src/Nelmio/Alice/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,35 @@ class Base implements LoaderInterface
{
protected $references = array();

/**
* @var \Faker\Generator[]
*/
private $generators;

/**
* Default locale to use with faker
*
* @var string
*/
private $defaultLocale;

/**
* Custom faker providers to use with faker generator
*
* @var array
*/
private $providers;

/**
* @param string $locale default locale to use with faker if none is
* specified in the expression
* @param array $providers custom faker providers in addition to the default
* ones from faker
*/
public function __construct($locale = 'en_US', array $providers = array())
{
$this->generator = \Faker\Factory::create($locale);
foreach ($providers as $provider) {
$this->generator->addProvider($provider);
}
$this->defaultLocale = $locale;
$this->providers = $providers;
}

/**
Expand Down Expand Up @@ -112,7 +135,34 @@ public function fake($formatter, $arg = null, $arg2 = null, $arg3 = null)
$args = func_get_args();
array_shift($args);

return $this->generator->format($formatter, $args);
$locale = null;
if (strpos($formatter, ':')) {
list($locale, $formatter) = explode(':', $formatter);
}
return $this->getGenerator($locale)->format($formatter, $args);
}


/**
* Get the generator for this locale
*
* @param string $locale the requested locale, defaults to constructor injected default
*
* @return \Faker\Generator the generator for the requested locale
*/
protected function getGenerator($locale = null)
{
if (is_null($locale)) {
$locale = $this->defaultLocale;
}
if (! isset($this->generators[$locale])) {
$generator = \Faker\Factory::create($locale);
foreach ($this->providers as $provider) {
$generator->addProvider($provider);
}
$this->generators[$locale] = $generator;
}
return $this->generators[$locale];
}

private function createObject($class, $name, $data)
Expand Down Expand Up @@ -207,11 +257,11 @@ private function process($data, array $variables)
};

// format placeholders without preg_replace if there is only one to avoid __toString() being called
if (preg_match('#^<(?<name>[a-z0-9_]+?)(?:\((?<args>.+?)\))?>$#i', $data, $matches)) {
if (preg_match('#^<(?<name>([a-z]+(_[a-z]+)?:)?[a-z0-9_]+?)(?:\((?<args>.+?)\))?>$#i', $data, $matches)) {
$data = $replacePlaceholder($matches);
} else {
// format placeholders inline
$data = preg_replace_callback('#<(?<name>[a-z0-9_]+?)(?:\((?<args>.+?)\))?>#i', function ($matches) use ($replacePlaceholder) {
$data = preg_replace_callback('#<(?<name>([a-z_]+:)?[a-z0-9_]+?)(?:\((?<args>.+?)\))?>#i', function ($matches) use ($replacePlaceholder) {
return $replacePlaceholder($matches);
}, $data);
}
Expand Down

0 comments on commit 19d6200

Please sign in to comment.