A customizable name formatter class for Laravel.
Via Composer
$ composer require taylornetwork/name-formatter
$ php artisan vendor:publish
This will add nameformatter.php
to your config
directory
Create a new instance of the class by passing it an instance of Illuminate\Eloquent\Model
and call format()
Let's say we have a Customer model at App\Customer
use Illuminate\Eloquent\Model;
class Customer extends Model
{
protected $fillable = [
'first_name', 'last_name', 'address',
];
}
We want to add a fullName
attribute to our customer.
We can create a getAttribute method Laravel will use to create the attribute for us.
See Laravel Eloquent Documentation for more information.
use Illuminate\Eloquent\Model;
use TaylorNetwork\Formatters\Name\Formatter;
class Customer extends Model
{
protected $fillable = [
'first_name', 'last_name', 'address',
];
public function getFullNameAttribute()
{
$formatter = new Formatter($this);
return $formatter->format();
}
}
Get the customer's full name
$customer = App\Customer::create([
'first_name' => 'John',
'last_name' => 'Doe',
'address' => '123 Main Street'
]);
echo $customer->fullName;
Returns
'John Doe'
By default the Formatter
class will concatenate the first_name
attribute, a space and the last_name
attribute.
This package includes a trait you can add to your model that will add a fullName
attribute.
use Illuminate\Eloquent\Model;
use TaylorNetwork\Formatters\Name\FormatsFullName;
class Customer extends Model
{
use FormatsFullName;
protected $fillable = [
'first_name', 'last_name', 'address',
];
}
You can then access the full name using the default configuration by:
echo $customer->fullName;
You can override the formatter config when using the trait by overriding the formatterConfig
method in your model
use Illuminate\Eloquent\Model;
use TaylorNetwork\Formatters\Name\FormatsFullName;
class Customer extends Model
{
use FormatsFullName;
protected $fillable = [
'first_name', 'last_name', 'address',
];
public function formatterConfig(&$formatter)
{
$formatter->style('L, F');
}
}
Returns formatted name.
By default the Formatter
class will look for a first_name
and last_name
attribute on the model it was passed. If the model you are passing it has different attribute names fot first and last names you can either pass an associative array to the map
function or two strings.
For a model with a first name attribute named fName
and a last name attribute named lName
$formatter = new TaylorNetwork\Formatters\Name\Formatter($model);
$formatter->map([ 'first_name' => 'fName', 'last_name' => 'lName' ])->format();
// OR
$formatter->map('first_name', 'fName')->map('last_name', 'lName')->format();
By default the Formatter
class will format names as $first_name . ' ' . $last_name
You can override the style with the style
function. style
accepts a string with the formatting you would like Formatter
to use
Key | Description | Example: 'John Doe' |
---|---|---|
'F' |
The full first name | 'John' |
'L' |
The full last name | 'Doe' |
'f' |
The first initial | 'J' |
'l' |
The last initial | 'D' |
Any other characters in the string will appear in the formatted name
$model->first_name = 'John';
$model->last_name = 'Doe';
$formatter = new TaylorNetwork\Formatters\Name\Formatter($model);
To get the first initial and last name
$formatter->style('f L')->format();
// Returns
'J Doe'
To get the last initial, the first name
$formatter->style('l, F')->format();
// Returns
'D, John'
To get the first and last initials
$formatter->style('fl')->format();
// Returns
'JD'
You can even add other characters to the style string
$formatter->style('F_L')->format();
// Returns
'John_Doe'
Every key in the string is replaced, so you could do something like this. (Though I don't know why you would)
$formatter->style('fffF lllL')->format();
// Returns
'JJJJohn DDDDoe'
Once you run php artisan vendor:publish
the config file nameformatter.php
will be in your config
directory. There you can set the defaults you want in terms of format style, field map, etc.
- Author: Sam Taylor
The MIT License (MIT). Please see License File for more information.