//通过方法添加属性默认值也是可以的! 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; }], ]; }
YII之模型添加默认属性值
最新推荐文章于 2021-10-21 08:19:25 发布