Skip to content

Commit

Permalink
Add RemoveNonAlphabeticFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed Mar 5, 2022
1 parent 9981bb5 commit ec43cbf
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ Yet this might be overkill for generic user input, where you just want to try to

Remove any characters which are not letters or numbers, so only A-Z, a-z and 0-9 are allowed. Can be handy for tokens, parts of an URL, an entered code, or other things where you know no other characters are allowed and you just want to ignore anything non-alphanumeric.

#### RemoveNonAlphabetic

Remove any characters which are not letters, so only A-Z and a-z are allowed. Can be handy for tokens, country or language codes, or other things where you know no other characters are allowed and you just want to ignore them.

#### RemoveNonNumeric

Remove any characters which are not numbers, so only 0-9 are allowed. Can be handy for tokens, parts of an URL, an entered code, or other things where you know no other characters are allowed and you just want to ignore them.
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
"squirrelphp/strings-bundle": "Symfony integration of squirrelphp/strings"
},
"config": {
"sort-packages": false
"sort-packages": false,
"allow-plugins": {
"bamarni/composer-bin-plugin": true,
"captainhook/plugin-composer": true
}
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="2"
reportMixedIssues="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
27 changes: 27 additions & 0 deletions src/Filter/RemoveNonAlphabeticFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Squirrel\Strings\Filter;

use Squirrel\Strings\Common\RegexExceptionTrait;
use Squirrel\Strings\StringFilterInterface;

/**
* Remove any characters which are not alphabetic (A-Z)
*/
class RemoveNonAlphabeticFilter implements StringFilterInterface
{
use RegexExceptionTrait;

public function filter(string $string): string
{
$string = \preg_replace("/[^a-zA-Z]/", '', $string);

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

return $string;
}
}
55 changes: 31 additions & 24 deletions tests/StringFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Squirrel\Strings\Filter\RemoveExcessSpacesFilter;
use Squirrel\Strings\Filter\RemoveHTMLTagCharacters;
use Squirrel\Strings\Filter\RemoveHTMLTagsFilter;
use Squirrel\Strings\Filter\RemoveNonAlphabeticFilter;
use Squirrel\Strings\Filter\RemoveNonAlphanumericFilter;
use Squirrel\Strings\Filter\RemoveNonAsciiAndControlCharactersFilter;
use Squirrel\Strings\Filter\RemoveNonNumericFilter;
Expand Down Expand Up @@ -57,78 +58,78 @@ class StringFilterTest extends \PHPUnit\Framework\TestCase
*
* @var string
*/
private $testString = " &amp; haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ";
private $testString = " &amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ";

public function testDecodeAllHTMLEntities(): void
{
$this->assertEquals(" & haha <strong>many</strong> \xc2\xa0 \xc2\xa0 grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!™ ", (new DecodeAllHTMLEntitiesFilter())->filter($this->testString));
$this->assertEquals(" & haha 13<strong>many</strong> \xc2\xa0 \xc2\xa0 grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!™ ", (new DecodeAllHTMLEntitiesFilter())->filter($this->testString));
}

public function testDecodeBasicHTMLEntities(): void
{
$this->assertEquals(" & haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new DecodeBasicHTMLEntitiesFilter())->filter($this->testString));
$this->assertEquals(" & haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new DecodeBasicHTMLEntitiesFilter())->filter($this->testString));
}

public function testRemoveExcessSpaces(): void
{
$this->assertEquals("&amp; haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t\n\n<invalid>\"l'etat\"\\ thing contained!!!&trade;", (new RemoveExcessSpacesFilter())->filter($this->testString));
$this->assertEquals("&amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t\n\n<invalid>\"l'etat\"\\ thing contained!!!&trade;", (new RemoveExcessSpacesFilter())->filter($this->testString));
}

public function testRemoveHTML(): void
{
$this->assertEquals(" &amp; haha many \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!&trade; ", (new RemoveHTMLTagsFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13many \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!&trade; ", (new RemoveHTMLTagsFilter())->filter($this->testString));
}

public function testReplaceNewlinesWithSpaces(): void
{
$this->assertEquals(" &amp; haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \t <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new ReplaceNewlinesWithSpacesFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \t <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new ReplaceNewlinesWithSpacesFilter())->filter($this->testString));
}

public function testReplaceTabsWithSpaces(): void
{
$this->assertEquals(" &amp; haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new ReplaceTabsWithSpacesFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new ReplaceTabsWithSpacesFilter())->filter($this->testString));
}

public function testLimitConsecutiveUnixNewlinesFilter(): void
{
$this->assertEquals(" &amp; haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new LimitConsecutiveUnixNewlinesFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new LimitConsecutiveUnixNewlinesFilter())->filter($this->testString));
}

public function testReplaceUnicodeWhitespaces(): void
{
$this->assertEquals(" &amp; haha <strong>many</strong> &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new ReplaceUnicodeWhitespacesFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13<strong>many</strong> &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new ReplaceUnicodeWhitespacesFilter())->filter($this->testString));
}

public function testLowercase(): void
{
$this->assertEquals(" &amp; haha <strong>many</strong>   &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\ thing contained!!!&trade; ", (new LowercaseFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13<strong>many</strong>   &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\ thing contained!!!&trade; ", (new LowercaseFilter())->filter($this->testString));
}

public function testUppercase(): void
{
$this->assertEquals(" &AMP; HAHA <STRONG>MANY</STRONG>   &NBSP; GRÜSS GÖTTER \r\n\n\n\t \n \n <INVALID>\"L'ETAT\"\ THING CONTAINED!!!&TRADE; ", (new UppercaseFilter())->filter($this->testString));
$this->assertEquals(" &AMP; HAHA 13<STRONG>MANY</STRONG>   &NBSP; GRÜSS GÖTTER \r\n\n\n\t \n \n <INVALID>\"L'ETAT\"\ THING CONTAINED!!!&TRADE; ", (new UppercaseFilter())->filter($this->testString));
}

public function testUppercaseWordsFirstCharacter(): void
{
$this->assertEquals(" &Amp; Haha <Strong>Many</Strong>   &Nbsp; Grüss Götter \r\n\n\n\t \n \n <Invalid>\"L'Etat\"\ Thing Contained!!!&Trade; ", (new UppercaseWordsFirstCharacterFilter())->filter($this->testString));
$this->assertEquals(" &Amp; Haha 13<Strong>Many</Strong>   &Nbsp; Grüss Götter \r\n\n\n\t \n \n <Invalid>\"L'Etat\"\ Thing Contained!!!&Trade; ", (new UppercaseWordsFirstCharacterFilter())->filter($this->testString));
}

public function testUppercaseFirstCharacter(): void
{
$this->assertEquals(" &amp; haha <strong>many</strong>   &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\ thing contained!!!&trade; ", (new UppercaseFirstCharacterFilter())->filter($this->testString));
$this->assertEquals(" &amp; haha 13<strong>many</strong>   &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\ thing contained!!!&trade; ", (new UppercaseFirstCharacterFilter())->filter($this->testString));
}

public function testTrim(): void
{
$this->assertEquals("&amp; haha <strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade;", (new TrimFilter())->filter($this->testString));
$this->assertEquals("&amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade;", (new TrimFilter())->filter($this->testString));
}

public function testRemoveNonUTF8Characters(): void
{
$nonUTF8 = utf8_decode($this->testString);

$this->assertEquals(" &amp; haha <strong>many</strong> &nbsp; grss gtter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new RemoveNonUTF8CharactersFilter())->filter($nonUTF8));
$this->assertEquals(" &amp; haha 13<strong>many</strong> &nbsp; grss gtter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", (new RemoveNonUTF8CharactersFilter())->filter($nonUTF8));
}

public function testWrapLongWordsNoHTML(): void
Expand Down Expand Up @@ -163,17 +164,23 @@ public function testWrapLongWordsWithHTML(): void

public function testRemoveNonAlphanumeric(): void
{
$this->assertEquals('amphahastrongmanystrongnbspgrssgtterinvalidletatthingcontainedtrade', (new RemoveNonAlphanumericFilter())->filter($this->testString));
$this->assertEquals('amphaha13strongmanystrongnbspgrssgtterinvalidletatthingcontainedtrade', (new RemoveNonAlphanumericFilter())->filter($this->testString));
}

public function testRemoveNonAlphabetic(): void
{
$this->assertEquals('amphahastrongmanystrongnbspgrssgtterinvalidletatthingcontainedtrade', (new RemoveNonAlphabeticFilter())->filter($this->testString));
}

public function testRemoveNonNumeric(): void
{
$this->assertEquals('4513', (new RemoveNonNumericFilter())->filter('hallö 45 dadaismus! <huhu> 1/3'));
$this->assertEquals('13', (new RemoveNonNumericFilter())->filter($this->testString));
}

public function testReplaceNonAlphanumeric(): void
{
$this->assertEquals('-amp-haha-strong-many-strong-nbsp-gr-ss-g-tter-invalid-l-etat-thing-contained-trade-', (new ReplaceNonAlphanumericFilter())->filter($this->testString));
$this->assertEquals('-amp-haha-13-strong-many-strong-nbsp-gr-ss-g-tter-invalid-l-etat-thing-contained-trade-', (new ReplaceNonAlphanumericFilter())->filter($this->testString));
}

public function testUrlFriendly(): void
Expand All @@ -184,12 +191,12 @@ public function testUrlFriendly(): void
$string = (new ReplaceNonAlphanumericFilter())->filter($string);
$string = (new TrimFilter('-'))->filter($string);

$this->assertEquals('haha-strong-many-strong-nbsp-gruss-gotter-invalid-l-etat-thing-contained-trade', $string);
$this->assertEquals('haha-13-strong-many-strong-nbsp-gruss-gotter-invalid-l-etat-thing-contained-trade', $string);
}

public function testRemoveNonAsciiAndControlCharacters(): void
{
$this->assertEquals(' &amp; haha <strong>many</strong> &nbsp; grss gtter <invalid>"l\'etat"\\ thing contained!!!&trade; ', (new RemoveNonAsciiAndControlCharactersFilter())->filter($this->testString));
$this->assertEquals(' &amp; haha 13<strong>many</strong> &nbsp; grss gtter <invalid>"l\'etat"\\ thing contained!!!&trade; ', (new RemoveNonAsciiAndControlCharactersFilter())->filter($this->testString));
}

public function testDecodeBasicHTMLEntitiesAndStreamlineInputWithNewlines(): void
Expand All @@ -198,15 +205,15 @@ public function testDecodeBasicHTMLEntitiesAndStreamlineInputWithNewlines(): voi
$string = (new DecodeBasicHTMLEntitiesFilter())->filter($string);
$string = (new StreamlineInputWithNewlinesFilter())->filter($string);

$this->assertEquals("& haha <strong>many</strong> &nbsp; grüss götter\n\n<invalid>\"l'etat\"\\ thing contained!!!&trade;", $string);
$this->assertEquals("& haha 13<strong>many</strong> &nbsp; grüss götter\n\n<invalid>\"l'etat\"\\ thing contained!!!&trade;", $string);
}

public function testStreamlineInputNoNewlines(): void
{
$string = $this->testString;
$string = (new StreamlineInputNoNewlinesFilter())->filter($string);

$this->assertEquals("&amp; haha <strong>many</strong> &nbsp; grüss götter <invalid>\"l'etat\"\\ thing contained!!!&trade;", $string);
$this->assertEquals("&amp; haha 13<strong>many</strong> &nbsp; grüss götter <invalid>\"l'etat\"\\ thing contained!!!&trade;", $string);
}

public function testReplaceNewlinesWithParagraphsAndBreaks(): void
Expand All @@ -222,14 +229,14 @@ public function testEncodeBasicHTMLEntities(): void
{
$string = (new EncodeBasicHTMLEntitiesFilter())->filter($this->testString);

$this->assertEquals(" &amp;amp; haha &lt;strong&gt;many&lt;/strong&gt; \xc2\xa0 &amp;nbsp; grüss götter \r\n\n\n\t \n \n &lt;invalid&gt;&quot;l&apos;etat&quot;\\ thing contained!!!&amp;trade; ", $string);
$this->assertEquals(" &amp;amp; haha 13&lt;strong&gt;many&lt;/strong&gt; \xc2\xa0 &amp;nbsp; grüss götter \r\n\n\n\t \n \n &lt;invalid&gt;&quot;l&apos;etat&quot;\\ thing contained!!!&amp;trade; ", $string);
}

public function testNormalizeLettersToAscii(): void
{
$string = (new NormalizeLettersToAsciiFilter())->filter($this->testString);

$this->assertEquals(" &amp; haha <strong>many</strong> \xc2\xa0 &nbsp; gruss gotter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", $string);
$this->assertEquals(" &amp; haha 13<strong>many</strong> \xc2\xa0 &nbsp; gruss gotter \r\n\n\n\t \n \n <invalid>\"l'etat\"\\ thing contained!!!&trade; ", $string);
}

public function testSnakeCaseToCamelCase(): void
Expand Down Expand Up @@ -373,7 +380,7 @@ public function testTrimUnicode(): void

public function testRemoveHTMLCharacters(): void
{
$this->assertEquals(" &amp; haha strongmany/strong \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n invalidl'etat\\ thing contained!!!&trade; ", (new RemoveHTMLTagCharacters())->filter($this->testString));
$this->assertEquals(" &amp; haha 13strongmany/strong \xc2\xa0 &nbsp; grüss götter \r\n\n\n\t \n \n invalidl'etat\\ thing contained!!!&trade; ", (new RemoveHTMLTagCharacters())->filter($this->testString));
}

public function testEmailStreamline(): void
Expand Down
5 changes: 5 additions & 0 deletions vendor-bin/phpcs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"require": {
"squizlabs/php_codesniffer": "^3.5",
"slevomat/coding-standard": "^7.0"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
}

0 comments on commit ec43cbf

Please sign in to comment.