Attribute.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 InvalidArgumentException;
  13. use think\db\Expression;
  14. use think\Exception;
  15. use think\Loader;
  16. use think\model\Relation;
  17. trait Attribute
  18. {
  19. /**
  20. * 数据表主键 复合主键使用数组定义
  21. * @var string|array
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 数据表字段信息 留空则自动获取
  26. * @var array
  27. */
  28. protected $field = [];
  29. /**
  30. * JSON数据表字段
  31. * @var array
  32. */
  33. protected $json = [];
  34. /**
  35. * JSON数据取出是否需要转换为数组
  36. * @var bool
  37. */
  38. protected $jsonAssoc = false;
  39. /**
  40. * JSON数据表字段类型
  41. * @var array
  42. */
  43. protected $jsonType = [];
  44. /**
  45. * 数据表废弃字段
  46. * @var array
  47. */
  48. protected $disuse = [];
  49. /**
  50. * 数据表只读字段
  51. * @var array
  52. */
  53. protected $readonly = [];
  54. /**
  55. * 数据表字段类型
  56. * @var array
  57. */
  58. protected $type = [];
  59. /**
  60. * 当前模型数据
  61. * @var array
  62. */
  63. private $data = [];
  64. /**
  65. * 修改器执行记录
  66. * @var array
  67. */
  68. private $set = [];
  69. /**
  70. * 原始数据
  71. * @var array
  72. */
  73. private $origin = [];
  74. /**
  75. * 动态获取器
  76. * @var array
  77. */
  78. private $withAttr = [];
  79. /**
  80. * 获取模型对象的主键
  81. * @access public
  82. * @return string|array
  83. */
  84. public function getPk()
  85. {
  86. return $this->pk;
  87. }
  88. /**
  89. * 判断一个字段名是否为主键字段
  90. * @access public
  91. * @param string $key 名称
  92. * @return bool
  93. */
  94. protected function isPk($key)
  95. {
  96. $pk = $this->getPk();
  97. if (is_string($pk) && $pk == $key) {
  98. return true;
  99. } elseif (is_array($pk) && in_array($key, $pk)) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * 获取模型对象的主键值
  106. * @access public
  107. * @return integer
  108. */
  109. public function getKey()
  110. {
  111. $pk = $this->getPk();
  112. if (is_string($pk) && array_key_exists($pk, $this->data)) {
  113. return $this->data[$pk];
  114. }
  115. return;
  116. }
  117. /**
  118. * 设置允许写入的字段
  119. * @access public
  120. * @param array|string|true $field 允许写入的字段 如果为true只允许写入数据表字段
  121. * @return $this
  122. */
  123. public function allowField($field)
  124. {
  125. if (is_string($field)) {
  126. $field = explode(',', $field);
  127. }
  128. $this->field = $field;
  129. return $this;
  130. }
  131. /**
  132. * 设置只读字段
  133. * @access public
  134. * @param array|string $field 只读字段
  135. * @return $this
  136. */
  137. public function readonly($field)
  138. {
  139. if (is_string($field)) {
  140. $field = explode(',', $field);
  141. }
  142. $this->readonly = $field;
  143. return $this;
  144. }
  145. /**
  146. * 设置数据对象值
  147. * @access public
  148. * @param mixed $data 数据或者属性名
  149. * @param mixed $value 值
  150. * @return $this
  151. */
  152. public function data($data, $value = null)
  153. {
  154. if (is_string($data)) {
  155. $this->data[$data] = $value;
  156. return $this;
  157. }
  158. // 清空数据
  159. $this->data = [];
  160. if (is_object($data)) {
  161. $data = get_object_vars($data);
  162. }
  163. if ($this->disuse) {
  164. // 废弃字段
  165. foreach ((array) $this->disuse as $key) {
  166. if (array_key_exists($key, $data)) {
  167. unset($data[$key]);
  168. }
  169. }
  170. }
  171. if (true === $value) {
  172. // 数据对象赋值
  173. foreach ($data as $key => $value) {
  174. $this->setAttr($key, $value, $data);
  175. }
  176. } elseif (is_array($value)) {
  177. foreach ($value as $name) {
  178. if (isset($data[$name])) {
  179. $this->data[$name] = $data[$name];
  180. }
  181. }
  182. } else {
  183. $this->data = $data;
  184. }
  185. return $this;
  186. }
  187. /**
  188. * 批量设置数据对象值
  189. * @access public
  190. * @param mixed $data 数据
  191. * @param bool $set 是否需要进行数据处理
  192. * @return $this
  193. */
  194. public function appendData($data, $set = false)
  195. {
  196. if ($set) {
  197. // 进行数据处理
  198. foreach ($data as $key => $value) {
  199. $this->setAttr($key, $value, $data);
  200. }
  201. } else {
  202. if (is_object($data)) {
  203. $data = get_object_vars($data);
  204. }
  205. $this->data = array_merge($this->data, $data);
  206. }
  207. return $this;
  208. }
  209. /**
  210. * 获取对象原始数据 如果不存在指定字段返回null
  211. * @access public
  212. * @param string $name 字段名 留空获取全部
  213. * @return mixed
  214. */
  215. public function getOrigin($name = null)
  216. {
  217. if (is_null($name)) {
  218. return $this->origin;
  219. }
  220. return array_key_exists($name, $this->origin) ? $this->origin[$name] : null;
  221. }
  222. /**
  223. * 获取对象原始数据 如果不存在指定字段返回false
  224. * @access public
  225. * @param string $name 字段名 留空获取全部
  226. * @return mixed
  227. * @throws InvalidArgumentException
  228. */
  229. public function getData($name = null)
  230. {
  231. if (is_null($name)) {
  232. return $this->data;
  233. } elseif (array_key_exists($name, $this->data)) {
  234. return $this->data[$name];
  235. } elseif (array_key_exists($name, $this->relation)) {
  236. return $this->relation[$name];
  237. }
  238. throw new InvalidArgumentException('property not exists:' . static::class . '->' . $name);
  239. }
  240. /**
  241. * 获取变化的数据 并排除只读数据
  242. * @access public
  243. * @return array
  244. */
  245. public function getChangedData()
  246. {
  247. if ($this->force) {
  248. $data = $this->data;
  249. } else {
  250. $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) {
  251. if ((empty($a) || empty($b)) && $a !== $b) {
  252. return 1;
  253. }
  254. return is_object($a) || $a != $b ? 1 : 0;
  255. });
  256. }
  257. if (!empty($this->readonly)) {
  258. // 只读字段不允许更新
  259. foreach ($this->readonly as $key => $field) {
  260. if (isset($data[$field])) {
  261. unset($data[$field]);
  262. }
  263. }
  264. }
  265. return $data;
  266. }
  267. /**
  268. * 修改器 设置数据对象值
  269. * @access public
  270. * @param string $name 属性名
  271. * @param mixed $value 属性值
  272. * @param array $data 数据
  273. * @return void
  274. */
  275. public function setAttr($name, $value, $data = [])
  276. {
  277. if (isset($this->set[$name])) {
  278. return;
  279. }
  280. if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
  281. // 自动写入的时间戳字段
  282. $value = $this->autoWriteTimestamp($name);
  283. } else {
  284. // 检测修改器
  285. $method = 'set' . Loader::parseName($name, 1) . 'Attr';
  286. if (method_exists($this, $method)) {
  287. $value = $this->$method($value, array_merge($this->data, $data));
  288. $this->set[$name] = true;
  289. } elseif (isset($this->type[$name])) {
  290. // 类型转换
  291. $value = $this->writeTransform($value, $this->type[$name]);
  292. }
  293. }
  294. // 设置数据对象属性
  295. $this->data[$name] = $value;
  296. }
  297. /**
  298. * 是否需要自动写入时间字段
  299. * @access public
  300. * @param bool $auto
  301. * @return $this
  302. */
  303. public function isAutoWriteTimestamp($auto)
  304. {
  305. $this->autoWriteTimestamp = $auto;
  306. return $this;
  307. }
  308. /**
  309. * 自动写入时间戳
  310. * @access protected
  311. * @param string $name 时间戳字段
  312. * @return mixed
  313. */
  314. protected function autoWriteTimestamp($name)
  315. {
  316. if (isset($this->type[$name])) {
  317. $type = $this->type[$name];
  318. if (strpos($type, ':')) {
  319. list($type, $param) = explode(':', $type, 2);
  320. }
  321. switch ($type) {
  322. case 'datetime':
  323. case 'date':
  324. $value = $this->formatDateTime('Y-m-d H:i:s.u');
  325. break;
  326. case 'timestamp':
  327. case 'integer':
  328. default:
  329. $value = time();
  330. break;
  331. }
  332. } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
  333. 'datetime',
  334. 'date',
  335. 'timestamp',
  336. ])) {
  337. $value = $this->formatDateTime('Y-m-d H:i:s.u');
  338. } else {
  339. $value = time();
  340. }
  341. return $value;
  342. }
  343. /**
  344. * 数据写入 类型转换
  345. * @access protected
  346. * @param mixed $value 值
  347. * @param string|array $type 要转换的类型
  348. * @return mixed
  349. */
  350. protected function writeTransform($value, $type)
  351. {
  352. if (is_null($value)) {
  353. return;
  354. }
  355. if ($value instanceof Expression) {
  356. return $value;
  357. }
  358. if (is_array($type)) {
  359. list($type, $param) = $type;
  360. } elseif (strpos($type, ':')) {
  361. list($type, $param) = explode(':', $type, 2);
  362. }
  363. switch ($type) {
  364. case 'integer':
  365. $value = (int) $value;
  366. break;
  367. case 'float':
  368. if (empty($param)) {
  369. $value = (float) $value;
  370. } else {
  371. $value = (float) number_format($value, $param, '.', '');
  372. }
  373. break;
  374. case 'boolean':
  375. $value = (bool) $value;
  376. break;
  377. case 'timestamp':
  378. if (!is_numeric($value)) {
  379. $value = strtotime($value);
  380. }
  381. break;
  382. case 'datetime':
  383. $value = is_numeric($value) ? $value : strtotime($value);
  384. $value = $this->formatDateTime('Y-m-d H:i:s.u', $value);
  385. break;
  386. case 'object':
  387. if (is_object($value)) {
  388. $value = json_encode($value, JSON_FORCE_OBJECT);
  389. }
  390. break;
  391. case 'array':
  392. $value = (array) $value;
  393. case 'json':
  394. $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE;
  395. $value = json_encode($value, $option);
  396. break;
  397. case 'serialize':
  398. $value = serialize($value);
  399. break;
  400. }
  401. return $value;
  402. }
  403. /**
  404. * 获取器 获取数据对象的值
  405. * @access public
  406. * @param string $name 名称
  407. * @param array $item 数据
  408. * @return mixed
  409. * @throws InvalidArgumentException
  410. */
  411. public function getAttr($name, &$item = null)
  412. {
  413. try {
  414. $notFound = false;
  415. $value = $this->getData($name);
  416. } catch (InvalidArgumentException $e) {
  417. $notFound = true;
  418. $value = null;
  419. }
  420. // 检测属性获取器
  421. $fieldName = Loader::parseName($name);
  422. $method = 'get' . Loader::parseName($name, 1) . 'Attr';
  423. if (isset($this->withAttr[$fieldName])) {
  424. if ($notFound && $relation = $this->isRelationAttr($name)) {
  425. $modelRelation = $this->$relation();
  426. $value = $this->getRelationData($modelRelation);
  427. }
  428. $closure = $this->withAttr[$fieldName];
  429. $value = $closure($value, $this->data);
  430. } elseif (method_exists($this, $method)) {
  431. if ($notFound && $relation = $this->isRelationAttr($name)) {
  432. $modelRelation = $this->$relation();
  433. $value = $this->getRelationData($modelRelation);
  434. }
  435. $value = $this->$method($value, $this->data);
  436. } elseif (isset($this->type[$name])) {
  437. // 类型转换
  438. $value = $this->readTransform($value, $this->type[$name]);
  439. } elseif ($this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
  440. if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
  441. 'datetime',
  442. 'date',
  443. 'timestamp',
  444. ])) {
  445. $value = $this->formatDateTime($this->dateFormat, $value);
  446. } else {
  447. $value = $this->formatDateTime($this->dateFormat, $value, true);
  448. }
  449. } elseif ($notFound) {
  450. $value = $this->getRelationAttribute($name, $item);
  451. }
  452. return $value;
  453. }
  454. /**
  455. * 获取关联属性值
  456. * @access protected
  457. * @param string $name 属性名
  458. * @param array $item 数据
  459. * @return mixed
  460. */
  461. protected function getRelationAttribute($name, &$item)
  462. {
  463. $relation = $this->isRelationAttr($name);
  464. if ($relation) {
  465. $modelRelation = $this->$relation();
  466. if ($modelRelation instanceof Relation) {
  467. $value = $this->getRelationData($modelRelation);
  468. if ($item && method_exists($modelRelation, 'getBindAttr') && $bindAttr = $modelRelation->getBindAttr()) {
  469. foreach ($bindAttr as $key => $attr) {
  470. $key = is_numeric($key) ? $attr : $key;
  471. if (isset($item[$key])) {
  472. throw new Exception('bind attr has exists:' . $key);
  473. } else {
  474. $item[$key] = $value ? $value->getAttr($attr) : null;
  475. }
  476. }
  477. return false;
  478. }
  479. // 保存关联对象值
  480. $this->relation[$name] = $value;
  481. return $value;
  482. }
  483. }
  484. throw new InvalidArgumentException('property not exists:' . static::class . '->' . $name);
  485. }
  486. /**
  487. * 数据读取 类型转换
  488. * @access protected
  489. * @param mixed $value 值
  490. * @param string|array $type 要转换的类型
  491. * @return mixed
  492. */
  493. protected function readTransform($value, $type)
  494. {
  495. if (is_null($value)) {
  496. return;
  497. }
  498. if (is_array($type)) {
  499. list($type, $param) = $type;
  500. } elseif (strpos($type, ':')) {
  501. list($type, $param) = explode(':', $type, 2);
  502. }
  503. switch ($type) {
  504. case 'integer':
  505. $value = (int) $value;
  506. break;
  507. case 'float':
  508. if (empty($param)) {
  509. $value = (float) $value;
  510. } else {
  511. $value = (float) number_format($value, $param, '.', '');
  512. }
  513. break;
  514. case 'boolean':
  515. $value = (bool) $value;
  516. break;
  517. case 'timestamp':
  518. if (!is_null($value)) {
  519. $format = !empty($param) ? $param : $this->dateFormat;
  520. $value = $this->formatDateTime($format, $value, true);
  521. }
  522. break;
  523. case 'datetime':
  524. if (!is_null($value)) {
  525. $format = !empty($param) ? $param : $this->dateFormat;
  526. $value = $this->formatDateTime($format, $value);
  527. }
  528. break;
  529. case 'json':
  530. $value = json_decode($value, true);
  531. break;
  532. case 'array':
  533. $value = empty($value) ? [] : json_decode($value, true);
  534. break;
  535. case 'object':
  536. $value = empty($value) ? new \stdClass() : json_decode($value);
  537. break;
  538. case 'serialize':
  539. try {
  540. $value = unserialize($value);
  541. } catch (\Exception $e) {
  542. $value = null;
  543. }
  544. break;
  545. default:
  546. if (false !== strpos($type, '\\')) {
  547. // 对象类型
  548. $value = new $type($value);
  549. }
  550. }
  551. return $value;
  552. }
  553. /**
  554. * 设置数据字段获取器
  555. * @access public
  556. * @param string|array $name 字段名
  557. * @param callable $callback 闭包获取器
  558. * @return $this
  559. */
  560. public function withAttribute($name, $callback = null)
  561. {
  562. if (is_array($name)) {
  563. foreach ($name as $key => $val) {
  564. $key = Loader::parseName($key);
  565. $this->withAttr[$key] = $val;
  566. }
  567. } else {
  568. $name = Loader::parseName($name);
  569. $this->withAttr[$name] = $callback;
  570. }
  571. return $this;
  572. }
  573. }