对yii2 扩展behavior理解(PinyinBehavior篇) [ 新手入门 ]
*PinyinBehavior: 自动填充指定的属性, 可把指定属性中的汉字翻译成拼音
PinyinBehavior行为类: 首先要 composer require "overtrue/pinyin:~3.0"
namespace common\behaviors;
use yii\base\InvalidConfigException;
use yii\behaviors\AttributeBehavior;
use yii\db\BaseActiveRecord;
use Overtrue\Pinyin\Pinyin;
/**
* Class PinyinBehavior
* @package common\behaviors
*/
class PinyinBehavior extends AttributeBehavior
{
/**
* @var string 拼音音调:不带音调输出: mei hao
*/
public const TONE_NONE = 'none';
/**
* @var string 拼音音调:带数字式音调: mei3 hao3
*/
public const TONE_ASCII = 'ascii';
/**
* @var string 拼音音调:UNICODE 式音调:měi hǎo
*/
public const TONE_UNICODE = 'unicode';
/**
* $pinyin->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lv-xing
* $pinyin->permalink('带着希望去旅行','.'); // dai.zhe.xi.wang.qu.lv.xing
* @var string 转换方式:生成用于链接的拼音字符串
*/
public const TYPE_PERMALINK = 'permalink';
/**
* $pinyin->abbr('带着希望去旅行'); // dzxwqlx
* $pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x
* @var string 转换方式:获取首字符字符串
*/
public const TYPE_ABBR = 'abbr';
/**
* 没有音调: $pinyin->sentence('带着希望去旅行,比到达终点更美好!'); // dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao!
* 有音调: $pinyin->sentence('带着希望去旅行,比到达终点更美好!', true); // dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
* @var string 转换方式:翻译整段文字为拼音:将会保留中文字符:,。 ! ? : “ ” ‘ ’ 并替换为对应的英文符号
*/
public const TYPE_SENTENCE = 'sentence';
/**
* $pinyin->name('单某某'); // ['shan', 'mou', 'mou']
* @var string 转换方式:翻译姓名: 姓名的姓的读音有些与普通字不一样,比如 ‘单’ 常见的音为 dan,而作为姓的时候读 shan
*/
public const TYPE_NAME = 'name';
/**
* @var string 将接收attribute值转换成拼音的属性
*/
public $slugAttribute = 'slug';
/**
* @var string|array 属性或属性列表,其值将被转换为拼音
*/
public $attribute = 'name';
/**
* @var string 转换方式
*/
public $type = self::TYPE_ABBR;
/**
* @var string 拼音音调
*/
public $tone = self::TONE_NONE;
/**
* @var string 拼音之间的分隔符,默认为空
*/
public $delimiter = '';
/**
* @var callable|string|null the value that will be used as a slug. This can be an anonymous function
* or an arbitrary value or null. If the former, the return value of the function will be used as a slug.
* If `null` then the `$attribute` property will be used to generate a slug.
* The signature of the function should be as follows,
*
* ```php
* function ($event)
* {
* // return slug
* }
* ```
*/
public $value;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty($this->attributes)) {
$this->attributes = [
BaseActiveRecord::EVENT_BEFORE_INSERT => $this->slugAttribute,
BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->slugAttribute,
];
}
if ($this->attribute === null && $this->value === null) {
throw new InvalidConfigException('Either "attribute" or "value" property must be specified.');
}
}
/**
* @inheritdoc
*/
protected function getValue($event)
{
$pinyin = new Pinyin();
switch ($this->type) {
case self::TYPE_PERMALINK:
default:
return $pinyin->permalink($this->owner->{$this->attribute}, $this->delimiter);
break;
case self::TYPE_ABBR:
return $pinyin->abbr($this->owner->{$this->attribute}, $this->delimiter);
break;
case self::TYPE_SENTENCE:
return $pinyin->sentence($this->owner->{$this->attribute});
break;
case self::TYPE_NAME:
return $pinyin->name($this->owner->{$this->attribute}, $this->tone);
break;
}
}
}
*Model中使用:
use common\behaviors\PinyinBehavior;
public function behavior()
{
return [
[
// 汉字翻译成拼音
'class' => PinyinBehavior::class,
'attribute' => 'name', // 要翻译的字段
'slugAttribute' => 'slug_name', // 翻译后所保存的字段
'type' => PinyinBehavior::TYPE_PERMALINK, // 转换方式
'tone' => PinyinBehavior::TONE_UNICODE, // 音调相关
'delimiter' => '-', // 拼音之间的连接符
],
];
}
参考文章: overtrue/pinyin, 汉字拼音相关参考资料
共 1 条回复
zhoupenghui
注册时间:2016-04-29
最后登录:2020-08-20
在线时长:44小时43分
最后登录:2020-08-20
在线时长:44小时43分
- 粉丝3
- 金钱870
- 威望120
- 积分2510