Skip to content

Commit 551ef04

Browse files
committed
Option to disable special characters
1 parent ba4669e commit 551ef04

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/Initials.php

+38-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
class Initials
66
{
7-
private $length = 2;
8-
private $initials = 'JD';
9-
private $keepCase = false;
10-
private $name = 'John Doe';
7+
protected $length = 2;
8+
protected $initials = 'JD';
9+
protected $keepCase = false;
10+
protected $allowSpecialCharacters = true;
11+
protected $name = 'John Doe';
1112

1213
/**
1314
* Set the name used for generating initials.
@@ -38,6 +39,20 @@ public function keepCase($keepCase = true)
3839
return $this;
3940
}
4041

42+
/**
43+
* Set if should allow (or remove) special characters.
44+
*
45+
* @param boolean $allowSpecialCharacters
46+
*
47+
* @return Initials
48+
*/
49+
public function allowSpecialCharacters($allowSpecialCharacters = true)
50+
{
51+
$this->allowSpecialCharacters = $allowSpecialCharacters;
52+
53+
return $this;
54+
}
55+
4156
/**
4257
* Set the length of the generated initials.
4358
*
@@ -113,15 +128,32 @@ public function __toString()
113128
*
114129
* @return string
115130
*/
116-
private function generateInitials()
131+
protected function generateInitials()
117132
{
118133
$nameOrInitials = trim($this->name);
119134

120135
if( !$this->keepCase ) {
121136
$nameOrInitials = mb_strtoupper($nameOrInitials);
122137
}
123138

139+
if( !$this->allowSpecialCharacters ) {
140+
$nameOrInitials = preg_replace('/[!@#$%^&*(),.?":{}|<>_]/', '', $nameOrInitials);
141+
}
142+
143+
$nameOrInitials = trim( trim( $nameOrInitials, '-' ) );
144+
124145
$names = explode(' ', $nameOrInitials);
146+
147+
// Get names with dash (-) between into separate names
148+
$names = array_map( static function ($namePart) { return explode('-', $namePart); }, $names );
149+
$realNames = [];
150+
151+
foreach( new \RecursiveIteratorIterator( new \RecursiveArrayIterator($names) ) as $namePart ) {
152+
$realNames[] = $namePart;
153+
}
154+
155+
$names = $realNames;
156+
125157
$initials = $nameOrInitials;
126158
$assignedNames = 0;
127159

@@ -159,7 +191,7 @@ private function generateInitials()
159191
*
160192
* @return string
161193
*/
162-
private function convertToUrlFriendlyString($string)
194+
protected function convertToUrlFriendlyString($string)
163195
{
164196
foreach (static::charsArray() as $key => $val) {
165197
$string = str_replace($val, mb_substr($key, 0, 1), $string);

tests/SpecialCharactersTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class SpecialCharactersTest extends TestCase
6+
{
7+
public function testDoesNotIncludeSpecialCharactersByDefault() {
8+
// without new feature
9+
$this->assertEquals( 'J(', ( new \LasseRafn\Initials\Initials )->name( 'John Doe (Anderson)' )->getInitials() );
10+
11+
// With new feature
12+
$this->assertEquals( 'JA', ( new \LasseRafn\Initials\Initials )->allowSpecialCharacters( false )->name( 'John Doe (Anderson)' )->getInitials() );
13+
$this->assertEquals( 'JD', ( new \LasseRafn\Initials\Initials )->allowSpecialCharacters( false )->name( 'John (Doe)' )->getInitials() );
14+
15+
$this->assertEquals( 'JD', ( new \LasseRafn\Initials\Initials )->allowSpecialCharacters( false )->name( 'John (!Doe$ !--' )->getInitials() );
16+
$this->assertEquals( 'JG', ( new \LasseRafn\Initials\Initials )->allowSpecialCharacters( false )->name( 'John Mc-Guire' )->getInitials() );
17+
18+
$this->assertEquals( 'JMG', ( new \LasseRafn\Initials\Initials )->allowSpecialCharacters( false )->name( 'John Mc-Guire' )->length( 3 )->getInitials() );
19+
}
20+
}

0 commit comments

Comments
 (0)