PHP code example of hosmelq / name-of-person

1. Go to this page and download the library: Download hosmelq/name-of-person library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hosmelq / name-of-person example snippets


use HosmelQ\NameOfPerson\PersonName;

// Direct instantiation with first and last name.
$name = new PersonName('David', 'Heinemeier Hansson');

// From full name strings.
$parsed = PersonName::fromFull('Jason Fried');

echo $parsed->first; // "Jason"
echo $parsed->last;  // "Fried"

// Handles single names.
$single = PersonName::fromFull('Cher');

echo $single->first; // "Cher"
echo $single->last;  // null

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

echo $name->first; // "David"

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

echo $name->last; // "Heinemeier Hansson"

$single = PersonName::fromFull('Cher');

echo $single->last; // null

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

$single = PersonName::fromFull('Cher');

echo $name->abbreviated();   // "D. Heinemeier Hansson"
echo $single->abbreviated(); // "Cher"

echo $name->familiar();   // "David H."
echo $single->familiar(); // "Cher"

echo $name->full(); // "David Heinemeier Hansson"

echo $name->initials(); // "DHH"

$complex = new PersonName('Mary Jane', 'Watson');

echo $complex->initials(); // "MJW"

echo $name->mentionable();   // "davidh"
echo $single->mentionable(); // "cher"

echo $name->possessive(); // "David Heinemeier Hansson's"

$james = new PersonName('James', null);

echo $james->possessive(); // "James'"

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

echo $name->sorted();   // "Heinemeier Hansson, David"
echo $single->sorted(); // "Cher"

$name1 = new PersonName('David', 'Heinemeier Hansson');
$name2 = new PersonName('David', 'Heinemeier Hansson');
$name3 = new PersonName('Jason', 'Fried');

echo $name1->equals($name2); // true
echo $name1->equals($name3); // false

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

// String conversion returns full name
echo (string) $name; // "David Heinemeier Hansson"

// JSON serialization returns full name
echo json_encode($name); // "David Heinemeier Hansson"

// Empty first name
new PersonName(''); // throws InvalidArgumentException

// Invalid possessive method
$name->possessive('invalid'); // throws InvalidArgumentException

$name = new PersonName('José', 'García');

echo $name->familiar(); // "José G."

use HosmelQ\NameOfPerson\PersonNameCast;

// Default configuration - uses first_name and last_name columns
class User extends Model
{
    protected function casts(): array
    {
        return [
            'name' => PersonNameCast::class,
        ];
    }
}

// Custom column names
class BlogPost extends Model
{
    protected function casts(): array
    {
        return [
            'author_name' => PersonNameCast::class.':author_first,author_last',
        ];
    }
}

class BlogPost extends Model
{
    protected function casts(): array
    {
        return [
            'author_name' => PersonNameCast::using('author_first', 'author_last'),
        ];
    }
}

$user = new User();

$user->name = 'David Heinemeier Hansson';

echo $user->name->familiar(); // "David H."

$user = User::find(1);

return response()->json([
    'user' => $user->name, // "David Heinemeier Hansson"
]);