StlUtil.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of StlUtil
  5. *
  6. * @author jgao
  7. */
  8. class StlUtil
  9. {
  10. //put your code here
  11. /**
  12. * 字典是否存在某元素
  13. * @param associat_array $dict
  14. * @param type $property
  15. */
  16. public static function dictHasProperty($dict, $property)
  17. {
  18. return property_exists($dict, "" . $property);
  19. }
  20. /**
  21. * 移除字典元素
  22. * @param associat_array $dict
  23. * @param type $property
  24. */
  25. public static function dictRemove($dict, $property)
  26. {
  27. unset($dict->$property);
  28. }
  29. /**
  30. * 获取字典全部属性
  31. * @param associat_array $dict
  32. * @return array
  33. */
  34. public static function dictPropertys($dict)
  35. {
  36. $propertys = array();
  37. foreach ($dict as $key => $val) {
  38. $propertys[] = $key;
  39. }
  40. return $propertys;
  41. }
  42. /**
  43. * 随机字典任意属性名
  44. * @param associat_array $dict
  45. */
  46. public static function dictPropertyRandom($dict)
  47. {
  48. return self::arrayRandom(self::dictPropertys($dict));
  49. }
  50. /**
  51. * 随机字典任一属性值
  52. * @param type $dict
  53. * @return obj
  54. */
  55. public static function dictRandom($dict)
  56. {
  57. $key = self::arrayRandom(self::dictPropertys($dict));
  58. return $dict->$key;
  59. }
  60. /**
  61. * 字典转数组
  62. * @param associat_array $dict 目标字典
  63. * @param type $mod 0.值转换;1.键转换;2.部分属性转换
  64. * @param type $propertyList 属性列表【数组】,仅$mod==2时生效
  65. * @return type
  66. */
  67. public static function dictToArray($dict, $mod = 0, $propertyList = null)
  68. {
  69. $array = array();
  70. foreach ($dict as $key => $val) {
  71. if ($mod == 0) { // 值转数组
  72. $array[] = $val;
  73. } elseif ($mod == 1) { // key转数组
  74. $array[] = $key;
  75. } else { // 单属性转数组
  76. $item = new \stdClass();
  77. foreach ($propertyList as $property) {
  78. if (self::dictHasProperty($dict, $property)) {
  79. $item->$property = $dict->$property;
  80. }
  81. }
  82. $array[] = $item;
  83. }
  84. }
  85. return $array;
  86. }
  87. /**
  88. * 创建数组,并将传入的参数压入数组
  89. * @param 可选...args
  90. */
  91. public static function arrayInit(/* $arg1, $arg2...$argN */)
  92. {
  93. $array = array();
  94. $items = func_get_args();
  95. foreach ($items as $item) {
  96. $array[] = $item;
  97. }
  98. return $array;
  99. }
  100. /**
  101. * 数组新增
  102. * @param array $array
  103. * @param type $item
  104. */
  105. public static function arrayPush(&$array, $item)
  106. {
  107. $array[] = $item;
  108. }
  109. /**
  110. * 获取数组元素
  111. * @param type $array
  112. * @param type $index
  113. */
  114. public static function arrayGet(&$array, $index)
  115. {
  116. return $array[$index];
  117. }
  118. /**
  119. * 设置数组元素
  120. * @param type $array
  121. * @param type $index
  122. * @param type $item
  123. * @return type
  124. */
  125. public static function arraySet(&$array, $index, $item)
  126. {
  127. if ($index >= count($array)) {
  128. $array[] = $item;
  129. return;
  130. }
  131. $array[$index] = $item;
  132. }
  133. /**
  134. * 数组插入
  135. * @param type $array
  136. * @param type $item
  137. * @param type $index
  138. */
  139. public static function arrayInsert(&$array, $index, $item)
  140. {
  141. $fore = ($index == 0) ? array() : array_splice($array, 0, $index);
  142. $fore[] = $item;
  143. $array = array_merge($fore, $array);
  144. }
  145. /**
  146. * 数组移除
  147. * @param array $array 数组
  148. * @param any $item 元素
  149. */
  150. public static function arrayRemove(&$array, $item)
  151. {
  152. $index = self::arrayIndexOf($array, $item);
  153. if ($index == -1) {
  154. } else {
  155. array_splice($array, $index, 1);
  156. }
  157. }
  158. /**
  159. * 数组移除
  160. * @param array $array
  161. * @param int $index
  162. */
  163. public static function arrayRemoveAt(&$array, $index)
  164. {
  165. array_splice($array, $index, 1);
  166. }
  167. /**
  168. * 数组克隆
  169. * @param array $array
  170. */
  171. public static function arrayClone($array)
  172. {
  173. return array_slice($array, 0);
  174. }
  175. /**
  176. * 数组清空
  177. * @param array $array
  178. */
  179. public static function arrayClear(&$array)
  180. {
  181. array_splice($array, 0);
  182. }
  183. /**
  184. * 元素是否位于某数组中
  185. * @param array $array
  186. * @param any $item
  187. * @return type
  188. */
  189. public static function arrayContains($array, $item)
  190. {
  191. return in_array($item, $array);
  192. }
  193. /**
  194. * 查找元素在数组中的位置
  195. * @param array $array
  196. * @param any $item
  197. * @return int
  198. */
  199. public static function arrayIndexOf($array, $item)
  200. {
  201. $indexs = array_keys($array, $item);
  202. if (count($indexs) > 0) {
  203. return $indexs[0];
  204. } else {
  205. return -1;
  206. }
  207. }
  208. /**
  209. * 数组去重
  210. * @param type $array
  211. */
  212. public static function arrayUnique(&$array)
  213. {
  214. $array = array_values(array_unique($array));
  215. }
  216. /**
  217. * 模仿as3中的sortby函数
  218. * @param type $arr
  219. * @param type $propertyName_sortBy
  220. */
  221. public static function arraySortBy($arr, $propertyName_sortBy)
  222. {
  223. throw new ErrorException("funciton not finish!");
  224. $a = function ($a, $b) {
  225. if ($a->id > $b->id) {
  226. return 1;
  227. } elseif ($a->id == $b->id) {
  228. return 0;
  229. } else {
  230. return -1;
  231. }
  232. };
  233. }
  234. /**
  235. * 辅助方法,将关联数组转换成class
  236. * @param type $assocArray
  237. * @return \stdClass
  238. */
  239. public static function array2class($assocArray)
  240. {
  241. $ret = new \stdClass();
  242. foreach ($assocArray as $p => $v) {
  243. $ret->$p = $v;
  244. }
  245. return $ret;
  246. }
  247. /**
  248. * 辅助方法,将数组转换成对象结构方便使用箭头操作
  249. * @param array $arr [{'kname':vallue,some:value},{'kname':vallue,some:value}...]
  250. * @param string $kName 作为key的字段的名称
  251. * @return \stdClass
  252. */
  253. public static function array2dict($arr, $kName)
  254. {
  255. $ret = new \stdClass();
  256. foreach ($arr as $item) {
  257. $k = $item->$kName;
  258. if ($k) {
  259. $ret->$k = $item;
  260. }
  261. }
  262. return $ret;
  263. }
  264. /**
  265. * 统计数组元素数或者对象属性个数
  266. * @param type $var
  267. * @return type
  268. */
  269. public static function count($var)
  270. {
  271. return count((array) $var);
  272. }
  273. }
  274. //\
  275. if (isEditor()) {
  276. /**
  277. * 关联数组, json_encode之后转为{}
  278. */
  279. class asoc_array
  280. {
  281. }
  282. }