Skip to content

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

License

Notifications You must be signed in to change notification settings

open-source-contributions/php-person-name

Repository files navigation

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"

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 = [
        '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

Please see CHANGELOG for more information on what has changed recently.

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages