From 7d72c6743f7c47a326173cbb26e3871e68bbb810 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 30 May 2016 16:01:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Model=E7=B1=BB=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 170 +++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 73 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 0cb19cda..8ca196bb 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1034,45 +1034,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $value = $this->$method($value, $this->data); } elseif (isset($this->type[$name])) { // 类型转换 - $type = $this->type[$name]; - if (strpos($type, ':')) { - list($type, $param) = explode(':', $type, 2); - } - switch ($type) { - case 'integer': - $value = (int) $value; - break; - case 'float': - if (empty($param)) { - $value = (float) $value; - } else { - $value = (float) number_format($value, $param); - } - break; - case 'boolean': - $value = (bool) $value; - break; - case 'datetime': - if (!is_numeric($value)) { - $value = strtotime($value); - } - break; - case 'timestamp': - $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, is_numeric($value) ? $value : strtotime($value)); - break; - case 'object': - if (is_object($value)) { - $value = json_encode($value, JSON_FORCE_OBJECT); - } - break; - case 'json': - case 'array': - if (is_array($value)) { - $value = json_encode($value, JSON_UNESCAPED_UNICODE); - } - break; - } + $value = $this->writeTransform($value, $this->type[$name]); } } @@ -1084,6 +1046,56 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->data[$name] = $value; } + /** + * 数据写入 类型转换 + * @access public + * @param mixed $value 值 + * @param string $type 要转换的类型 + * @return mixed + */ + protected function writeTransform($value, $type) + { + if (strpos($type, ':')) { + list($type, $param) = explode(':', $type, 2); + } + switch ($type) { + case 'integer': + $value = (int) $value; + break; + case 'float': + if (empty($param)) { + $value = (float) $value; + } else { + $value = (float) number_format($value, $param); + } + break; + case 'boolean': + $value = (bool) $value; + break; + case 'datetime': + if (!is_numeric($value)) { + $value = strtotime($value); + } + break; + case 'timestamp': + $format = !empty($param) ? $param : $this->dateFormat; + $value = date($format, is_numeric($value) ? $value : strtotime($value)); + break; + case 'object': + if (is_object($value)) { + $value = json_encode($value, JSON_FORCE_OBJECT); + } + break; + case 'json': + case 'array': + if (is_array($value)) { + $value = json_encode($value, JSON_UNESCAPED_UNICODE); + } + break; + } + return $value; + } + /** * 获取器 获取数据对象的值 * @access public @@ -1100,40 +1112,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this->$method($value, $this->data); } elseif (!is_null($value) && isset($this->type[$name])) { // 类型转换 - $type = $this->type[$name]; - if (strpos($type, ':')) { - list($type, $param) = explode(':', $type, 2); - } - switch ($type) { - case 'integer': - $value = (int) $value; - break; - case 'float': - if (empty($param)) { - $value = (float) $value; - } else { - $value = (float) number_format($value, $param); - } - break; - case 'boolean': - $value = (bool) $value; - break; - case 'datetime': - $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, $value); - break; - case 'timestamp': - $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, strtotime($value)); - break; - case 'json': - case 'array': - $value = json_decode($value, true); - break; - case 'object': - $value = json_decode($value); - break; - } + $value = $this->readTransform($value, $this->type[$name]); } elseif (is_null($value) && method_exists($this, $name)) { // 获取关联数据 $value = $this->relation()->getRelation($name); @@ -1143,6 +1122,51 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $value; } + /** + * 数据读取 类型转换 + * @access public + * @param mixed $value 值 + * @param string $type 要转换的类型 + * @return mixed + */ + protected function readTransform($value, $type) + { + if (strpos($type, ':')) { + list($type, $param) = explode(':', $type, 2); + } + switch ($type) { + case 'integer': + $value = (int) $value; + break; + case 'float': + if (empty($param)) { + $value = (float) $value; + } else { + $value = (float) number_format($value, $param); + } + break; + case 'boolean': + $value = (bool) $value; + break; + case 'datetime': + $format = !empty($param) ? $param : $this->dateFormat; + $value = date($format, $value); + break; + case 'timestamp': + $format = !empty($param) ? $param : $this->dateFormat; + $value = date($format, strtotime($value)); + break; + case 'json': + case 'array': + $value = json_decode($value, true); + break; + case 'object': + $value = json_decode($value); + break; + } + return $value; + } + /** * 检测数据对象的值 * @access public