289 lines
7.8 KiB
PHP
Executable File
289 lines
7.8 KiB
PHP
Executable File
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// +----------------------------------------------------------------------
|
|
// | Author: liu21st <liu21st@gmail.com>
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace think\model\concern;
|
|
|
|
use think\Collection;
|
|
use think\Exception;
|
|
use think\Loader;
|
|
use think\Model;
|
|
use think\model\Collection as ModelCollection;
|
|
|
|
/**
|
|
* 模型数据转换处理
|
|
*/
|
|
trait Conversion
|
|
{
|
|
/**
|
|
* 数据输出显示的属性
|
|
* @var array
|
|
*/
|
|
protected $visible = [];
|
|
|
|
/**
|
|
* 数据输出隐藏的属性
|
|
* @var array
|
|
*/
|
|
protected $hidden = [];
|
|
|
|
/**
|
|
* 数据输出需要追加的属性
|
|
* @var array
|
|
*/
|
|
protected $append = [];
|
|
|
|
/**
|
|
* 数据集对象名
|
|
* @var string
|
|
*/
|
|
protected $resultSetType;
|
|
|
|
/**
|
|
* 设置需要附加的输出属性
|
|
* @access public
|
|
* @param array $append 属性列表
|
|
* @param bool $override 是否覆盖
|
|
* @return $this
|
|
*/
|
|
public function append(array $append = [], $override = false)
|
|
{
|
|
$this->append = $override ? $append : array_merge($this->append, $append);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 设置附加关联对象的属性
|
|
* @access public
|
|
* @param string $attr 关联属性
|
|
* @param string|array $append 追加属性名
|
|
* @return $this
|
|
* @throws Exception
|
|
*/
|
|
public function appendRelationAttr($attr, $append)
|
|
{
|
|
if (is_string($append)) {
|
|
$append = explode(',', $append);
|
|
}
|
|
|
|
$relation = Loader::parseName($attr, 1, false);
|
|
if (isset($this->relation[$relation])) {
|
|
$model = $this->relation[$relation];
|
|
} else {
|
|
$model = $this->getRelationData($this->$relation());
|
|
}
|
|
|
|
if ($model instanceof Model) {
|
|
foreach ($append as $key => $attr) {
|
|
$key = is_numeric($key) ? $attr : $key;
|
|
if (isset($this->data[$key])) {
|
|
throw new Exception('bind attr has exists:' . $key);
|
|
} else {
|
|
$this->data[$key] = $model->$attr;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 设置需要隐藏的输出属性
|
|
* @access public
|
|
* @param array $hidden 属性列表
|
|
* @param bool $override 是否覆盖
|
|
* @return $this
|
|
*/
|
|
public function hidden(array $hidden = [], $override = false)
|
|
{
|
|
$this->hidden = $override ? $hidden : array_merge($this->hidden, $hidden);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 设置需要输出的属性
|
|
* @access public
|
|
* @param array $visible
|
|
* @param bool $override 是否覆盖
|
|
* @return $this
|
|
*/
|
|
public function visible(array $visible = [], $override = false)
|
|
{
|
|
$this->visible = $override ? $visible : array_merge($this->visible, $visible);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 转换当前模型对象为数组
|
|
* @access public
|
|
* @return array
|
|
*/
|
|
public function toArray()
|
|
{
|
|
$item = [];
|
|
$visible = [];
|
|
$hidden = [];
|
|
|
|
// 合并关联数据
|
|
$data = array_merge($this->data, $this->relation);
|
|
|
|
// 过滤属性
|
|
if (!empty($this->visible)) {
|
|
$array = $this->parseAttr($this->visible, $visible);
|
|
$data = array_intersect_key($data, array_flip($array));
|
|
} elseif (!empty($this->hidden)) {
|
|
$array = $this->parseAttr($this->hidden, $hidden, false);
|
|
$data = array_diff_key($data, array_flip($array));
|
|
}
|
|
|
|
foreach ($data as $key => $val) {
|
|
if ($val instanceof Model || $val instanceof ModelCollection) {
|
|
// 关联模型对象
|
|
if (isset($visible[$key])) {
|
|
$val->visible( |