YII之模型添加默认属性值

//通过方法添加属性默认值也是可以的!
public function rules()
{
    return [
        [['app_id', 'alipay_public_key', 'app_public_key', 'app_private_key', 'cs_tnt_inst_id', 'cs_scene'], 'trim'],
        [['app_id', 'alipay_public_key', 'app_private_key'], 'required'],
        ['alipay_public_key', function ($attr) {
            // key自动添加 -----BEGIN PUBLIC KEY-----&&-----END PUBLIC KEY-----&&换行符

            $begin_str = '-----BEGIN PUBLIC KEY-----';
            $end_str = '-----END PUBLIC KEY-----';
            $key = $this->alipay_public_key;

            $key = $this->pregReplaceAll('/---.*---/', '', $key);
            $key = trim($key);
            $key = str_replace("\n", '', $key);
            $key = str_replace("\r\n", '', $key);
            $key = str_replace("\r", '', $key);
            $key = wordwrap($key, 64, "\r\n", true);

            if (mb_stripos($key, $begin_str) === false) {
                $key = $begin_str . "\r\n" . $key;
            }
            if (mb_stripos($key, $end_str) === false) {
                $key = $key . "\r\n" . $end_str;
            }
            $this->alipay_public_key = $key;
        }],
        ['app_private_key', function ($attr) {
            // key自动添加 -----BEGIN RSA PRIVATE KEY-----&&-----END RSA PRIVATE KEY-----&&换行符
            $begin_str = '-----BEGIN RSA PRIVATE KEY-----';
            $end_str = '-----END RSA PRIVATE KEY-----';
            $key = $this->app_private_key;

            $key = $this->pregReplaceAll('/---.*---/', '', $key);
            $key = trim($key);
            $key = str_replace("\n", '', $key);
            $key = str_replace("\r\n", '', $key);
            $key = str_replace("\r", '', $key);
            $key = wordwrap($key, 64, "\r\n", true);

            if (mb_stripos($key, $begin_str) === false) {
                $key = $begin_str . "\r\n" . $key;
            }
            if (mb_stripos($key, $end_str) === false) {
                $key = $key . "\r\n" . $end_str;
            }
            $this->app_private_key = $key;
        }],
    ];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值