taylornetwork/name-formatter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

taylornetwork/name-formatter

Composer 安装命令:

composer require taylornetwork/name-formatter

包简介

A name formatter for Laravel

README 文档

README

A customizable name formatter class for Laravel.

Install

Via Composer

$ composer require taylornetwork/name-formatter

Publish Config

$ php artisan vendor:publish

This will add nameformatter.php to your config directory

Usage

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.

Trait

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;

Override Formatter Config

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');
	}
}

Available Methods

format ()

Returns formatted name.

map (string | array $field, string | null $modelField)

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();

style (string $style)

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

Examples
$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'

Config

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.

Credits

License

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

taylornetwork/name-formatter 适用场景与选型建议

taylornetwork/name-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 11 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 taylornetwork/name-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 taylornetwork/name-formatter 我们能提供哪些服务?
定制开发 / 二次开发

基于 taylornetwork/name-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 65
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-18