Skip to content

Commit

Permalink
Add streamline email filter
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed Apr 14, 2021
1 parent 3a75369 commit ca8bb4d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Filter/NormalizeToAlphanumericFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class NormalizeToAlphanumericFilter implements StringFilterInterface
{
private StringFilterRunner $filterRunner;
private StringFilterInterface $filterRunner;

public function __construct()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/NormalizeToAlphanumericLowercaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class NormalizeToAlphanumericLowercaseFilter implements StringFilterInterface
{
private StringFilterRunner $filterRunner;
private StringFilterInterface $filterRunner;

public function __construct()
{
Expand Down
32 changes: 32 additions & 0 deletions src/Filter/StreamlineEmailFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Squirrel\Strings\Filter;

use Squirrel\Strings\StringFilterInterface;

/**
* Streamline email format, converting domain part to lowercase and keeping the username part case-sensitive
*/
class StreamlineEmailFilter implements StringFilterInterface
{
public function filter(string $string): string
{
if (\strpos($string, '@') === false) {
return $string;
}

// Split up email in username and domain part
$emailParts = \explode('@', $string);

// The first part is always the username part
$username = \array_shift($emailParts);

// All the other parts are the domain part
$domain = \implode('@', $emailParts);

$lowercaseFilter = new LowercaseFilter();

// Only lowercase the domain part, username can be case sensitive
return $username . '@' . $lowercaseFilter->filter($domain);
}
}
2 changes: 1 addition & 1 deletion src/Filter/StreamlineInputNoNewlinesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class StreamlineInputNoNewlinesFilter implements StringFilterInterface
{
private StringFilterRunner $filterRunner;
private StringFilterInterface $filterRunner;

public function __construct()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/StreamlineInputWithNewlinesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class StreamlineInputWithNewlinesFilter implements StringFilterInterface
{
private StringFilterRunner $filterRunner;
private StringFilterInterface $filterRunner;

public function __construct()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/StringFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Squirrel\Strings\Filter\ReplaceUnicodeWhitespacesFilter;
use Squirrel\Strings\Filter\ReplaceUnixStyleNewlinesWithParagraphsAndBreaksFilter;
use Squirrel\Strings\Filter\SnakeCaseToCamelCaseFilter;
use Squirrel\Strings\Filter\StreamlineEmailFilter;
use Squirrel\Strings\Filter\StreamlineInputNoNewlinesFilter;
use Squirrel\Strings\Filter\StreamlineInputWithNewlinesFilter;
use Squirrel\Strings\Filter\TrimFilter;
Expand Down Expand Up @@ -374,4 +375,22 @@ public function testRemoveHTMLCharacters()
{
$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));
}

public function testEmailStreamline()
{
$emails = [
'bademail' => 'bademail',
'BADEMAIL' => 'BADEMAIL',
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
'[email protected]' => '[email protected]',
];

$emailFilter = new StreamlineEmailFilter();

foreach ($emails as $input => $expectedOutput) {
$this->assertSame($expectedOutput, $emailFilter->filter($input));
}
}
}

0 comments on commit ca8bb4d

Please sign in to comment.