Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ text extension #30

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}