Skip to content

Commit 317470c

Browse files
committed
DRY code
1 parent 80212af commit 317470c

File tree

2 files changed

+4
-108
lines changed

2 files changed

+4
-108
lines changed

src/Faker/Provider/ar_JO/Text.php

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,9 @@
44

55
class Text extends \Faker\Provider\Text
66
{
7-
8-
/**
9-
* generates text string in arabic
10-
*
11-
* @example ' ولا النوم ولا القرار وكان يقال: لا يجد المريض لذة الطعام والشراب'
12-
* @param integer $maxNbChars
13-
* @param integer $indexSize
14-
* @return string
15-
* @throws \InvalidArgumentException
16-
*/
17-
public function realText($maxNbChars = 200, $indexSize = 2)
7+
protected static function validStart($word)
188
{
19-
if ($maxNbChars < 10) {
20-
throw new \InvalidArgumentException('maxNbChars must be at least 10');
21-
}
22-
23-
if ($indexSize < 1) {
24-
throw new \InvalidArgumentException('indexSize must be at least 1');
25-
}
26-
27-
if ($indexSize > 5) {
28-
throw new \InvalidArgumentException('indexSize must be at most 5');
29-
}
30-
31-
$words = $this->getConsecutiveWords($indexSize);
32-
$result = array();
33-
$resultLength = 0;
34-
// take a random starting point
35-
$next = static::randomKey($words);
36-
while ($resultLength < $maxNbChars && isset($words[$next])) {
37-
// fetch a random word to append
38-
$word = static::randomElement($words[$next]);
39-
40-
// calculate next index
41-
$currentWords = explode(' ', $next);
42-
43-
$currentWords[] = $word;
44-
array_shift($currentWords);
45-
$next = implode(' ', $currentWords);
46-
47-
if ($resultLength == 0 && !preg_match('/^\p{Arabic}/u', $word)) {
48-
continue;
49-
}
50-
// append the element
51-
$result[] = $word;
52-
$resultLength += strlen($word) + 1;
53-
}
54-
55-
// remove the element that caused the text to overflow
56-
array_pop($result);
57-
58-
// build result
59-
$result = implode(' ', $result);
60-
61-
return $result.'.';
9+
return preg_match('/^\p{Arabic}/u', $word);
6210
}
6311

6412
/**

src/Faker/Provider/ar_SA/Text.php

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,9 @@
44

55
class Text extends \Faker\Provider\Text
66
{
7-
8-
/**
9-
* generates text string in arabic
10-
*
11-
* @example ' ولا النوم ولا القرار وكان يقال: لا يجد المريض لذة الطعام والشراب'
12-
* @param integer $maxNbChars
13-
* @param integer $indexSize
14-
* @return string
15-
* @throws \InvalidArgumentException
16-
*/
17-
public function realText($maxNbChars = 200, $indexSize = 2)
7+
protected static function validStart($word)
188
{
19-
if ($maxNbChars < 10) {
20-
throw new \InvalidArgumentException('maxNbChars must be at least 10');
21-
}
22-
23-
if ($indexSize < 1) {
24-
throw new \InvalidArgumentException('indexSize must be at least 1');
25-
}
26-
27-
if ($indexSize > 5) {
28-
throw new \InvalidArgumentException('indexSize must be at most 5');
29-
}
30-
31-
$words = $this->getConsecutiveWords($indexSize);
32-
$result = array();
33-
$resultLength = 0;
34-
// take a random starting point
35-
$next = static::randomKey($words);
36-
while ($resultLength < $maxNbChars && isset($words[$next])) {
37-
// fetch a random word to append
38-
$word = static::randomElement($words[$next]);
39-
40-
// calculate next index
41-
$currentWords = explode(' ', $next);
42-
43-
$currentWords[] = $word;
44-
array_shift($currentWords);
45-
$next = implode(' ', $currentWords);
46-
47-
if ($resultLength == 0 && !preg_match('/^\p{Arabic}/u', $word)) {
48-
continue;
49-
}
50-
// append the element
51-
$result[] = $word;
52-
$resultLength += strlen($word) + 1;
53-
}
54-
55-
// remove the element that caused the text to overflow
56-
array_pop($result);
57-
58-
// build result
59-
$result = implode(' ', $result);
60-
61-
return $result.'.';
9+
return preg_match('/^\p{Arabic}/u', $word);
6210
}
6311

6412
/**

0 commit comments

Comments
 (0)