diff --git a/src/Faker/Provider/Text.php b/src/Faker/Provider/Text.php index 64d09a402d..675ad85169 100644 --- a/src/Faker/Provider/Text.php +++ b/src/Faker/Provider/Text.php @@ -5,6 +5,8 @@ abstract class Text extends \Faker\Provider\Base { protected static $baseText = ''; + protected static $separator = ' '; + protected static $separatorLen = 1; protected $explodedText = null; protected $consecutiveWords = array(); @@ -37,6 +39,7 @@ public function realText($maxNbChars = 200, $indexSize = 2) throw new \InvalidArgumentException('indexSize must be at most 5'); } + $words = $this->getConsecutiveWords($indexSize); $result = array(); $resultLength = 0; @@ -47,28 +50,28 @@ public function realText($maxNbChars = 200, $indexSize = 2) $word = static::randomElement($words[$next]); // calculate next index - $currentWords = explode(' ', $next); + $currentWords = static::explode($next); $currentWords[] = $word; array_shift($currentWords); - $next = implode(' ', $currentWords); + $next = static::implode($currentWords); // ensure text starts with an uppercase letter - if ($resultLength == 0 && !preg_match('/^\p{Lu}/u', $word)) { + if ($resultLength == 0 && !static::validStart($word)) { continue; } // append the element $result[] = $word; - $resultLength += strlen($word) + 1; + $resultLength += static::strlen($word) + static::$separatorLen; } // remove the element that caused the text to overflow array_pop($result); // build result - $result = implode(' ', $result); + $result = static::implode($result); - return $result.'.'; + return static::appendEnd($result); } protected function getConsecutiveWords($indexSize) @@ -82,7 +85,7 @@ protected function getConsecutiveWords($indexSize) } for ($i = 0, $count = count($parts); $i < $count; $i++) { - $stringIndex = implode(' ', $index); + $stringIndex = static::implode($index); if (!isset($words[$stringIndex])) { $words[$stringIndex] = array(); } @@ -101,9 +104,34 @@ protected function getConsecutiveWords($indexSize) protected function getExplodedText() { if ($this->explodedText === null) { - $this->explodedText = explode(' ', preg_replace('/\s+/u', ' ', static::$baseText)); + $this->explodedText = static::explode(preg_replace('/\s+/u', ' ', static::$baseText)); } return $this->explodedText; } + + protected static function explode($text) + { + return explode(static::$separator, $text); + } + + protected static function implode($words) + { + return implode(static::$separator, $words); + } + + protected static function strlen($text) + { + return function_exists('mb_strlen') ? mb_strlen($text, 'UTF-8') : strlen($text); + } + + protected static function validStart($word) + { + return preg_match('/^\p{Lu}/u', $word); + } + + protected static function appendEnd($text) + { + return $text.'.'; + } } diff --git a/src/Faker/Provider/zh_TW/Text.php b/src/Faker/Provider/zh_TW/Text.php new file mode 100644 index 0000000000..b35903f62b --- /dev/null +++ b/src/Faker/Provider/zh_TW/Text.php @@ -0,0 +1,124 @@ +