Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #606 from watermanio/master
Browse files Browse the repository at this point in the history
Improve name guesser logic using field length when available
  • Loading branch information
fzaninotto committed Aug 24, 2015
2 parents feeb4e8 + bef3350 commit 0df79fd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
73 changes: 67 additions & 6 deletions src/Faker/Guesser/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public function __construct(\Faker\Generator $generator)
$this->generator = $generator;
}

public function guessFormat($name)
/**
* @param string $name
* @param int|null $size Length of field, if known
* @return callable
*/
public function guessFormat($name, $size = null)
{
$name = Base::toLower($name);
$generator = $this->generator;
Expand All @@ -27,13 +32,11 @@ public function guessFormat($name)
return $generator->dateTime;
};
}
switch ($name) {
case 'first_name':
switch (str_replace('_', '', $name)) {
case 'firstname':
return function () use ($generator) {
return $generator->firstName;
};
case 'last_name':
case 'lastname':
return function () use ($generator) {
return $generator->lastName;
Expand All @@ -44,12 +47,14 @@ public function guessFormat($name)
return $generator->userName;
};
case 'email':
case 'emailaddress':
return function () use ($generator) {
return $generator->email;
};
case 'phone_number':
case 'phonenumber':
case 'phone':
case 'telephone':
case 'telnumber':
return function () use ($generator) {
return $generator->phoneNumber;
};
Expand All @@ -58,6 +63,7 @@ public function guessFormat($name)
return $generator->address;
};
case 'city':
case 'town':
return function () use ($generator) {
return $generator->city;
};
Expand All @@ -71,19 +77,74 @@ public function guessFormat($name)
return $generator->postcode;
};
case 'state':
return function () use ($generator) {
return $generator->state;
};
case 'county':
if ($this->generator->locale == 'en_US') {
return function () use ($generator) {
return sprintf('%s County', $generator->city);
};
}

return function () use ($generator) {
return $generator->state;
};
case 'country':
switch ($size) {
case 2:
return function () use ($generator) {
return $generator->countryCode;
};
case 3:
return function () use ($generator) {
return $generator->countryISOAlpha3;
};
case 5:
case 6:
return function () use ($generator) {
return $generator->locale;
};
default:
return function () use ($generator) {
return $generator->country;
};
}
break;
case 'locale':
return function () use ($generator) {
return $generator->locale;
};
case 'currency':
case 'currencycode':
return function () use ($generator) {
return $generator->country;
return $generator->currencyCode;
};
case 'url':
case 'website':
return function () use ($generator) {
return $generator->url;
};
case 'company':
case 'companyname':
case 'employer':
return function () use ($generator) {
return $generator->company;
};
case 'title':
if ($size !== null && $size <= 10) {
return function () use ($generator) {
return $generator->title;
};
}

return function () use ($generator) {
return $generator->sentence;
};
case 'body':
case 'summary':
case 'article':
case 'description':
return function () use ($generator) {
return $generator->text;
};
Expand Down
3 changes: 2 additions & 1 deletion src/Faker/ORM/Doctrine/EntityPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function guessColumnFormatters(\Faker\Generator $generator)
continue;
}

if ($formatter = $nameGuesser->guessFormat($fieldName)) {
$size = isset($this->class->fieldMappings[$fieldName]['length']) ? $this->class->fieldMappings[$fieldName]['length'] : null;
if ($formatter = $nameGuesser->guessFormat($fieldName, $size)) {
$formatters[$fieldName] = $formatter;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/ORM/Propel/EntityPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function guessColumnFormatters(\Faker\Generator $generator)
if ($columnMap->isPrimaryKey()) {
continue;
}
if ($formatter = $nameGuesser->guessFormat($columnMap->getPhpName())) {
if ($formatter = $nameGuesser->guessFormat($columnMap->getPhpName(), $columnMap->getSize())) {
$formatters[$columnMap->getPhpName()] = $formatter;
continue;
}
Expand Down

0 comments on commit 0df79fd

Please sign in to comment.