Created
September 19, 2018 10:42
-
-
Save jaggy/18b0b0a9954fc19a5f1f7387391098a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
trait HasPersonName | |
{ | |
public function getNameAttribute($name) | |
{ | |
return new PersonName($name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Contracts\Support\Arrayable; | |
class PersonName implements Arrayable | |
{ | |
private $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
public function full() | |
{ | |
return $this->name; | |
} | |
public function first() | |
{ | |
return str_before($this->name, ' '); | |
} | |
public function last() | |
{ | |
if (! str_contains($this->name, ' ')) { | |
return null; | |
} | |
return str_after($this->name, ' '); | |
} | |
public function initials() | |
{ | |
preg_match_all('/(?<=\s|^)[A-Z]/', $this->name, $matches); | |
return implode('', head($matches)); | |
} | |
public function abbreviated() | |
{ | |
return $this->first()[0] . '. ' . $this->last(); | |
} | |
public function sorted() | |
{ | |
return $this->last() . ', ' . $this->first(); | |
} | |
public function mentionable() | |
{ | |
return strtolower($this->first() . $this->last()[0]); | |
} | |
public function possessive() | |
{ | |
if (ends_with($this->name, 's')) { | |
return $this->name . "'"; | |
} | |
return $this->name . "'s"; | |
} | |
public function familiar() | |
{ | |
return $this->first() . ' ' . strtoupper($this->last()[0]) . '.'; | |
} | |
public function __get($attribute) | |
{ | |
return call_user_func([$this, $attribute]); | |
} | |
public function __toString() | |
{ | |
return $this->name; | |
} | |
public function toArray() | |
{ | |
return [ | |
'full' => $this->name, | |
'first' => $this->first, | |
'last' => $this->last, | |
'initials' => $this->initials, | |
'abbreviated' => $this->abbreviated, | |
'sorted' => $this->sorted, | |
'mentionable' => $this->mentionable, | |
'possessive' => $this->possessive, | |
'familiar' => $this->familiar, | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Unit; | |
use Tests\TestCase; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use App\PersonName; | |
class PersonNameTest extends TestCase | |
{ | |
function test_full_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('Terry Crews', $name->full); | |
} | |
function test_first_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('Terry', $name->first); | |
} | |
function test_last_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('Crews', $name->last); | |
} | |
function test_return_null_when_no_last_name() | |
{ | |
$name = new PersonName('Terry'); | |
$this->assertEquals('', $name->last); | |
} | |
function test_initials() | |
{ | |
$name = new PersonName('Terry A. Crews'); | |
$this->assertEquals('TAC', $name->initials); | |
} | |
function test_abbreviated_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('T. Crews', $name->abbreviated); | |
} | |
function test_sorted_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('Crews, Terry', $name->sorted); | |
} | |
function test_mentionable_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('terryc', $name->mentionable); | |
} | |
function test_possessive_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals("Terry Crews'", $name->possessive); | |
$name = new PersonName('Melissa Fumero'); | |
$this->assertEquals("Melissa Fumero's", $name->possessive); | |
} | |
function test_familiar_name() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals("Terry C.", $name->familiar); | |
} | |
function test_full_name_when_object_cast_as_string() | |
{ | |
$name = new PersonName('Terry Crews'); | |
$this->assertEquals('Terry Crews', (string) $name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment