Conversion.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\model\concern;
  12. use think\Collection;
  13. use think\Exception;
  14. use think\Loader;
  15. use think\Model;
  16. use think\model\Collection as ModelCollection;
  17. /**
  18. * 模型数据转换处理
  19. */
  20. trait Conversion
  21. {
  22. /**
  23. * 数据输出显示的属性
  24. * @var array
  25. */
  26. protected $visible = [];
  27. /**
  28. * 数据输出隐藏的属性
  29. * @var array
  30. */
  31. protected $hidden = [];
  32. /**
  33. * 数据输出需要追加的属性
  34. * @var array
  35. */
  36. protected $append = [];
  37. /**
  38. * 数据集对象名
  39. * @var string
  40. */
  41. protected $resultSetType;
  42. /**
  43. * 设置需要附加的输出属性
  44. * @access public
  45. * @param array $append 属性列表
  46. * @param bool $override 是否覆盖
  47. * @return $this
  48. */
  49. public function append(array $append = [], $override = false)
  50. {
  51. $this->append = $override ? $append : array_merge($this->append, $append);
  52. return $this;
  53. }
  54. /**
  55. * 设置附加关联对象的属性
  56. * @access public
  57. * @param string $attr 关联属性
  58. * @param string|array $append 追加属性名
  59. * @return $this
  60. * @throws Exception
  61. */
  62. public function appendRelationAttr($attr, $append)
  63. {
  64. if (is_string($append)) {
  65. $append = explode(',', $append);
  66. }
  67. $relation = Loader::parseName($attr, 1, false);
  68. if (isset($this->relation[$relation])) {
  69. $model = $this->relation[$relation];
  70. } else {
  71. $model = $this->getRelationData($this->$relation());
  72. }
  73. if ($model instanceof Model) {
  74. foreach ($append as $key => $attr) {
  75. $key = is_numeric($key) ? $attr : $key;
  76. if (isset($this->data[$key])) {
  77. throw new Exception('bind attr has exists:' . $key);
  78. } else {
  79. $this->data[$key] = $model->getAttr($attr);
  80. }
  81. }
  82. }
  83. return $this;
  84. }
  85. /**
  86. * 设置需要隐藏的输出属性
  87. * @access public
  88. * @param array $hidden 属性列表
  89. * @param bool $override 是否覆盖
  90. * @return $this
  91. */
  92. public function hidden(array $hidden = [], $override = false)
  93. {
  94. $this->hidden = $override ? $hidden : array_merge($this->hidden, $hidden);
  95. return $this;
  96. }
  97. /**
  98. * 设置需要输出的属性
  99. * @access public
  100. * @param array $visible
  101. * @param bool $override 是否覆盖
  102. * @return $this
  103. */
  104. public function visible(array $visible = [], $override = false)
  105. {
  106. $this->visible = $override ? $visible : array_merge($this->visible, $visible);
  107. return $this;
  108. }
  109. /**
  110. * 转换当前模型对象为数组
  111. * @access public
  112. * @return array
  113. */
  114. public function toArray()
  115. {
  116. $item = [];
  117. $hasVisible = false;
  118. foreach ($this->visible as $key => $val) {
  119. if (is_string($val)) {
  120. if (strpos($val, '.')) {
  121. list($relation, $name) = explode('.', $val);
  122. $this->visible[$relation][] = $name;
  123. } else {
  124. $this->visible[$val] = true;
  125. $hasVisible = true;
  126. }
  127. unset($this->visible[$key]);
  128. }
  129. }
  130. foreach ($this->hidden as $key => $val) {
  131. if (is_string($val)) {
  132. if (strpos($val, '.')) {
  133. list($relation, $name) = explode('.', $val);
  134. $this->hidden[$relation][] = $name;
  135. } else {
  136. $this->hidden[$val] = true;
  137. }
  138. unset($this->hidden[$key]);
  139. }
  140. }
  141. // 合并关联数据
  142. $data = array_merge($this->data, $this->relation);
  143. foreach ($data as $key => $val) {
  144. if ($val instanceof Model || $val instanceof ModelCollection) {
  145. // 关联模型对象
  146. if (isset($this->visible[$key]) && is_array($this->visible[$key])) {
  147. $val->visible($this->visible[$key]);
  148. } elseif (isset($this->hidden[$key]) && is_array($this->hidden[$key])) {
  149. $val->hidden($this->hidden[$key]);
  150. }
  151. // 关联模型对象
  152. if (!isset($this->hidden[$key]) || true !== $this->hidden[$key]) {
  153. $item[$key] = $val->toArray();
  154. }
  155. } elseif (isset($this->visible[$key])) {
  156. $item[$key] = $this->getAttr($key);
  157. } elseif (!isset($this->hidden[$key]) && !$hasVisible) {
  158. $item[$key] = $this->getAttr($key);
  159. }
  160. }
  161. // 追加属性(必须定义获取器)
  162. if (!empty($this->append)) {
  163. foreach ($this->append as $key => $name) {
  164. if (is_array($name)) {
  165. // 追加关联对象属性
  166. $relation = $this->getRelation($key);
  167. if (!$relation) {
  168. $relation = $this->getAttr($key);
  169. if ($relation) {
  170. $relation->visible($name);
  171. }
  172. }
  173. $item[$key] = $relation ? $relation->append($name)->toArray() : [];
  174. } elseif (strpos($name, '.')) {
  175. list($key, $attr) = explode('.', $name);
  176. // 追加关联对象属性
  177. $relation = $this->getRelation($key);
  178. if (!$relation) {
  179. $relation = $this->getAttr($key);
  180. if ($relation) {
  181. $relation->visible([$attr]);
  182. }
  183. }
  184. $item[$key] = $relation ? $relation->append([$attr])->toArray() : [];
  185. } else {
  186. $item[$name] = $this->getAttr($name, $item);
  187. }
  188. }
  189. }
  190. return $item;
  191. }
  192. /**
  193. * 转换当前模型对象为JSON字符串
  194. * @access public
  195. * @param integer $options json参数
  196. * @return string
  197. */
  198. public function toJson($options = JSON_UNESCAPED_UNICODE)
  199. {
  200. return json_encode($this->toArray(), $options);
  201. }
  202. /**
  203. * 移除当前模型的关联属性
  204. * @access public
  205. * @return $this
  206. */
  207. public function removeRelation()
  208. {
  209. $this->relation = [];
  210. return $this;
  211. }
  212. public function __toString()
  213. {
  214. return $this->toJson();
  215. }
  216. // JsonSerializable
  217. public function jsonSerialize()
  218. {
  219. return $this->toArray();
  220. }
  221. /**
  222. * 转换数据集为数据集对象
  223. * @access public
  224. * @param array|Collection $collection 数据集
  225. * @param string $resultSetType 数据集类
  226. * @return Collection
  227. */
  228. public function toCollection($collection, $resultSetType = null)
  229. {
  230. $resultSetType = $resultSetType ?: $this->resultSetType;
  231. if ($resultSetType && false !== strpos($resultSetType, '\\')) {
  232. $collection = new $resultSetType($collection);
  233. } else {
  234. $collection = new ModelCollection($collection);
  235. }
  236. return $collection;
  237. }
  238. }