Skip to content

Commit

Permalink
updated text extension
Browse files Browse the repository at this point in the history
  • Loading branch information
johnykvsky committed Dec 23, 2024
1 parent 5ef48f0 commit 7ce8242
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 193 deletions.
236 changes: 120 additions & 116 deletions docs/extensions.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions docs/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ No problem, just try this:
echo $generator->foo('Anna'); // gives 'Anna is awesome!'
echo $generator->boo('School'); // gives 'School is a crap!'
```

## Text

Text extension is a bit different for one reason - it uses external `txt` file as source to large test. By default it's in `resources/en_US.txt` but you can either:

* pass file location to `Text` constructor
* extend `Text` class and use different location in `$defaultText` property
1 change: 1 addition & 0 deletions resources/en_US-copyright.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Copyright belong to The Edgar Allan Poe Museum https://poemuseum.org
33 changes: 33 additions & 0 deletions resources/en_US.txt

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion src/Core/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
use DummyGenerator\Definitions\Extension\Exception\ExtensionOverflowException;
use DummyGenerator\Definitions\Extension\TextExtensionInterface;

abstract class Text implements
class Text implements
TextExtensionInterface,
RandomizerAwareExtensionInterface,
ReplacerAwareExtensionInterface
{
use RandomizerAwareExtensionTrait;
use ReplacerAwareExtensionTrait;

protected string $defaultText = __DIR__.'/../../resources/en_US.txt';

protected string $baseText = '';
/**
* @var non-empty-string
Expand All @@ -36,6 +38,17 @@ abstract class Text implements
protected array $consecutiveWords = [];
protected bool $textStartsWithUppercase = true;

public function __construct(string $baseText = null)
{
if (null !== $baseText && $file = file_get_contents($baseText)) {
$this->baseText = $file;
} else {
$file = file_get_contents($this->defaultText);
$this->baseText = $file !== false ? $file : '';
}
}


/**
* Generate a text string by the Markov chain algorithm.
*
Expand Down
4 changes: 3 additions & 1 deletion src/DefinitionPack/DefinitionPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use DummyGenerator\Core\PhoneNumber;
use DummyGenerator\Core\Randomizer\Randomizer;
use DummyGenerator\Core\Replacer\Replacer;
use DummyGenerator\Core\Text;
use DummyGenerator\Core\Transliterator\Transliterator;
use DummyGenerator\Core\UserAgent;
use DummyGenerator\Core\Version;
Expand Down Expand Up @@ -56,6 +57,7 @@
use DummyGenerator\Definitions\Extension\PaymentExtensionInterface;
use DummyGenerator\Definitions\Extension\PersonExtensionInterface;
use DummyGenerator\Definitions\Extension\PhoneNumberExtensionInterface;
use DummyGenerator\Definitions\Extension\TextExtensionInterface;
use DummyGenerator\Definitions\Extension\UserAgentExtensionInterface;
use DummyGenerator\Definitions\Extension\VersionExtensionInterface;
use DummyGenerator\Definitions\Randomizer\RandomizerInterface;
Expand Down Expand Up @@ -125,7 +127,7 @@ public function __construct()
FileExtensionInterface::class => File::class,
PaymentExtensionInterface::class => Payment::class,
PhoneNumberExtensionInterface::class => PhoneNumber::class,
// TextExtension::class => new Implementation\Text,
TextExtensionInterface::class => Text::class,
UserAgentExtensionInterface::class => UserAgent::class,
VersionExtensionInterface::class => Version::class,
];
Expand Down
3 changes: 2 additions & 1 deletion test/Extension/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DummyGenerator\Container\DefinitionContainer;
use DummyGenerator\Core\Randomizer\Randomizer;
use DummyGenerator\Core\Replacer\Replacer;
use DummyGenerator\Core\Text;
use DummyGenerator\Core\Transliterator\Transliterator;
use DummyGenerator\Definitions\Extension\TextExtensionInterface;
use DummyGenerator\Definitions\Randomizer\RandomizerInterface;
Expand All @@ -28,7 +29,7 @@ public function setUp(): void
$container->add(RandomizerInterface::class, Randomizer::class);
$container->add(TransliteratorInterface::class, Transliterator::class);
$container->add(ReplacerInterface::class, Replacer::class);
$container->add(TextExtensionInterface::class, TextProvider::class);
$container->add(TextExtensionInterface::class, Text::class);
$this->generator = new DummyGenerator($container);
}
public function testRealText(): void
Expand Down
74 changes: 0 additions & 74 deletions test/Fixtures/TextProvider.php

This file was deleted.

0 comments on commit 7ce8242

Please sign in to comment.