dataset.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /**
  3. * The dataset class file of zin of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin\utils;
  12. /**
  13. * Manage dataset properties for html element and widgets.
  14. */
  15. class dataset implements \JsonSerializable
  16. {
  17. /**
  18. * Store dataset properties list in an array.
  19. *
  20. * @var array
  21. * @access protected
  22. */
  23. protected $storedData = array();
  24. /**
  25. * Parent node
  26. *
  27. * @access public
  28. * @var node
  29. */
  30. public $parent = null;
  31. /**
  32. * Create an instance, the initialed data can be passed.
  33. *
  34. * @access public
  35. * @param array|object|string $data Properties list array.
  36. * @param mixed $value Property value.
  37. */
  38. public function __construct($data = null, $value = null)
  39. {
  40. if($data !== null) $this->set($data, $value);
  41. }
  42. /**
  43. * Convert dataset to json string
  44. *
  45. * @access public
  46. * @return string
  47. */
  48. public function __toString()
  49. {
  50. return $this->toStr();
  51. }
  52. /**
  53. * Get debug info.
  54. *
  55. * @access public
  56. * @return string
  57. */
  58. public function __debugInfo()
  59. {
  60. return $this->storedData;
  61. }
  62. /**
  63. * Method for sub class to hook on setting it.
  64. *
  65. * @access protected
  66. * @param string $prop Property name or properties list.
  67. * @param mixed $value Property value.
  68. * @return dataset
  69. */
  70. protected function setVal($prop, $value)
  71. {
  72. $this->storedData[$prop] = $value;
  73. return $this;
  74. }
  75. /**
  76. * Method for sub class to hook on getting it.
  77. *
  78. * @access protected
  79. * @param string $prop Property name.
  80. * @return mixed
  81. */
  82. protected function getVal($prop)
  83. {
  84. return isset($this->storedData[$prop]) ? $this->storedData[$prop] : null;
  85. }
  86. /**
  87. * Get properties count.
  88. *
  89. * @access public
  90. * @param bool $skipEmpty Whether to skip to count empty value.
  91. * @return int
  92. */
  93. public function getCount($skipEmpty = false)
  94. {
  95. if(!$skipEmpty) return count($this->storedData);
  96. $count = 0;
  97. foreach($this->storedData as $value) if($value !== null) $count++;
  98. return $count;
  99. }
  100. /**
  101. * Get properties count.
  102. *
  103. * @deprecated Use getCount instead.
  104. * @access public
  105. * @param bool $skipEmpty Whether to skip to count empty value.
  106. * @return int
  107. */
  108. public function count($skipEmpty = false)
  109. {
  110. return $this->getCount($skipEmpty);
  111. }
  112. /**
  113. * Convert dataset to json string.
  114. *
  115. * @access public
  116. * @return string
  117. */
  118. public function toStr()
  119. {
  120. return json_encode($this->toArray());
  121. }
  122. /**
  123. * Convert dataset to array.
  124. *
  125. * @access public
  126. * @return array
  127. */
  128. public function toArray()
  129. {
  130. return $this->storedData;
  131. }
  132. /**
  133. * Convert dataset to json array.
  134. *
  135. * @deprecated Use toArray instead.
  136. * @access public
  137. * @return array
  138. */
  139. public function toJSON()
  140. {
  141. return $this->storedData;
  142. }
  143. /**
  144. * Serialized to JSON.
  145. * 序列化为 JSON。
  146. *
  147. * @access public
  148. * @return mixed
  149. */
  150. #[\ReturnTypeWillChange]
  151. public function jsonSerialize()
  152. {
  153. return $this->toArray();
  154. }
  155. /**
  156. * Set property, an array can be passed to set multiple properties.
  157. *
  158. * @access public
  159. * @param array|object|string|int $prop Property name or properties list.
  160. * @param mixed $value Property value.
  161. * @return dataset
  162. */
  163. public function set($prop, $value = null)
  164. {
  165. if(is_int($prop)) $prop = (string)$prop;
  166. if(is_string($prop)) return $this->setVal($prop, $value);
  167. if(is_object($prop)) $prop = get_object_vars($prop);
  168. if(is_array($prop))
  169. {
  170. foreach($prop as $name => $val) $this->set($name, $val);
  171. }
  172. return $this;
  173. }
  174. /**
  175. * Get property value by name, if the value not exists, return defaultValue.
  176. * If not property name passed, return all properties.
  177. *
  178. * @access public
  179. * @param string|array $prop Property name.
  180. * @param mixed $defaultValue Optional default value if actual value is null.
  181. * @return mixed
  182. */
  183. public function get($prop = null, $defaultValue = null)
  184. {
  185. if(is_null($prop)) return $this->storedData;
  186. if(is_array($prop))
  187. {
  188. $values = array();
  189. foreach($prop as $name)
  190. {
  191. $value = $this->getVal($name);
  192. $values[$name] = (is_null($value) && is_array($defaultValue)) ? (isset($defaultValue[$name]) ? $defaultValue[$name] : null) : $value;
  193. }
  194. return $values;
  195. }
  196. $val = $this->getVal($prop);
  197. return $val === null ? $defaultValue : $val;
  198. }
  199. /**
  200. * Add data to map.
  201. *
  202. * @access public
  203. * @param string $prop Property name.
  204. * @param object|mixed[] $map Map data.
  205. * @return dataset
  206. */
  207. public function addToMap($prop, $map)
  208. {
  209. if($map instanceof dataset) $map = $map->toArray();
  210. elseif(is_object($map)) $map = get_object_vars($map);
  211. elseif(!is_array($map)) return $this;
  212. $this->set($prop, array_merge($this->getMap($prop), $map));
  213. return $this;
  214. }
  215. /**
  216. * Get map value by name, if not exists, return an empty array.
  217. *
  218. * @access public
  219. * @param string $prop Property name.
  220. * @return array
  221. */
  222. public function getMap($prop)
  223. {
  224. return $this->get($prop, array());
  225. }
  226. /**
  227. * Remove value from map.
  228. *
  229. * @access public
  230. * @param string $prop Property name.
  231. * @param string $keys Map keys.
  232. * @return mixed
  233. */
  234. public function removeFromMap($prop, ...$keys)
  235. {
  236. $map = $this->getMap($prop);
  237. foreach($keys as $key) unset($map[$key]);
  238. return $this->setVal($prop, $map);
  239. }
  240. /**
  241. * Add value to array list.
  242. *
  243. * @access public
  244. * @param string $prop Property name.
  245. * @param mixed ...$item Item Value to add to list.
  246. * @return mixed
  247. * @param string|null $key
  248. */
  249. public function addToList($prop, $item, $key = null)
  250. {
  251. $list = $this->getList($prop);
  252. if($key) $list[$key] = $item;
  253. else $list[] = $item;
  254. return $this->set($prop, $list);
  255. }
  256. /**
  257. * Merge value to array list.
  258. *
  259. * @access public
  260. * @param string $prop Property name.
  261. * @param mixed $newList Values to add to list.
  262. * @return mixed
  263. */
  264. public function mergeToList($prop, $newList)
  265. {
  266. $list = $this->getList($prop);
  267. return $this->set($prop, array_merge($list, $newList));
  268. }
  269. /**
  270. * Get array list value by name, if not exists, return an empty array.
  271. *
  272. * @access public
  273. * @param string $prop Property name.
  274. * @return mixed
  275. */
  276. public function getList($prop)
  277. {
  278. return $this->get($prop, array());
  279. }
  280. /**
  281. * Get string value, if not value exists, return an empty string.
  282. *
  283. * @access public
  284. * @param string $prop Property name.
  285. * @return string
  286. */
  287. public function getStr($prop)
  288. {
  289. $val = $this->get($prop);
  290. if(is_null($val)) return '';
  291. return (string)$val;
  292. }
  293. /**
  294. * Append string to property.
  295. *
  296. * @access public
  297. * @param string $prop Property name.
  298. * @param string $str String to append.
  299. * @param string $joiner Joiner string.
  300. * @return dataset
  301. */
  302. public function appendToStr($prop, $str, $joiner = '')
  303. {
  304. $val = $this->getStr($prop);
  305. if(strlen($val) > 0) $val .= $joiner . $str;
  306. else $val = $str;
  307. return $this->set($prop, $val);
  308. }
  309. /**
  310. * Delete property by name.
  311. *
  312. * @access public
  313. * @param string $prop Property name.
  314. * @return dataset
  315. */
  316. public function remove($prop)
  317. {
  318. return $this->setVal($prop, null);
  319. }
  320. /**
  321. * Clear all properties.
  322. *
  323. * @access public
  324. * @return dataset
  325. */
  326. public function clear()
  327. {
  328. $this->storedData = array();
  329. return $this;
  330. }
  331. /**
  332. * Check whether the specified property is not null.
  333. *
  334. * @access public
  335. * @param string $prop Property name.
  336. * @return bool
  337. */
  338. public function has($prop)
  339. {
  340. return $this->getVal($prop) !== null;
  341. }
  342. /**
  343. * Check whether has specified property.
  344. *
  345. * @access public
  346. * @param string $prop Property name.
  347. * @return bool
  348. */
  349. public function isset($prop)
  350. {
  351. return isset($this->storedData[$prop]);
  352. }
  353. /**
  354. * Clone a new instance.
  355. *
  356. * @access public
  357. * @return object
  358. */
  359. public function copy()
  360. {
  361. $className = get_called_class();
  362. return new $className($this->storedData);
  363. }
  364. /**
  365. * Merge data to current dataset.
  366. *
  367. * @access public
  368. * @param array|object $data Data to merge.
  369. * @return dataset
  370. */
  371. public function merge($data)
  372. {
  373. if($data instanceof dataset) return $this->set($data->toArray());
  374. if(is_object($data) && isset($data->data)) return $this->set($data->data);
  375. return $this->set($data);
  376. }
  377. }