Presenting names for English-language applications where a basic model of first and last name(s) combined is sufficient. This approach is not meant to cover all possible naming cases, deal with other languages, or even titulations. Just the basics.
You can install the package via Composer.
composer require webstronauts/person-name
$name = new PersonName::make('David Heinemeier Hansson')
echo $name->full // "David Heinemeier Hansson"
echo $name->first // "David"
echo $name->last // "Heinemeier Hansson"
echo $name->initials // "DHH"
echo $name->familiar // "David H."
echo $name->abbreviated // "D. Heinemeier Hansson"
echo $name->sorted // "Heinemeier Hansson, David"
echo $name->mentionable // "davidh"
echo $name->possessive // "David Heinemeier Hansson's"
$name = new PersonName::make("DANIEL O'DELL")
echo $name->proper // Daniel O'Dell
This is an example model which exposes a name
virtual attribute composed from the first_name
and last_name
attributes:
use Webstronauts\PersonName\PersonName;
class User extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'first_name', 'last_name',
];
/**
* Return a PersonName instance composed from the `first_name` and `last_name` attributes.
*
* @return PersonName
*/
public function getNameAttribute()
{
return new PersonName($this->first_name, $this->last_name);
}
/**
* Sets the `first_name` and `last_name` attributes from a full name.
*
* @param string $name
* @return void
*/
public function setNameAttribute($name)
{
$fullName = PersonName::make($name);
[$this->first_name, $this->last_name] = $fullName ? [$fullName->first, $fullName->last] : [null, null];
}
}
composer test
Detailed release notes for a given version can be found on our releases page.
As it's just a simple port of Ruby to PHP code. All credits should go to the Basecamp team and their name_of_person gem.
The MIT License (MIT). Please see License File for more information.