Skip to content

Commit

Permalink
Rename selectors and small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed Jul 10, 2019
1 parent 44f77e6 commit 18a1562
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 47 deletions.
74 changes: 43 additions & 31 deletions src/Filter/WrapLongWordsWithHTMLFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,15 @@ public function filter(string $string): string
{
// Look for HTML tags
if (\preg_match_all('/([<][^>]+[>])/si', $string, $tags)) {
// Exchange all tags with character 17
$string = \preg_replace('/[<][^>]+[>]/si', \chr(17), $string);

// @codeCoverageIgnoreStart
if ($string === null) {
throw $this->generateRegexException();
}
// @codeCoverageIgnoreEnd

// Add extra spaces to break up long words
$string = \preg_replace("/([^ \n]{" . $this->maxCharacters . '})(?=[^ \n])/siu', "\\1 ", $string);

// @codeCoverageIgnoreStart
if ($string === null) {
throw $this->generateRegexException();
}
// @codeCoverageIgnoreEnd

// Put HTML tags back into the string - exchange characters 17 with HTML tags
foreach ($tags[0] as $key => $val) {
$string = \preg_replace('/' . \chr(17) . '/si', $tags[0][$key], $string, 1);

// @codeCoverageIgnoreStart
if ($string === null) {
throw $this->generateRegexException();
}
// @codeCoverageIgnoreEnd
}

return $string;
return $this->wrapHTML($string, $tags);
}

// No HTML tags found - use the non-HTML wrapper
return $this->wrapNonHTML($string);
}

/**
* Wrap long words and do not factor in HTML
* Wrap long words when there is no HTML tags in the string
*
* @param string $string
* @return string
Expand All @@ -83,4 +54,45 @@ private function wrapNonHTML(string $string): string

return $string;
}

/**
* Wrap long words and count HTML elements as only one character each
*
* @param string $string
* @param array $tags
* @return string
*/
private function wrapHTML(string $string, array $tags): string
{
// Exchange all tags with character 17
$string = \preg_replace('/[<][^>]+[>]/si', \chr(17), $string);

// @codeCoverageIgnoreStart
if ($string === null) {
throw $this->generateRegexException();
}
// @codeCoverageIgnoreEnd

// Add extra spaces to break up long words
$string = \preg_replace("/([^ \n]{" . $this->maxCharacters . '})(?=[^ \n])/siu', "\\1 ", $string);

// @codeCoverageIgnoreStart
if ($string === null) {
throw $this->generateRegexException();
}
// @codeCoverageIgnoreEnd

// Put HTML tags back into the string - exchange characters 17 with HTML tags
foreach ($tags[0] as $key => $val) {
$string = \preg_replace('/' . \chr(17) . '/si', $tags[0][$key], $string, 1);

// @codeCoverageIgnoreStart
if ($string === null) {
throw $this->generateRegexException();
}
// @codeCoverageIgnoreEnd
}

return $string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Handles all random string generators
*/
class RandomStringGeneratorManager implements RandomStringGeneratorSelectInterface
class RandomStringGeneratorSelector implements RandomStringGeneratorSelectInterface
{
use InvalidValueExceptionTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Handles all string filters
*/
class StringFilterManager implements StringFilterSelectInterface
class StringFilterSelector implements StringFilterSelectInterface
{
use InvalidValueExceptionTrait;

Expand Down
4 changes: 2 additions & 2 deletions tests/FormExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Squirrel\Strings\Annotation\StringFilterProcessor;
use Squirrel\Strings\Filter\LowercaseFilter;
use Squirrel\Strings\Filter\TrimFilter;
use Squirrel\Strings\StringFilterManager;
use Squirrel\Strings\StringFilterSelector;
use Squirrel\Strings\Tests\TestClasses\ClassWithPrivateProperties;
use Squirrel\Strings\Tests\TestClasses\ClassWithPublicProperties;
use Squirrel\Strings\Tests\TestForms\PrivatePropertiesForm;
Expand All @@ -32,7 +32,7 @@ protected function setUp(): void
$reader = new AnnotationReader();

// String filters available
$manager = new StringFilterManager([
$manager = new StringFilterSelector([
'Lowercase' => new LowercaseFilter(),
'Trim' => new TrimFilter(),
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/RandomStringGeneratorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Squirrel\Strings\Exception\InvalidValueException;
use Squirrel\Strings\Random\GeneratorAscii;
use Squirrel\Strings\Random\GeneratorUnicode;
use Squirrel\Strings\RandomStringGeneratorManager;
use Squirrel\Strings\RandomStringGeneratorSelector;

class RandomStringGeneratorManagerTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -16,7 +16,7 @@ public function testRegular()
'two' => new GeneratorUnicode('346789'),
];

$manager = new RandomStringGeneratorManager($generators);
$manager = new RandomStringGeneratorSelector($generators);

$this->assertSame($generators['one'], $manager->getGenerator('one'));
$this->assertSame($generators['two'], $manager->getGenerator('two'));
Expand All @@ -26,7 +26,7 @@ public function testNotFound()
{
$this->expectException(InvalidValueException::class);

$manager = new RandomStringGeneratorManager([]);
$manager = new RandomStringGeneratorSelector([]);
$manager->getGenerator('dada');
}
}
4 changes: 2 additions & 2 deletions tests/StringFilterAnnotationProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Squirrel\Strings\Exception\InvalidValueException;
use Squirrel\Strings\Filter\LowercaseFilter;
use Squirrel\Strings\Filter\TrimFilter;
use Squirrel\Strings\StringFilterManager;
use Squirrel\Strings\StringFilterSelector;
use Squirrel\Strings\Tests\TestClasses\ClassWithInvalidAnnotations;
use Squirrel\Strings\Tests\TestClasses\ClassWithPrivateProperties;
use Squirrel\Strings\Tests\TestClasses\ClassWithPublicProperties;
Expand All @@ -31,7 +31,7 @@ protected function setUp(): void
$reader = new AnnotationReader();

// String filters available
$manager = new StringFilterManager([
$manager = new StringFilterSelector([
'Lowercase' => new LowercaseFilter(),
'Trim' => new TrimFilter(),
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/StringFilterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Squirrel\Strings\Exception\InvalidValueException;
use Squirrel\Strings\Filter\LowercaseFilter;
use Squirrel\Strings\Filter\TrimFilter;
use Squirrel\Strings\StringFilterManager;
use Squirrel\Strings\StringFilterSelector;

class StringFilterManagerTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -16,7 +16,7 @@ public function testRegular()
'Trim' => new TrimFilter(),
];

$manager = new StringFilterManager($filters);
$manager = new StringFilterSelector($filters);

$this->assertSame($filters['Lowercase'], $manager->getFilter('Lowercase'));
$this->assertSame($filters['Trim'], $manager->getFilter('Trim'));
Expand All @@ -26,7 +26,7 @@ public function testNotFound()
{
$this->expectException(InvalidValueException::class);

$manager = new StringFilterManager([]);
$manager = new StringFilterSelector([]);
$manager->getFilter('dada');
}
}
8 changes: 4 additions & 4 deletions tests/TwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Squirrel\Strings\Filter\LowercaseFilter;
use Squirrel\Strings\Filter\TrimFilter;
use Squirrel\Strings\Random\GeneratorAscii;
use Squirrel\Strings\RandomStringGeneratorManager;
use Squirrel\Strings\StringFilterManager;
use Squirrel\Strings\RandomStringGeneratorSelector;
use Squirrel\Strings\StringFilterSelector;
use Squirrel\Strings\Twig\StringExtension;

class TwigExtensionTest extends \PHPUnit\Framework\TestCase
Expand All @@ -19,12 +19,12 @@ class TwigExtensionTest extends \PHPUnit\Framework\TestCase

protected function setUp(): void
{
$filterManager = new StringFilterManager([
$filterManager = new StringFilterSelector([
'Lowercase' => new LowercaseFilter(),
'Trim' => new TrimFilter(),
]);

$randomManager = new RandomStringGeneratorManager([
$randomManager = new RandomStringGeneratorSelector([
'one' => new GeneratorAscii('346789'),
'eighteen' => new GeneratorAscii('18'),
]);
Expand Down

0 comments on commit 18a1562

Please sign in to comment.