1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-22 08:19:52 +01:00

Simplication of the catchPhrase formatter

This commit is contained in:
Geoffrey Brier 2012-08-09 19:01:07 +02:00
parent 2fa2cf821e
commit a10417da99
2 changed files with 79 additions and 31 deletions

View File

@ -18,18 +18,15 @@ class Company extends \Faker\Provider\Company
* @var array French catch phrase formats.
*/
protected static $catchPhraseFormats = array(
'{{cpNoun}} {{cpVerb}} {{cpAttribute}}',
'{{cpNoun}} et {{cpNoun}} {{cpAttribute}}',
'{{cpNoun}} et {{cpNoun}} {{cpMultipleAttribute}}',
'{{catchPhraseNoun}} {{catchPhraseVerb}} {{catchPhraseAttribute}}',
);
/**
* @var array French nouns (used by the catch phrase format).
*/
protected static $noun = array(
'la sécurité', 'le plaisir', "l'efficacité", 'le confort', 'la simplicité', 'la qualité', "l'assurance",
'la santé', 'la technologie', "l'art", 'le pouvoir', 'le prestige', "l'honneur", 'la chance', 'la faculté',
'la possibilité', 'le droit', "l'avantage", 'la liberté'
'la sécurité', 'le plaisir', 'le confort', 'la simplicité', "l'assurance", "l'art", 'le pouvoir', 'le droit',
'la possibilité', "l'avantage", 'la liberté'
);
/**
@ -44,16 +41,9 @@ class Company extends \Faker\Provider\Company
* @var array End of sentences (used by the catch phrase format).
*/
protected static $attribute = array(
'moins', 'de manière efficace', 'plus rapidement', 'plus facilement', 'plus simplement', 'en toute tranquilité',
'avant-tout', "d'abord", 'autrement', 'naturellement', 'à la pointe', 'sans soucis', "à l'état pur",
'à sa source', 'sûre', 'pour la vie'
);
/**
* @var array End of sentences when using multiple nouns (used by the catch phrase format).
*/
protected static $multipleAttribute = array('sont nos priorités', 'sont nos points forts', 'font notre force',
'qui assurent', 'sont nos passions', 'supérieurs'
'de manière efficace', 'plus rapidement', 'plus facilement', 'plus simplement', 'en toute tranquilité',
'avant-tout', 'autrement', 'naturellement', 'à la pointe', 'sans soucis', "à l'état pur",
'à sa source', 'de manière sûre', 'en toute sécurité'
);
/**
@ -66,22 +56,32 @@ class Company extends \Faker\Provider\Company
*/
protected static $sirenFormat = "### ### ###";
public function cpNoun()
/**
* Returns a random catch phrase noun.
*
* @return string
*/
public function catchPhraseNoun()
{
return static::randomElement(static::$noun);
}
public function cpAttribute()
/**
* Returns a random catch phrase attribute.
*
* @return string
*/
public function catchPhraseAttribute()
{
return static::randomElement(static::$attribute);
}
public function cpMultipleAttribute()
{
return static::randomElement(static::$multipleAttribute);
}
public function cpVerb()
/**
* Returns a random catch phrase verb.
*
* @return string
*/
public function catchPhraseVerb()
{
return static::randomElement(static::$verb);
}
@ -93,9 +93,16 @@ class Company extends \Faker\Provider\Company
*/
public function catchPhrase()
{
$format = static::randomElement(static::$catchPhraseFormats);
do {
$format = static::randomElement(static::$catchPhraseFormats);
$catchPhrase = ucfirst($this->generator->parse($format));
return ucfirst($this->generator->parse($format));
if (static::isCatchPhraseValid($catchPhrase)) {
break;
}
} while (true);
return $catchPhrase;
}
/**
@ -119,13 +126,40 @@ class Company extends \Faker\Provider\Company
return static::numerify(static::siren() . ' ' . $sequentialNumber . '#');
}
/**
* Generates a siren number (9 digits).
*
* @return string
*/
/**
* Generates a siren number (9 digits).
*
* @return string
*/
public static function siren()
{
return static::numerify(static::$sirenFormat);
}
/**
* @var array An array containing string which should not appear twice in a catch phrase.
*/
private static $wordsWhichShouldNotAppearTwice = array('sécurité', 'simpl');
/**
* Validates a french catch phrase.
*
* @param string $catchPhrase The catch phrase to validate.
*
* @return boolean (true if valid, false otherwise)
*/
public static function isCatchPhraseValid($catchPhrase)
{
foreach (static::$wordsWhichShouldNotAppearTwice as $word) {
// Fastest way to check if a piece of word does not appear twice.
$beginPos = strpos($catchPhrase, $word);
$endPos = strrpos($catchPhrase, $word);
if ($beginPos !== false && $beginPos != $endPos) {
return false;
}
}
return true;
}
}

View File

@ -32,4 +32,18 @@ class CompanyTest extends \PHPUnit_Framework_TestCase
$this->assertRegExp("/[\d]{3} [\d]{3} [\d]{3} 0[\d]{4}/", $siret3);
$this->assertRegExp("/[\d]{3} [\d]{3} [\d]{3} [\d]{5}/", $siret4);
}
public function testCatchPhraseValidationReturnsFalse()
{
$isCatchPhraseValid = Company::isCatchPhraseValid('La sécurité de rouler en toute sécurité');
$this->assertFalse($isCatchPhraseValid);
}
public function testCatchPhraseValidationReturnsTrue()
{
$isCatchPhraseValid = Company::isCatchPhraseValid('La sécurité de rouler en toute simplicité');
$this->assertTrue($isCatchPhraseValid);
}
}