PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/fr_FR/Company.php

https://github.com/gobb/Faker
PHP | 221 lines | 125 code | 40 blank | 56 comment | 17 complexity | 57e7c14e942032e225f508f1537b8014 MD5 | raw file
  1. <?php
  2. namespace Faker\Provider\fr_FR;
  3. class Company extends \Faker\Provider\Company
  4. {
  5. /**
  6. * @var array French company name formats.
  7. */
  8. protected static $formats = array(
  9. '{{lastName}} {{companySuffix}}',
  10. '{{lastName}} {{lastName}} {{companySuffix}}',
  11. '{{lastName}}',
  12. '{{lastName}}',
  13. );
  14. /**
  15. * @var array French catch phrase formats.
  16. */
  17. protected static $catchPhraseFormats = array(
  18. '{{catchPhraseNoun}} {{catchPhraseVerb}} {{catchPhraseAttribute}}',
  19. );
  20. /**
  21. * @var array French nouns (used by the catch phrase format).
  22. */
  23. protected static $noun = array(
  24. 'la sécurité', 'le plaisir', 'le confort', 'la simplicité', "l'assurance", "l'art", 'le pouvoir', 'le droit',
  25. 'la possibilité', "l'avantage", 'la liberté'
  26. );
  27. /**
  28. * @var array French verbs (used by the catch phrase format).
  29. */
  30. protected static $verb = array(
  31. 'de rouler', "d'avancer", "d'évoluer", 'de changer', "d'innover", 'de louer', "d'atteindre vos buts",
  32. 'de concrétiser vos projets'
  33. );
  34. /**
  35. * @var array End of sentences (used by the catch phrase format).
  36. */
  37. protected static $attribute = array(
  38. 'de manière efficace', 'plus rapidement', 'plus facilement', 'plus simplement', 'en toute tranquilité',
  39. 'avant-tout', 'autrement', 'naturellement', 'à la pointe', 'sans soucis', "à l'état pur",
  40. 'à sa source', 'de manière sûre', 'en toute sécurité'
  41. );
  42. /**
  43. * @var array Company suffixes.
  44. */
  45. protected static $companySuffix = array('SA', 'S.A.', 'SARL', 'S.A.R.L.', 'S.A.S.', 'et Fils');
  46. /**
  47. * Returns a random catch phrase noun.
  48. *
  49. * @return string
  50. */
  51. public function catchPhraseNoun()
  52. {
  53. return static::randomElement(static::$noun);
  54. }
  55. /**
  56. * Returns a random catch phrase attribute.
  57. *
  58. * @return string
  59. */
  60. public function catchPhraseAttribute()
  61. {
  62. return static::randomElement(static::$attribute);
  63. }
  64. /**
  65. * Returns a random catch phrase verb.
  66. *
  67. * @return string
  68. */
  69. public function catchPhraseVerb()
  70. {
  71. return static::randomElement(static::$verb);
  72. }
  73. /**
  74. * Generates a french catch phrase.
  75. *
  76. * @return string
  77. */
  78. public function catchPhrase()
  79. {
  80. do {
  81. $format = static::randomElement(static::$catchPhraseFormats);
  82. $catchPhrase = ucfirst($this->generator->parse($format));
  83. if ($this->isCatchPhraseValid($catchPhrase)) {
  84. break;
  85. }
  86. } while (true);
  87. return $catchPhrase;
  88. }
  89. /**
  90. * Generates a siret number (14 digits) that passes the Luhn check.
  91. * Use $maxSequentialDigits to make sure the digits at position 2 to 5 are not zeros.
  92. * @see http://en.wikipedia.org/wiki/Luhn_algorithm
  93. * @param int $maxSequentialDigits The maximum number of digits for the sequential number (> 0 && <= 4).
  94. * @return string
  95. */
  96. public static function siret($maxSequentialDigits = 2)
  97. {
  98. if ($maxSequentialDigits > 4 || $maxSequentialDigits <= 0) {
  99. $maxSequentialDigits = 2;
  100. }
  101. $controlDigit = mt_rand(0, 9);
  102. $siret = $sum = $controlDigit;
  103. $position = 2;
  104. for ($i = 0; $i < $maxSequentialDigits; $i++) {
  105. $sequentialDigit = mt_rand(0, 9);
  106. $isEven = $position++ % 2 === 0;
  107. $tmp = $isEven ? $sequentialDigit * 2 : $sequentialDigit;
  108. if ($tmp >= 10) $tmp -= 9;
  109. $sum += $tmp;
  110. $siret = $sequentialDigit . $siret;
  111. }
  112. $siret = str_pad($siret, 5, '0', STR_PAD_LEFT);
  113. $position = 6;
  114. for ($i = 0; $i < 7; $i++) {
  115. $digit = mt_rand(0, 9);
  116. $isEven = $position++ % 2 === 0;
  117. $tmp = $isEven ? $digit * 2 : $digit;
  118. if ($tmp >= 10) $tmp -= 9;
  119. $sum += $tmp;
  120. $siret = $digit . $siret;
  121. }
  122. $mod = $sum % 10;
  123. if ($mod === 0) {
  124. $siret = '00' . $siret;
  125. } else {
  126. // Use the odd position to avoid multiplying by two
  127. $siret = '0' . (10 - $mod) . $siret;
  128. }
  129. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{5})/", "$1 $2 $3 $4", $siret);
  130. }
  131. /**
  132. * Generates a siren number (9 digits) that passes the Luhn check.
  133. * @see http://en.wikipedia.org/wiki/Luhn_algorithm
  134. * @return string
  135. */
  136. public static function siren()
  137. {
  138. $siren = '';
  139. $sum = 0;
  140. for ($i = 9; $i > 1; $i--) {
  141. $digit = mt_rand(0, 9);
  142. $isEven = $i % 2 === 0;
  143. $tmp = $isEven ? $digit * 2 : $digit;
  144. if ($tmp >= 10) $tmp -= 9;
  145. $sum += $tmp;
  146. $siren = $digit . $siren;
  147. }
  148. $mod = $sum % 10;
  149. if ($mod === 0) {
  150. $siren = '0' . $siren;
  151. } else {
  152. $siren = (10 - $mod) . $siren;
  153. }
  154. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{3})/", "$1 $2 $3", $siren);
  155. }
  156. /**
  157. * @var array An array containing string which should not appear twice in a catch phrase.
  158. */
  159. protected static $wordsWhichShouldNotAppearTwice = array('sécurité', 'simpl');
  160. /**
  161. * Validates a french catch phrase.
  162. *
  163. * @param string $catchPhrase The catch phrase to validate.
  164. *
  165. * @return boolean (true if valid, false otherwise)
  166. */
  167. protected static function isCatchPhraseValid($catchPhrase)
  168. {
  169. foreach (static::$wordsWhichShouldNotAppearTwice as $word) {
  170. // Fastest way to check if a piece of word does not appear twice.
  171. $beginPos = strpos($catchPhrase, $word);
  172. $endPos = strrpos($catchPhrase, $word);
  173. if ($beginPos !== false && $beginPos != $endPos) {
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. }