Hosmel Quintana
Introduction
Installation
Basic usage
Creating Names
Formatting Names
Eloquent Casts
View on GitHub

Formatting Names

Format a name as a full, familiar, abbreviated, or sorted name, as initials or a mention handle, or in its possessive form.

Each format is a method that returns a string. Without a last name, the full, familiar, abbreviated, and sorted forms are all the first name:

MethodDavid Heinemeier HanssonCher
full()David Heinemeier HanssonCher
familiar()David H.Cher
abbreviated()D. Heinemeier HanssonCher
sorted()Heinemeier Hansson, DavidCher
initials()DHHC
mentionable()davidhcher
possessive()David Heinemeier Hansson'sCher's

familiar() shortens the last name to its initial, and abbreviated() shortens the first name instead. sorted() puts the last name first, for lists ordered by last name.

Initials

initials() takes the first letter of each word, and skips text in parentheses or square brackets, such as a company name:

php
use HosmelQ\NameOfPerson\PersonName;

new PersonName('Conor', 'Muirhead [Basecamp]')->initials(); // "CM"
new PersonName('Mary Jane', 'Watson')->initials(); // "MJW"

Mention handles

mentionable() lowercases the familiar name and removes its spaces and final period, for @-mentions:

php
new PersonName('David', 'Heinemeier Hansson')->mentionable(); // "davidh"

Possessives

possessive() adds 's to the full name, or only an apostrophe when the name ends in "s":

php
new PersonName('David', 'Heinemeier Hansson')->possessive(); // "David Heinemeier Hansson's"
new PersonName('James')->possessive(); // "James'"

To make another form possessive, pass full, first, last, abbreviated, sorted, or initials:

php
$name = new PersonName('David', 'Heinemeier Hansson');

$name->possessive('first'); // "David's"
$name->possessive('abbreviated'); // "D. Heinemeier Hansson's"
$name->possessive('initials'); // "DHH's"

Without a last name, last uses the first name. Any other value throws InvalidArgumentException.

Creating NamesEloquent Casts