/php-person-name

Presenting names of people in full, familiar, abbreviated, and initialized forms (but without titulation etc).

Primary LanguagePHPMIT LicenseMIT

PersonName

Latest Version on Packagist Build Status Code Coverage Quality Score StyleCI Total Downloads

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.

Installation

You can install the package via Composer.

composer require webstronauts/person-name

Usage

$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

Laravel

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];
    }
}

Testing

composer test

Changelog

Detailed release notes for a given version can be found on our releases page.

Credits

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.

License

The MIT License (MIT). Please see License File for more information.