Skip to content

Commit

Permalink
Merge pull request #30 from xefi/feature/lorem-to-text-extension
Browse files Browse the repository at this point in the history
✨ text extension
  • Loading branch information
GautierDele authored Oct 29, 2024
2 parents 6a15c6e + fb1d751 commit b44eee1
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 104 deletions.
48 changes: 0 additions & 48 deletions src/Extensions/LoremExtension.php

This file was deleted.

118 changes: 118 additions & 0 deletions src/Extensions/TextExtension.php
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);
}
}
4 changes: 2 additions & 2 deletions src/FakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
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
{
public function boot(): void
{
$this->extensions([
LoremExtension::class,
TextExtension::class,
NumbersExtension::class,
StringsExtension::class,
HashExtension::class,
Expand Down
54 changes: 0 additions & 54 deletions tests/Unit/Extensions/LoremExtensionTest.php

This file was deleted.

139 changes: 139 additions & 0 deletions tests/Unit/Extensions/TextExtensionTest.php
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);
}
}
}

0 comments on commit b44eee1

Please sign in to comment.