*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, 漢字拼音相關參考資料
版權聲明:本文爲博主原創文章,商業轉載請聯繫作者獲得授權,非商業轉載請註明出處