diff --git a/src/Extensions/LoremExtension.php b/src/Extensions/LoremExtension.php deleted file mode 100644 index 9f41d54..0000000 --- a/src/Extensions/LoremExtension.php +++ /dev/null @@ -1,48 +0,0 @@ -pickArrayRandomElement($this->latinWords); - } - - public function words(int $words = 3) - { - return $this->pickArrayRandomElements($this->latinWords, $words); - } - - public function sentence(int $words = 6) - { - return implode(' ', $this->words($words)); - } -} diff --git a/src/Extensions/TextExtension.php b/src/Extensions/TextExtension.php new file mode 100644 index 0000000..118bb9e --- /dev/null +++ b/src/Extensions/TextExtension.php @@ -0,0 +1,118 @@ + 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); + } +} diff --git a/src/FakerServiceProvider.php b/src/FakerServiceProvider.php index b9ab0d0..c95f7e6 100644 --- a/src/FakerServiceProvider.php +++ b/src/FakerServiceProvider.php @@ -7,10 +7,10 @@ use Xefi\Faker\Extensions\FinancialExtension; use Xefi\Faker\Extensions\HashExtension; use Xefi\Faker\Extensions\InternetExtension; -use Xefi\Faker\Extensions\LoremExtension; use Xefi\Faker\Extensions\NumbersExtension; use Xefi\Faker\Extensions\PersonExtension; use Xefi\Faker\Extensions\StringsExtension; +use Xefi\Faker\Extensions\TextExtension; use Xefi\Faker\Providers\Provider; class FakerServiceProvider extends Provider @@ -18,7 +18,7 @@ class FakerServiceProvider extends Provider public function boot(): void { $this->extensions([ - LoremExtension::class, + TextExtension::class, NumbersExtension::class, StringsExtension::class, HashExtension::class, diff --git a/tests/Unit/Extensions/LoremExtensionTest.php b/tests/Unit/Extensions/LoremExtensionTest.php deleted file mode 100644 index 2a627cc..0000000 --- a/tests/Unit/Extensions/LoremExtensionTest.php +++ /dev/null @@ -1,54 +0,0 @@ -latinWords = (new ReflectionClass($loremExtension))->getProperty('latinWords')->getValue($loremExtension); - } - - public function testWord(): void - { - $results = []; - for ($i = 0; $i < count($this->latinWords); $i++) { - $results[] = $this->faker->unique()->word(); - } - - $this->assertEqualsCanonicalizing( - $this->latinWords, - $results - ); - } - - public function testWords(): void - { - $results = $this->faker->words(40); - - $this->assertCount(40, $results); - - foreach ($results as $result) { - $this->assertContains($result, $this->latinWords); - } - } - - public function testSentence(): void - { - $result = $this->faker->sentence(40); - - $words = explode(' ', $result); - $this->assertCount(40, $words); - - foreach ($words as $word) { - $this->assertContains($word, $this->latinWords); - } - } -} diff --git a/tests/Unit/Extensions/TextExtensionTest.php b/tests/Unit/Extensions/TextExtensionTest.php new file mode 100644 index 0000000..1430bcc --- /dev/null +++ b/tests/Unit/Extensions/TextExtensionTest.php @@ -0,0 +1,139 @@ +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); + } + } +}