12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/6/20
- * Time: 下午1:45
- */
- namespace app\source\model;
- /**
- * Class InitUpdate
- * @package app\source\model
- */
- class InitUpdate
- {
- //设置的属性与方法映射
- public $fieldsMethod = [];
- /**
- * 绑定参数
- * @param $data
- * @return static
- */
- public function bind($data)
- {
- foreach ($data as $key => $value) {
- if (array_key_exists($key, $this->fieldsMethod)) {
- $this->$key = $value;
- }
- }
- return $this;
- }
- /**
- * 设置属性
- * @param $name
- * @param $value
- * @return $this
- */
- public function __set($name, $value)
- {
- if (is_array($value)) {
- $value = json_encode($value, JSON_UNESCAPED_UNICODE);
- }
- if ($this->fieldsMethod && array_key_exists($name, $this->fieldsMethod)) {
- if (method_exists($this, $this->fieldsMethod[$name])) {
- call_user_func([$this, $this->fieldsMethod[$name]], $value);
- } else {
- $this->$name = (int)$value;
- }
- }
- return $this;
- }
- /**
- * 转化为数组
- * @return mixed
- */
- public function toArray()
- {
- $data = [];
- foreach ($this as $field => $value) {
- if ($field == 'fieldsMethod') {
- continue;
- }
- $data[$field] = $value;
- }
- return $data;
- }
- }
|