StlUtil.php 8.0 KB

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