Skip to content

Commit ba4669e

Browse files
committed
Can now keep inputted case
1 parent ea0522f commit ba4669e

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/Initials.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
class Initials
66
{
7-
private $parameter_length = 2;
8-
private $parameter_initials = 'JD';
9-
private $parameter_name = 'John Doe';
7+
private $length = 2;
8+
private $initials = 'JD';
9+
private $keepCase = false;
10+
private $name = 'John Doe';
1011

1112
/**
1213
* Set the name used for generating initials.
@@ -22,6 +23,21 @@ public function name($nameOrInitials)
2223
return $this;
2324
}
2425

26+
/**
27+
* Set if should keep lettercase on name.
28+
* Setting this to false (default) will uppercase the name.
29+
*
30+
* @param boolean $keepCase
31+
*
32+
* @return Initials
33+
*/
34+
public function keepCase($keepCase = true)
35+
{
36+
$this->keepCase = $keepCase;
37+
38+
return $this;
39+
}
40+
2541
/**
2642
* Set the length of the generated initials.
2743
*
@@ -31,8 +47,8 @@ public function name($nameOrInitials)
3147
*/
3248
public function length($length = 2)
3349
{
34-
$this->parameter_length = (int) $length;
35-
$this->parameter_initials = $this->generateInitials();
50+
$this->length = (int) $length;
51+
$this->initials = $this->generateInitials();
3652

3753
return $this;
3854
}
@@ -47,8 +63,8 @@ public function length($length = 2)
4763
public function generate($name = null)
4864
{
4965
if ($name !== null) {
50-
$this->parameter_name = $name;
51-
$this->parameter_initials = $this->generateInitials();
66+
$this->name = $name;
67+
$this->initials = $this->generateInitials();
5268
}
5369

5470
return (string) $this;
@@ -61,7 +77,7 @@ public function generate($name = null)
6177
*/
6278
public function getInitials()
6379
{
64-
return $this->parameter_initials;
80+
return $this->initials;
6581
}
6682

6783
/**
@@ -74,7 +90,7 @@ public function getUrlfriendlyInitials()
7490
{
7591
$urlFriendlyInitials = $this->convertToUrlFriendlyString($this->getInitials());
7692

77-
$urlFriendlyInitials = mb_substr($urlFriendlyInitials, 0, $this->parameter_length);
93+
$urlFriendlyInitials = mb_substr($urlFriendlyInitials, 0, $this->length);
7894

7995
return $urlFriendlyInitials;
8096
}
@@ -99,7 +115,12 @@ public function __toString()
99115
*/
100116
private function generateInitials()
101117
{
102-
$nameOrInitials = mb_strtoupper(trim($this->parameter_name));
118+
$nameOrInitials = trim($this->name);
119+
120+
if( !$this->keepCase ) {
121+
$nameOrInitials = mb_strtoupper($nameOrInitials);
122+
}
123+
103124
$names = explode(' ', $nameOrInitials);
104125
$initials = $nameOrInitials;
105126
$assignedNames = 0;
@@ -108,10 +129,10 @@ private function generateInitials()
108129
$initials = '';
109130
$start = 0;
110131

111-
for ($i = 0; $i < $this->parameter_length; $i++) {
132+
for ($i = 0; $i < $this->length; $i++) {
112133
$index = $i;
113134

114-
if (($index === ($this->parameter_length - 1) && $index > 0) || ($index > (count($names) - 1))) {
135+
if (($index === ($this->length - 1) && $index > 0) || ($index > (count($names) - 1))) {
115136
$index = count($names) - 1;
116137
}
117138

@@ -124,7 +145,7 @@ private function generateInitials()
124145
}
125146
}
126147

127-
$initials = mb_substr($initials, 0, $this->parameter_length);
148+
$initials = mb_substr($initials, 0, $this->length);
128149

129150
return $initials;
130151
}

tests/InitialCaseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class InitialCaseTest extends TestCase
6+
{
7+
public function testCanPreventUppercase()
8+
{
9+
$avatar = new \LasseRafn\Initials\Initials();
10+
11+
$avatar->name('lRa')->keepCase()->length(3);
12+
13+
$this->assertEquals('lRa', $avatar->getInitials());
14+
$this->assertEquals(3, strlen($avatar->getInitials()));
15+
}
16+
}

0 commit comments

Comments
 (0)