-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from xefi/feature/lorem-to-text-extension
✨ text extension
- Loading branch information
Showing
5 changed files
with
259 additions
and
104 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Xefi\Faker\Extensions; | ||
|
||
use Xefi\Faker\Extensions\Traits\HasLocale; | ||
|
||
class TextExtension extends Extension | ||
{ | ||
use HasLocale; | ||
|
||
/** | ||
* Text in format => Paragraphs => Sentences => Words. | ||
* | ||
* @var array|array[] | ||
*/ | ||
protected array $paragraphs = [ | ||
[ | ||
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit.'], | ||
['Nullam', 'eu', 'nunc', 'non', 'mi', 'aliquet', 'varius.'], | ||
['Curabitur', 'et', 'vestibulum', 'nulla.'], | ||
['Donec', 'placerat', 'tempor', 'arcu,', 'in', 'viverra', 'sapien', 'laoreet', 'eu.'], | ||
['Sed', 'vitae', 'ligula', 'eget', 'mauris', 'malesuada', 'pretium', 'in', 'at', 'lorem.'], | ||
['Integer', 'condimentum', 'urna', 'at', 'lacus', 'fermentum,', 'nec', 'sagittis', 'purus', 'venenatis.'], | ||
], | ||
[ | ||
['Nunc', 'at', 'ligula', 'id', 'nisl', 'varius', 'egestas.'], | ||
['Suspendisse', 'eget', 'nulla', 'dapibus,', 'efficitur', 'purus', 'a,', 'congue', 'quam.'], | ||
['Donec', 'sagittis', 'interdum', 'libero', 'non', 'ornare.'], | ||
['Nam', 'non', 'massa', 'lacus.'], | ||
['Etiam', 'fermentum', 'neque', 'ut', 'est', 'porttitor,', 'ut', 'tincidunt', 'risus', 'suscipit.'], | ||
['Nam', 'id', 'nisi', 'eget', 'lorem', 'vehicula', 'eleifend.'], | ||
], | ||
[ | ||
['Quisque', 'accumsan', 'nisl', 'ut', 'quam', 'pretium,', 'eget', 'lacinia', 'arcu', 'lobortis.'], | ||
['Nam', 'dapibus', 'justo', 'nec', 'nibh', 'dapibus,', 'ac', 'varius', 'velit', 'varius.'], | ||
['Nulla', 'facilisi.'], | ||
['Praesent', 'volutpat', 'suscipit', 'nibh,', 'eget', 'congue', 'ante', 'ornare', 'a.'], | ||
['Nam', 'aliquet', 'risus', 'eget', 'leo', 'gravida', 'scelerisque.'], | ||
], | ||
[ | ||
['Aenean', 'accumsan', 'leo', 'at', 'odio', 'vestibulum,', 'non', 'fermentum', 'nisl', 'varius.'], | ||
['Suspendisse', 'in', 'quam', 'sed', 'ligula', 'convallis', 'sodales.'], | ||
['Mauris', 'consequat', 'risus', 'sit', 'amet', 'libero', 'iaculis,', 'quis', 'volutpat', 'eros', 'scelerisque.'], | ||
['Pellentesque', 'habitants', 'morbi', 'tristique', 'senectus', 'et', 'netus', 'et', 'malesuada', 'fames', 'ac', 'turpis', 'egestas.'], | ||
], | ||
[ | ||
['Donec', 'ultricies', 'euismod', 'libero,', 'vel', 'scelerisque', 'enim', 'condimentum', 'ut.'], | ||
['Fusce', 'varius', 'urna', 'ac', 'ipsum', 'ultricies,', 'vel', 'elementum', 'turpis', 'dictum.'], | ||
['Proin', 'nec', 'ante', 'at', 'erat', 'pharetra', 'interdum.'], | ||
['Etiam', 'nec', 'ligula', 'felis.'], | ||
['Curabitur', 'sit', 'amet', 'varius', 'nisi,', 'in', 'sagittis', 'turpis.'], | ||
['Sed', 'eget', 'ex', 'quis', 'risus', 'varius', 'pharetra', 'in', 'a', 'felis.'], | ||
], | ||
]; | ||
|
||
protected array $flattenedWords; | ||
|
||
protected array $flattenedSentences; | ||
|
||
protected array $flattenedParahraphs; | ||
|
||
protected function flattenedWords(): array | ||
{ | ||
if (isset($this->flattenedWords)) { | ||
return $this->flattenedWords; | ||
} | ||
|
||
return array_merge(...$this->flattenedSentences()); | ||
} | ||
|
||
protected function flattenedSentences(): array | ||
{ | ||
if (isset($this->flattenedSentences)) { | ||
return $this->flattenedSentences; | ||
} | ||
|
||
return array_merge(...$this->paragraphs); | ||
} | ||
|
||
public function wordsAsArray(int $wordsCount = 3): array | ||
{ | ||
return $this->pickArrayRandomElements($this->flattenedWords(), $wordsCount); | ||
} | ||
|
||
public function words(int $wordsCount = 3): string | ||
{ | ||
$words = $this->wordsAsArray($wordsCount); | ||
|
||
// Remove any uppercase / comma / dots | ||
return strtolower(preg_replace('/[.,]/', '', implode(' ', $words))); | ||
} | ||
|
||
public function sentencesAsArray(int $sentencesCount = 3): array | ||
{ | ||
return $this->pickArrayRandomElements($this->flattenedSentences(), $sentencesCount); | ||
} | ||
|
||
public function sentences(int $sentencesCount = 3): string | ||
{ | ||
$sentences = $this->sentencesAsArray($sentencesCount); | ||
$sentences = array_map(function ($sentence) { return implode(' ', $sentence); }, $sentences); | ||
|
||
return implode(' ', $sentences); | ||
} | ||
|
||
public function paragraphsAsArray(int $paragraphsCount = 3): array | ||
{ | ||
return $this->pickArrayRandomElements($this->paragraphs, $paragraphsCount); | ||
} | ||
|
||
public function paragraphs(int $paragraphsCount = 3): string | ||
{ | ||
$paragraphs = $this->paragraphsAsArray($paragraphsCount); | ||
$paragraphs = array_map(function ($sentences) { return implode(' ', array_merge(...$sentences)); }, $paragraphs); | ||
|
||
return implode(PHP_EOL, $paragraphs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace Xefi\Faker\Tests\Unit\Extensions; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use ReflectionClass; | ||
|
||
final class TextExtensionTest extends TestCase | ||
{ | ||
protected array $paragraphs = []; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$testExtension = new \Xefi\Faker\Extensions\TextExtension(new \Random\Randomizer()); | ||
$this->paragraphs = (new ReflectionClass($testExtension))->getProperty('paragraphs')->getValue($testExtension); | ||
} | ||
|
||
public function testWordsWithDefaultValue(): void | ||
{ | ||
$sentences = array_merge(...$this->paragraphs); | ||
$words = array_merge(...$sentences); | ||
$wordsWithoutPunctuationAndLowercased = array_map(function (string $word) { return strtolower(preg_replace('/[.,]/', '', $word)); }, $words); | ||
$result = $this->faker->words(); | ||
|
||
$this->assertCount(3, explode(' ', $result)); | ||
foreach (explode(' ', $result) as $word) { | ||
$this->assertContains($word, $wordsWithoutPunctuationAndLowercased); | ||
} | ||
} | ||
|
||
public static function wordsProvider() | ||
{ | ||
return [ | ||
[1], | ||
[2], | ||
[3], | ||
[4], | ||
[5], | ||
[6], | ||
[7], | ||
[8], | ||
[9], | ||
[10], | ||
]; | ||
} | ||
|
||
#[DataProvider('wordsProvider')] | ||
public function testWords(int $count): void | ||
{ | ||
$sentences = array_merge(...$this->paragraphs); | ||
$words = array_merge(...$sentences); | ||
$wordsWithoutPunctuationAndLowercased = array_map(function (string $word) { return strtolower(preg_replace('/[.,]/', '', $word)); }, $words); | ||
$result = $this->faker->unique()->words($count); | ||
|
||
$this->assertCount($count, explode(' ', $result)); | ||
foreach (explode(' ', $result) as $word) { | ||
$this->assertContains($word, $wordsWithoutPunctuationAndLowercased); | ||
} | ||
} | ||
|
||
public function testSentencesWithDefaultValue(): void | ||
{ | ||
$sentences = array_merge(...$this->paragraphs); | ||
$words = array_merge(...$sentences); | ||
$result = $this->faker->sentences(); | ||
|
||
$this->assertCount(3, array_filter(explode('.', $result))); | ||
foreach (explode(' ', $result) as $word) { | ||
$this->assertContains($word, $words); | ||
} | ||
} | ||
|
||
public static function sentencesProvider() | ||
{ | ||
return [ | ||
[1], | ||
[2], | ||
[3], | ||
[4], | ||
[5], | ||
[6], | ||
[7], | ||
[8], | ||
[9], | ||
[10], | ||
]; | ||
} | ||
|
||
#[DataProvider('sentencesProvider')] | ||
public function testSentences(int $count): void | ||
{ | ||
$sentences = array_merge(...$this->paragraphs); | ||
$words = array_merge(...$sentences); | ||
$result = $this->faker->unique()->sentences($count); | ||
|
||
$this->assertCount($count, array_filter(explode('.', $result))); | ||
foreach (explode(' ', $result) as $word) { | ||
$this->assertContains($word, $words); | ||
} | ||
} | ||
|
||
public function testParagraphsWithDefaultValue(): void | ||
{ | ||
$sentences = array_merge(...$this->paragraphs); | ||
$words = array_merge(...$sentences); | ||
$result = $this->faker->paragraphs(); | ||
|
||
$this->assertCount(3, array_filter(explode(PHP_EOL, $result))); | ||
foreach (preg_split('/\s+/', $result) as $word) { | ||
$this->assertContains($word, $words); | ||
} | ||
} | ||
|
||
public static function paragraphsProvider() | ||
{ | ||
return [ | ||
[1], | ||
[2], | ||
[3], | ||
[4], | ||
[5], | ||
]; | ||
} | ||
|
||
#[DataProvider('paragraphsProvider')] | ||
public function testParagraphs(int $count): void | ||
{ | ||
$sentences = array_merge(...$this->paragraphs); | ||
$words = array_merge(...$sentences); | ||
$result = $this->faker->unique()->paragraphs($count); | ||
|
||
$this->assertCount($count, array_filter(explode(PHP_EOL, $result))); | ||
foreach (preg_split('/\s+/', $result) as $word) { | ||
$this->assertContains($word, $words); | ||
} | ||
} | ||
} |