Skip to content

Commit ec43cbf

Browse files
committed
Add RemoveNonAlphabeticFilter
1 parent 9981bb5 commit ec43cbf

File tree

6 files changed

+74
-26
lines changed

6 files changed

+74
-26
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ Yet this might be overkill for generic user input, where you just want to try to
172172

173173
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.
174174

175+
#### RemoveNonAlphabetic
176+
177+
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.
178+
175179
#### RemoveNonNumeric
176180

177181
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.

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
"squirrelphp/strings-bundle": "Symfony integration of squirrelphp/strings"
3636
},
3737
"config": {
38-
"sort-packages": false
38+
"sort-packages": false,
39+
"allow-plugins": {
40+
"bamarni/composer-bin-plugin": true,
41+
"captainhook/plugin-composer": true
42+
}
3943
},
4044
"autoload": {
4145
"psr-4": {

psalm.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="false"
3+
errorLevel="2"
4+
reportMixedIssues="false"
45
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
56
xmlns="https://getpsalm.org/schema/config"
67
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Squirrel\Strings\Filter;
4+
5+
use Squirrel\Strings\Common\RegexExceptionTrait;
6+
use Squirrel\Strings\StringFilterInterface;
7+
8+
/**
9+
* Remove any characters which are not alphabetic (A-Z)
10+
*/
11+
class RemoveNonAlphabeticFilter implements StringFilterInterface
12+
{
13+
use RegexExceptionTrait;
14+
15+
public function filter(string $string): string
16+
{
17+
$string = \preg_replace("/[^a-zA-Z]/", '', $string);
18+
19+
// @codeCoverageIgnoreStart
20+
if ($string === null) {
21+
throw $this->generateRegexException();
22+
}
23+
// @codeCoverageIgnoreEnd
24+
25+
return $string;
26+
}
27+
}

tests/StringFilterTest.php

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Squirrel\Strings\Filter\RemoveExcessSpacesFilter;
1616
use Squirrel\Strings\Filter\RemoveHTMLTagCharacters;
1717
use Squirrel\Strings\Filter\RemoveHTMLTagsFilter;
18+
use Squirrel\Strings\Filter\RemoveNonAlphabeticFilter;
1819
use Squirrel\Strings\Filter\RemoveNonAlphanumericFilter;
1920
use Squirrel\Strings\Filter\RemoveNonAsciiAndControlCharactersFilter;
2021
use Squirrel\Strings\Filter\RemoveNonNumericFilter;
@@ -57,78 +58,78 @@ class StringFilterTest extends \PHPUnit\Framework\TestCase
5758
*
5859
* @var string
5960
*/
60-
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; ";
61+
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; ";
6162

6263
public function testDecodeAllHTMLEntities(): void
6364
{
64-
$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));
65+
$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));
6566
}
6667

6768
public function testDecodeBasicHTMLEntities(): void
6869
{
69-
$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));
70+
$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));
7071
}
7172

7273
public function testRemoveExcessSpaces(): void
7374
{
74-
$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));
75+
$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));
7576
}
7677

7778
public function testRemoveHTML(): void
7879
{
79-
$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));
80+
$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));
8081
}
8182

8283
public function testReplaceNewlinesWithSpaces(): void
8384
{
84-
$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));
85+
$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));
8586
}
8687

8788
public function testReplaceTabsWithSpaces(): void
8889
{
89-
$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));
90+
$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));
9091
}
9192

9293
public function testLimitConsecutiveUnixNewlinesFilter(): void
9394
{
94-
$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));
95+
$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));
9596
}
9697

9798
public function testReplaceUnicodeWhitespaces(): void
9899
{
99-
$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));
100+
$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));
100101
}
101102

102103
public function testLowercase(): void
103104
{
104-
$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));
105+
$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));
105106
}
106107

107108
public function testUppercase(): void
108109
{
109-
$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));
110+
$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));
110111
}
111112

112113
public function testUppercaseWordsFirstCharacter(): void
113114
{
114-
$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));
115+
$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));
115116
}
116117

117118
public function testUppercaseFirstCharacter(): void
118119
{
119-
$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));
120+
$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));
120121
}
121122

122123
public function testTrim(): void
123124
{
124-
$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));
125+
$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));
125126
}
126127

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

131-
$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));
132+
$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));
132133
}
133134

134135
public function testWrapLongWordsNoHTML(): void
@@ -163,17 +164,23 @@ public function testWrapLongWordsWithHTML(): void
163164

164165
public function testRemoveNonAlphanumeric(): void
165166
{
166-
$this->assertEquals('amphahastrongmanystrongnbspgrssgtterinvalidletatthingcontainedtrade', (new RemoveNonAlphanumericFilter())->filter($this->testString));
167+
$this->assertEquals('amphaha13strongmanystrongnbspgrssgtterinvalidletatthingcontainedtrade', (new RemoveNonAlphanumericFilter())->filter($this->testString));
168+
}
169+
170+
public function testRemoveNonAlphabetic(): void
171+
{
172+
$this->assertEquals('amphahastrongmanystrongnbspgrssgtterinvalidletatthingcontainedtrade', (new RemoveNonAlphabeticFilter())->filter($this->testString));
167173
}
168174

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

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

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

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

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

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

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

204211
public function testStreamlineInputNoNewlines(): void
205212
{
206213
$string = $this->testString;
207214
$string = (new StreamlineInputNoNewlinesFilter())->filter($string);
208215

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

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

225-
$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);
232+
$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);
226233
}
227234

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

232-
$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);
239+
$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);
233240
}
234241

235242
public function testSnakeCaseToCamelCase(): void
@@ -373,7 +380,7 @@ public function testTrimUnicode(): void
373380

374381
public function testRemoveHTMLCharacters(): void
375382
{
376-
$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));
383+
$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));
377384
}
378385

379386
public function testEmailStreamline(): void

vendor-bin/phpcs/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"require": {
33
"squizlabs/php_codesniffer": "^3.5",
44
"slevomat/coding-standard": "^7.0"
5+
},
6+
"config": {
7+
"allow-plugins": {
8+
"dealerdirect/phpcodesniffer-composer-installer": false
9+
}
510
}
611
}

0 commit comments

Comments
 (0)