V.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @see http://pl2.php.net/manual/en/book.var.php
  4. *
  5. * Define Your own convert function: var func_type_convert_{$type}($var);
  6. */
  7. class V {
  8. /**
  9. * Get variable from array or object.
  10. */
  11. public static function get($name, $default, &$from, $type = '') {
  12. $ret = null;
  13. if (is_array($from)) {
  14. if (array_key_exists($name, $from)) {
  15. $ret = $from[$name];
  16. }
  17. }
  18. else if (is_object($from)) {
  19. if (isset($from->$name)) {
  20. $ret = $from->$name;
  21. }
  22. }
  23. if (isset($ret) && $type != '') {
  24. $ret = V::convert($ret, $type);
  25. }
  26. return (isset($ret))? $ret : $default;
  27. }
  28. /**
  29. * Convert variable type.
  30. */
  31. public static function convert(&$from, $type = 'string') {
  32. $type = strtolower($type);
  33. // is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
  34. $ret = null;
  35. switch ($type) {
  36. case 'string':
  37. if (is_scalar($from)) {
  38. $ret = $from;
  39. settype($ret, $type);
  40. }
  41. break;
  42. case 'word':
  43. if (is_scalar($from)) {
  44. $ret = $from;
  45. settype($ret, 'string');
  46. $ret = trim($ret);
  47. if (false !== ($pos = strpos($ret, ' '))) {
  48. $ret = substr($ret, 0, $pos);
  49. }
  50. }
  51. break;
  52. case 'int':
  53. case 'integer':
  54. if (is_scalar($from)) {
  55. $ret = $from;
  56. settype($ret, $type);
  57. }
  58. break;
  59. case 'float':
  60. case 'double':
  61. if (is_scalar($from)) {
  62. $ret = str_replace(',','.',$from);
  63. settype($ret, $type);
  64. }
  65. break;
  66. case 'object':
  67. case 'array':
  68. if (is_scalar($from) || is_array($from) || is_object($from)) {
  69. $ret = $from;
  70. settype($ret, $type);
  71. }
  72. break;
  73. case 'int_array':
  74. if (is_scalar($from) || is_array($from) || is_object($from)) {
  75. $ret = array();
  76. $arr = $from;
  77. settype($arr, $type);
  78. foreach ($arr as $v) {
  79. $v = V::convert($v,'int');
  80. $ret[] = $v;
  81. }
  82. }
  83. break;
  84. case 'uint_array':// uncigned int array
  85. if (is_scalar($from) || is_array($from) || is_object($from)) {
  86. $ret = array();
  87. $arr = $from;
  88. settype($arr, $type);
  89. foreach ($arr as $v) {
  90. $v = V::convert($v,'int');
  91. if ($v <= 0) continue;
  92. $ret[] = $v;
  93. }
  94. }
  95. break;
  96. case 'float_array':// uncigned int array
  97. if (is_scalar($from) || is_array($from) || is_object($from)) {
  98. $ret = array();
  99. $arr = $from;
  100. settype($arr, $type);
  101. foreach ($arr as $v) {
  102. $v = V::convert($v, 'float');
  103. $ret[] = $v;
  104. }
  105. }
  106. break;
  107. default:
  108. $fun = 'func_type_convert_'.$type;
  109. if (function_exists($fun)) {
  110. $ret = $fun($from);
  111. }
  112. break;
  113. }
  114. return $ret;
  115. }
  116. /**
  117. * Merge the contents of two objects/array.
  118. *
  119. * array V::extend(mixed $defaults, mixed $params);
  120. * @see http://api.jquery.com/jQuery.extend/
  121. * is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
  122. */
  123. public static function extend($defaults, $params) {
  124. $ret = array();
  125. $d = (is_array($defaults))? $defaults : (array)$defaults;
  126. $p = (is_array($params))? $params : (array)$params;
  127. foreach ($d as $k => $v) {
  128. $ret[$k] = $v;
  129. }
  130. foreach ($p as $k => $v) {
  131. if (array_key_exists($k, $ret) && (is_array($ret[$k]) || is_object($ret[$k])) && (is_array($v) || is_object($v))) {
  132. $ret[$k] = V::extend($ret[$k], $v);
  133. } else {
  134. $ret[$k] = $v;
  135. }
  136. }
  137. return $ret;
  138. }
  139. public static function json_encode_latin2($o, $force_object = false) {
  140. if ($o === '') {
  141. return '""';
  142. }
  143. else if (!$o) {
  144. return 'null';
  145. }
  146. else if (is_array($o)) {
  147. $arr = '';
  148. if ($force_object) {
  149. foreach ($o as $k => $v) {
  150. $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  151. }
  152. return '{'.implode(',',$arr).'}';
  153. }
  154. else {
  155. foreach ($o as $k => $v) {
  156. if (is_string($k)) $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  157. else $arr[] = V::json_encode_latin2($v);
  158. }
  159. return '['.implode(',',$arr).']';
  160. }
  161. }
  162. else if (is_object($o)) {
  163. $arr = '';
  164. foreach (get_object_vars($o) as $k => $v) {
  165. $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  166. }
  167. return '{'.implode(',',$arr).'}';
  168. }
  169. else if (is_string($o)) {
  170. return '"'.addslashes(str_replace(array("\n","\r"), array('\n',''), $o)).'"';
  171. }
  172. else if (is_numeric($o)) {
  173. return ''.$o.'';
  174. }
  175. else if (is_bool($o)) {
  176. return ''.(($o)? 'true' : 'false').'';
  177. }
  178. }
  179. public static function copy($o) {
  180. $null = null;
  181. if (!$o) {
  182. return $null;
  183. }
  184. else if (is_array($o)) {
  185. $ret = array();
  186. foreach ($o as $k => $v) {
  187. $ret[$k] = $v;
  188. }
  189. return $ret;
  190. }
  191. else if (is_object($o)) {
  192. $ret = new stdClass();
  193. foreach (get_object_vars($o) as $k => $v) {
  194. $ret->$k = $v;
  195. }
  196. return $ret;
  197. }
  198. else {
  199. $ret = $o;
  200. return $ret;
  201. }
  202. }
  203. public static function make_link($prefix = '', $params = array()) {
  204. $ret = '';
  205. if ($prefix) {
  206. $ret = $prefix;
  207. }
  208. if (!empty($params)) {
  209. $ret_arr = array();
  210. foreach ($params as $k => $v) {
  211. $ret_arr[] = $k . "=" . $v;
  212. }
  213. $ret .= "?" . implode("&", $ret_arr);
  214. }
  215. return $ret;
  216. }
  217. public static function strShort($label, $maxLength = 10, $suffix = ' ...') {
  218. if (strlen($label) > $maxLength) {
  219. $pos = strpos($label, ' - ');
  220. if ($pos > $maxLength || $pos < 5) {
  221. $label = substr($label, 0, $pos) . $suffix;
  222. } else {
  223. $label = substr($label, 0, $pos);
  224. }
  225. }
  226. return $label;
  227. }
  228. public static function strShortUtf8($label, $maxLength = 10, $suffix = ' ...') {
  229. if (mb_strlen($label, 'utf-8') > $maxLength) {
  230. $pos = mb_strpos($label, ' - ', 0, 'utf-8');
  231. if ($pos > $maxLength || $pos < 5) {
  232. $label = mb_substr($label, 0, $maxLength, 'utf-8') . $suffix;
  233. } else {
  234. $label = mb_substr($label, 0, $pos, 'utf-8');
  235. }
  236. }
  237. return $label;
  238. }
  239. }