From ec43cbf49b7207e364a5d345da192a235b4dff5e Mon Sep 17 00:00:00 2001 From: Andreas Leathley Date: Sat, 5 Mar 2022 10:09:04 +0100 Subject: [PATCH] Add RemoveNonAlphabeticFilter --- README.md | 4 ++ composer.json | 6 ++- psalm.xml | 3 +- src/Filter/RemoveNonAlphabeticFilter.php | 27 ++++++++++++ tests/StringFilterTest.php | 55 +++++++++++++----------- vendor-bin/phpcs/composer.json | 5 +++ 6 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 src/Filter/RemoveNonAlphabeticFilter.php diff --git a/README.md b/README.md index 92fb947..924d042 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 4e5ef59..822bb8d 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/psalm.xml b/psalm.xml index d4c531f..30e5581 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,7 @@ generateRegexException(); + } + // @codeCoverageIgnoreEnd + + return $string; + } +} diff --git a/tests/StringFilterTest.php b/tests/StringFilterTest.php index ecd2f95..3378bcb 100644 --- a/tests/StringFilterTest.php +++ b/tests/StringFilterTest.php @@ -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; @@ -57,78 +58,78 @@ class StringFilterTest extends \PHPUnit\Framework\TestCase * * @var string */ - private $testString = " & haha many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ "; + private $testString = " & haha 13many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ "; public function testDecodeAllHTMLEntities(): void { - $this->assertEquals(" & haha many \xc2\xa0 \xc2\xa0 grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new DecodeAllHTMLEntitiesFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many \xc2\xa0 \xc2\xa0 grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new DecodeAllHTMLEntitiesFilter())->filter($this->testString)); } public function testDecodeBasicHTMLEntities(): void { - $this->assertEquals(" & haha many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new DecodeBasicHTMLEntitiesFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new DecodeBasicHTMLEntitiesFilter())->filter($this->testString)); } public function testRemoveExcessSpaces(): void { - $this->assertEquals("& haha many \xc2\xa0   grüss götter \r\n\n\n\t\n\n\"l'etat\"\\ thing contained!!!™", (new RemoveExcessSpacesFilter())->filter($this->testString)); + $this->assertEquals("& haha 13many \xc2\xa0   grüss götter \r\n\n\n\t\n\n\"l'etat\"\\ thing contained!!!™", (new RemoveExcessSpacesFilter())->filter($this->testString)); } public function testRemoveHTML(): void { - $this->assertEquals(" & haha many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new RemoveHTMLTagsFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new RemoveHTMLTagsFilter())->filter($this->testString)); } public function testReplaceNewlinesWithSpaces(): void { - $this->assertEquals(" & haha many \xc2\xa0   grüss götter \t \"l'etat\"\\ thing contained!!!™ ", (new ReplaceNewlinesWithSpacesFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many \xc2\xa0   grüss götter \t \"l'etat\"\\ thing contained!!!™ ", (new ReplaceNewlinesWithSpacesFilter())->filter($this->testString)); } public function testReplaceTabsWithSpaces(): void { - $this->assertEquals(" & haha many \xc2\xa0   grüss götter \r\n\n\n \n \n \"l'etat\"\\ thing contained!!!™ ", (new ReplaceTabsWithSpacesFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many \xc2\xa0   grüss götter \r\n\n\n \n \n \"l'etat\"\\ thing contained!!!™ ", (new ReplaceTabsWithSpacesFilter())->filter($this->testString)); } public function testLimitConsecutiveUnixNewlinesFilter(): void { - $this->assertEquals(" & haha many \xc2\xa0   grüss götter \r\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new LimitConsecutiveUnixNewlinesFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many \xc2\xa0   grüss götter \r\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new LimitConsecutiveUnixNewlinesFilter())->filter($this->testString)); } public function testReplaceUnicodeWhitespaces(): void { - $this->assertEquals(" & haha many   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new ReplaceUnicodeWhitespacesFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new ReplaceUnicodeWhitespacesFilter())->filter($this->testString)); } public function testLowercase(): void { - $this->assertEquals(" & haha many     grüss götter \r\n\n\n\t \n \n \"l'etat\"\ thing contained!!!™ ", (new LowercaseFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many     grüss götter \r\n\n\n\t \n \n \"l'etat\"\ thing contained!!!™ ", (new LowercaseFilter())->filter($this->testString)); } public function testUppercase(): void { - $this->assertEquals(" & HAHA MANY   &NBSP; GRÜSS GÖTTER \r\n\n\n\t \n \n \"L'ETAT\"\ THING CONTAINED!!!™ ", (new UppercaseFilter())->filter($this->testString)); + $this->assertEquals(" & HAHA 13MANY   &NBSP; GRÜSS GÖTTER \r\n\n\n\t \n \n \"L'ETAT\"\ THING CONTAINED!!!™ ", (new UppercaseFilter())->filter($this->testString)); } public function testUppercaseWordsFirstCharacter(): void { - $this->assertEquals(" &Amp; Haha Many   &Nbsp; Grüss Götter \r\n\n\n\t \n \n \"L'Etat\"\ Thing Contained!!!&Trade; ", (new UppercaseWordsFirstCharacterFilter())->filter($this->testString)); + $this->assertEquals(" &Amp; Haha 13Many   &Nbsp; Grüss Götter \r\n\n\n\t \n \n \"L'Etat\"\ Thing Contained!!!&Trade; ", (new UppercaseWordsFirstCharacterFilter())->filter($this->testString)); } public function testUppercaseFirstCharacter(): void { - $this->assertEquals(" & haha many     grüss götter \r\n\n\n\t \n \n \"l'etat\"\ thing contained!!!™ ", (new UppercaseFirstCharacterFilter())->filter($this->testString)); + $this->assertEquals(" & haha 13many     grüss götter \r\n\n\n\t \n \n \"l'etat\"\ thing contained!!!™ ", (new UppercaseFirstCharacterFilter())->filter($this->testString)); } public function testTrim(): void { - $this->assertEquals("& haha many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™", (new TrimFilter())->filter($this->testString)); + $this->assertEquals("& haha 13many \xc2\xa0   grüss götter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™", (new TrimFilter())->filter($this->testString)); } public function testRemoveNonUTF8Characters(): void { $nonUTF8 = utf8_decode($this->testString); - $this->assertEquals(" & haha many   grss gtter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new RemoveNonUTF8CharactersFilter())->filter($nonUTF8)); + $this->assertEquals(" & haha 13many   grss gtter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", (new RemoveNonUTF8CharactersFilter())->filter($nonUTF8)); } public function testWrapLongWordsNoHTML(): void @@ -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! 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 @@ -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(' & haha many   grss gtter "l\'etat"\\ thing contained!!!™ ', (new RemoveNonAsciiAndControlCharactersFilter())->filter($this->testString)); + $this->assertEquals(' & haha 13many   grss gtter "l\'etat"\\ thing contained!!!™ ', (new RemoveNonAsciiAndControlCharactersFilter())->filter($this->testString)); } public function testDecodeBasicHTMLEntitiesAndStreamlineInputWithNewlines(): void @@ -198,7 +205,7 @@ public function testDecodeBasicHTMLEntitiesAndStreamlineInputWithNewlines(): voi $string = (new DecodeBasicHTMLEntitiesFilter())->filter($string); $string = (new StreamlineInputWithNewlinesFilter())->filter($string); - $this->assertEquals("& haha many   grüss götter\n\n\"l'etat\"\\ thing contained!!!™", $string); + $this->assertEquals("& haha 13many   grüss götter\n\n\"l'etat\"\\ thing contained!!!™", $string); } public function testStreamlineInputNoNewlines(): void @@ -206,7 +213,7 @@ public function testStreamlineInputNoNewlines(): void $string = $this->testString; $string = (new StreamlineInputNoNewlinesFilter())->filter($string); - $this->assertEquals("& haha many   grüss götter \"l'etat\"\\ thing contained!!!™", $string); + $this->assertEquals("& haha 13many   grüss götter \"l'etat\"\\ thing contained!!!™", $string); } public function testReplaceNewlinesWithParagraphsAndBreaks(): void @@ -222,14 +229,14 @@ public function testEncodeBasicHTMLEntities(): void { $string = (new EncodeBasicHTMLEntitiesFilter())->filter($this->testString); - $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; ", $string); + $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; ", $string); } public function testNormalizeLettersToAscii(): void { $string = (new NormalizeLettersToAsciiFilter())->filter($this->testString); - $this->assertEquals(" & haha many \xc2\xa0   gruss gotter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", $string); + $this->assertEquals(" & haha 13many \xc2\xa0   gruss gotter \r\n\n\n\t \n \n \"l'etat\"\\ thing contained!!!™ ", $string); } public function testSnakeCaseToCamelCase(): void @@ -373,7 +380,7 @@ public function testTrimUnicode(): void public function testRemoveHTMLCharacters(): void { - $this->assertEquals(" & haha strongmany/strong \xc2\xa0   grüss götter \r\n\n\n\t \n \n invalidl'etat\\ thing contained!!!™ ", (new RemoveHTMLTagCharacters())->filter($this->testString)); + $this->assertEquals(" & haha 13strongmany/strong \xc2\xa0   grüss götter \r\n\n\n\t \n \n invalidl'etat\\ thing contained!!!™ ", (new RemoveHTMLTagCharacters())->filter($this->testString)); } public function testEmailStreamline(): void diff --git a/vendor-bin/phpcs/composer.json b/vendor-bin/phpcs/composer.json index ff825e2..bafba11 100644 --- a/vendor-bin/phpcs/composer.json +++ b/vendor-bin/phpcs/composer.json @@ -2,5 +2,10 @@ "require": { "squizlabs/php_codesniffer": "^3.5", "slevomat/coding-standard": "^7.0" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": false + } } }