您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20170110|thinkphp5-Model.php-3

2016-12-07 08:50 435 查看
/**
* 自动写入时间戳
* @access public
* @param string         $name 时间戳字段
* @return mixed
*/
protected function autoWriteTimestamp($name)
{// 自动写入 时间戳
if (isset($this->type[$name])) {// 如果当期时间戳字段 拥有 类型
$type = $this->type[$name];// 获取当前字段 时间类型
if (strpos($type, ':')) {// 如果是冒号形式 则 后面的是参数
list($type, $param) = explode(':', $type, 2);
}
switch ($type) {
case 'datetime':// 时间类型
case 'date':// 日期类型
$format = !empty($param) ? $param : $this->dateFormat;// 获取格式化信息
$value  = date($format, $_SERVER['REQUEST_TIME']);// 获取 结果
break;
case 'timestamp':
case 'int':
$value = $_SERVER['REQUEST_TIME'];// 直接获取结果
break;
}
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) {
$value = date($this->dateFormat, $_SERVER['REQUEST_TIME']);// 不区分字段 的情况
} else {
$value = $_SERVER['REQUEST_TIME'];// 如果不区分字段,默认的情况 就是 设置值
}
return $value;
}

/**
* 数据写入 类型转换
* @access public
* @param mixed         $value 值
* @param string|array  $type 要转换的类型
* @return mixed
*/
protected function writeTransform($value, $type)// 数据写入 类型转换
{
if (is_array($type)) {// 如果类型是数组
list($type, $param) = $type;// 列表 显示 这些信息
} elseif (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 'timestamp':
if (!is_numeric($value)) {// 时间戳类型
$value = strtotime($value);
}
break;
case 'datetime':// 时间格式化类型
$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);// json 格式化 对象
}
break;
case 'array':
$value = (array) $value;// 转化为数组
case 'json':
$option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE;// 编码json 对象
$value  = json_encode($value, $option);
break;
case 'serialize':
$value = serialize($value);// 序列化
break;
}
return $value;
}

/**
* 获取器 获取数据对象的值
* @access public
* @param string $name 名称
* @return mixed
* @throws InvalidArgumentException
*/
public function getAttr($name)// 获取器,获取数据对象的值
{
try {
$notFound = false;// 默认设置获取成功
$value    = $this->getData($name);// 进行获取动作
} catch (InvalidArgumentException $e) {
$notFound = true;
$value    = null;
}

// 检测属性获取器
$method = 'get' . Loader::parseName($name, 1) . 'Attr';// 获取 函数名字
if (method_exists($this, $method)) {
$value = $this->$method($value, $this->data);// 执行 相应 的函数,
} elseif (isset($this->type[$name])) {
// 类型转换
$value = $this->readTransform($value, $this->type[$name]);// 读取数据
} elseif ($notFound) {// 如果没有找到
if (method_exists($this, $name) && !method_exists('\think\Model', $name)) {
// 不存在该字段 获取关联数据
$value = $this->relation()->getRelation($name);// 获取关联数据
// 保存关联对象值
$this->data[$name] = $value;//保存数据
} else {
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
}
}
return $value;// 返回获取的值
}

/**
* 数据读取 类型转换
* @access public
* @param mixed         $value 值
* @param string|array  $type 要转换的类型
* @return mixed
*/
protected function readTransform($value, $type)
{
if (is_array($type)) {
list($type, $param) = $type;
} elseif (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 'timestamp':
$format = !empty($param) ? $param : $this->dateFormat;
$value  = date($format, $value);
break;
case 'datetime':
$format = !empty($param) ? $param : $this->dateFormat;
$value  = date($format, strtotime($value));
break;
case 'json':
$value = json_decode($value, true);
break;
case 'array':
$value = is_null($value) ? [] : json_decode($value, true);
break;
case 'object':
$value = empty($value) ? new \stdClass() : json_decode($value);
break;
case 'serialize':
$value = unserialize($value);
break;
}
return $value;
}// 同上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息