mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 23:02:48 +08:00
优化Model类代码
This commit is contained in:
@@ -1034,45 +1034,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$value = $this->$method($value, $this->data);
|
$value = $this->$method($value, $this->data);
|
||||||
} elseif (isset($this->type[$name])) {
|
} elseif (isset($this->type[$name])) {
|
||||||
// 类型转换
|
// 类型转换
|
||||||
$type = $this->type[$name];
|
$value = $this->writeTransform($value, $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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1084,6 +1046,56 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$this->data[$name] = $value;
|
$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
|
* @access public
|
||||||
@@ -1100,40 +1112,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return $this->$method($value, $this->data);
|
return $this->$method($value, $this->data);
|
||||||
} elseif (!is_null($value) && isset($this->type[$name])) {
|
} elseif (!is_null($value) && isset($this->type[$name])) {
|
||||||
// 类型转换
|
// 类型转换
|
||||||
$type = $this->type[$name];
|
$value = $this->readTransform($value, $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;
|
|
||||||
}
|
|
||||||
} elseif (is_null($value) && method_exists($this, $name)) {
|
} elseif (is_null($value) && method_exists($this, $name)) {
|
||||||
// 获取关联数据
|
// 获取关联数据
|
||||||
$value = $this->relation()->getRelation($name);
|
$value = $this->relation()->getRelation($name);
|
||||||
@@ -1143,6 +1122,51 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return $value;
|
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
|
* @access public
|
||||||
|
|||||||
Reference in New Issue
Block a user