From a10417da99d65050abab0bfd067dce6b147cd46e Mon Sep 17 00:00:00 2001
From: Geoffrey Brier <geoffrey.brier@gmail.com>
Date: Thu, 9 Aug 2012 19:01:07 +0200
Subject: [PATCH] Simplication of the catchPhrase formatter

---
 src/Faker/Provider/fr_FR/Company.php      | 96 +++++++++++++++--------
 test/Faker/Provider/fr_FR/CompanyTest.php | 14 ++++
 2 files changed, 79 insertions(+), 31 deletions(-)

diff --git a/src/Faker/Provider/fr_FR/Company.php b/src/Faker/Provider/fr_FR/Company.php
index 305da4e3..d39648c3 100644
--- a/src/Faker/Provider/fr_FR/Company.php
+++ b/src/Faker/Provider/fr_FR/Company.php
@@ -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;
+	}
 }
\ No newline at end of file
diff --git a/test/Faker/Provider/fr_FR/CompanyTest.php b/test/Faker/Provider/fr_FR/CompanyTest.php
index 6f1bef5c..0096b00d 100644
--- a/test/Faker/Provider/fr_FR/CompanyTest.php
+++ b/test/Faker/Provider/fr_FR/CompanyTest.php
@@ -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);
+	}
 }
\ No newline at end of file