V.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 = '', $filterCallback = null) {
  12. if (empty($name)) return null;
  13. $ret = null;
  14. if (is_array($from)) {
  15. if (array_key_exists($name, $from)) {
  16. $ret = $from[$name];
  17. }
  18. }
  19. else if (is_object($from)) {
  20. if (isset($from->$name)) {
  21. $ret = $from->$name;
  22. }
  23. }
  24. if (isset($ret) && $type != '') {
  25. $ret = V::convert($ret, $type);
  26. }
  27. if (!empty($filterCallback)) {
  28. if ($type == 'array' && is_array($ret) && !empty($ret)) {
  29. $ret = V::filter($ret, $filterCallback);
  30. }
  31. }
  32. $ret = (null !== $ret)? $ret : $default;
  33. return $ret;
  34. }
  35. /**
  36. * Convert variable type.
  37. */
  38. public static function convert($from, $type = 'string') {
  39. $type = strtolower($type);
  40. // is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
  41. $ret = null;
  42. switch ($type) {
  43. case 'string':
  44. if (is_scalar($from)) {
  45. $ret = $from;
  46. settype($ret, $type);
  47. }
  48. break;
  49. case 'word':
  50. if (is_scalar($from)) {
  51. $ret = $from;
  52. settype($ret, 'string');
  53. $ret = trim($ret);
  54. if (false !== ($pos = strpos($ret, ' '))) {
  55. $ret = substr($ret, 0, $pos);
  56. }
  57. }
  58. break;
  59. case 'int':
  60. case 'integer':
  61. if (is_scalar($from)) {
  62. $ret = $from;
  63. settype($ret, $type);
  64. }
  65. break;
  66. case 'float':
  67. case 'double':
  68. if (is_scalar($from)) {
  69. $ret = str_replace(',', '.', $from);
  70. settype($ret, $type);
  71. }
  72. break;
  73. case 'object':
  74. case 'array':
  75. if (is_scalar($from) || is_array($from) || is_object($from)) {
  76. $ret = $from;
  77. settype($ret, $type);
  78. }
  79. break;
  80. case 'int_array':
  81. if (is_scalar($from) || is_array($from) || is_object($from)) {
  82. $ret = array();
  83. $arr = $from;
  84. settype($arr, 'array');
  85. foreach ($arr as $v) {
  86. $v = V::convert($v, 'int');
  87. $ret[] = $v;
  88. }
  89. }
  90. break;
  91. case 'uint_array':// uncigned int array
  92. if (is_scalar($from) || is_array($from) || is_object($from)) {
  93. $ret = array();
  94. $arr = $from;
  95. settype($arr, 'array');
  96. foreach ($arr as $v) {
  97. $v = V::convert($v, 'int');
  98. if ($v <= 0) continue;
  99. $ret[] = $v;
  100. }
  101. }
  102. break;
  103. case 'float_array':// uncigned int array
  104. if (is_scalar($from) || is_array($from) || is_object($from)) {
  105. $ret = array();
  106. $arr = $from;
  107. settype($arr, 'array');
  108. foreach ($arr as $v) {
  109. $v = V::convert($v, 'float');
  110. $ret[] = $v;
  111. }
  112. }
  113. break;
  114. default:
  115. $fun = 'func_type_convert_'.$type;
  116. if (function_exists($fun)) {
  117. $ret = $fun($from);
  118. }
  119. break;
  120. }
  121. return $ret;
  122. }
  123. /**
  124. * Merge the contents of two objects/array.
  125. *
  126. * array V::extend(mixed $defaults, mixed $params);
  127. * @see http://api.jquery.com/jQuery.extend/
  128. * is_scalar($from) - return TRUE if int,float,string,bool, FALSE if array,object,resource, ...
  129. */
  130. public static function extend($defaults, $params) {
  131. $ret = array();
  132. $d = (is_array($defaults))? $defaults : (array)$defaults;
  133. $p = (is_array($params))? $params : (array)$params;
  134. foreach ($d as $k => $v) {
  135. $ret[$k] = $v;
  136. }
  137. foreach ($p as $k => $v) {
  138. if (array_key_exists($k, $ret) && (is_array($ret[$k]) || is_object($ret[$k])) && (is_array($v) || is_object($v))) {
  139. $ret[$k] = V::extend($ret[$k], $v);
  140. } else {
  141. $ret[$k] = $v;
  142. }
  143. }
  144. return $ret;
  145. }
  146. public static function json_encode_latin2($o, $force_object = false) {
  147. if ($o === '') {
  148. return '""';
  149. }
  150. else if (!$o) {
  151. return 'null';
  152. }
  153. else if (is_array($o)) {
  154. $arr = '';
  155. if ($force_object) {
  156. foreach ($o as $k => $v) {
  157. $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  158. }
  159. return '{'.implode(',',$arr).'}';
  160. }
  161. else {
  162. foreach ($o as $k => $v) {
  163. if (is_string($k)) $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  164. else $arr[] = V::json_encode_latin2($v);
  165. }
  166. return '['.implode(',',$arr).']';
  167. }
  168. }
  169. else if (is_object($o)) {
  170. $arr = '';
  171. foreach (get_object_vars($o) as $k => $v) {
  172. $arr[] = '"'.$k.'":'.V::json_encode_latin2($v, $force_object);
  173. }
  174. return '{'.implode(',',$arr).'}';
  175. }
  176. else if (is_string($o)) {
  177. return '"'.addslashes(str_replace(array("\n","\r"), array('\n',''), $o)).'"';
  178. }
  179. else if (is_numeric($o)) {
  180. return ''.$o.'';
  181. }
  182. else if (is_bool($o)) {
  183. return ''.(($o)? 'true' : 'false').'';
  184. }
  185. }
  186. public static function copy($o) {
  187. $null = null;
  188. if (!$o) {
  189. return $null;
  190. }
  191. else if (is_array($o)) {
  192. $ret = array();
  193. foreach ($o as $k => $v) {
  194. $ret[$k] = $v;
  195. }
  196. return $ret;
  197. }
  198. else if (is_object($o)) {
  199. $ret = new stdClass();
  200. foreach (get_object_vars($o) as $k => $v) {
  201. $ret->$k = $v;
  202. }
  203. return $ret;
  204. }
  205. else {
  206. $ret = $o;
  207. return $ret;
  208. }
  209. }
  210. public static function make_link($prefix = '', $params = array()) {
  211. $ret = '';
  212. if ($prefix) {
  213. $ret = $prefix;
  214. }
  215. if (!empty($params)) {
  216. $ret_arr = array();
  217. foreach ($params as $k => $v) {
  218. $ret_arr[] = $k . "=" . $v;
  219. }
  220. $ret .= "?" . implode("&", $ret_arr);
  221. }
  222. return $ret;
  223. }
  224. public static function strShort($label, $maxLength = 10, $suffix = ' ...') {
  225. if (strlen($label) > $maxLength) {
  226. $pos = strpos($label, ' - ');
  227. if ($pos > $maxLength || $pos < 5) {
  228. $label = substr($label, 0, $pos) . $suffix;
  229. } else {
  230. $label = substr($label, 0, $pos);
  231. }
  232. }
  233. return $label;
  234. }
  235. public static function strShortUtf8($label, $maxLength = 10, $suffix = ' ...') {
  236. if (mb_strlen($label, 'utf-8') > $maxLength) {
  237. $pos = mb_strpos($label, ' - ', 0, 'utf-8');
  238. if ($pos > $maxLength || $pos < 5) {
  239. $label = mb_substr($label, 0, $maxLength, 'utf-8') . $suffix;
  240. } else {
  241. $label = mb_substr($label, 0, $pos, 'utf-8');
  242. }
  243. }
  244. return $label;
  245. }
  246. public static function filter($array, $filterCallback) {
  247. if (!is_callable($filterCallback)) {
  248. throw new Exception("callback is not callable '" . ((is_array($filterCallback))? implode('.', $filterCallback) : $filterCallback) . "'");
  249. }
  250. return array_filter($array, $filterCallback);
  251. }
  252. public static function filterNotEmpty($value) {
  253. return !empty($value);
  254. }
  255. public static function filterInteger($value) {// An integer or string with integer value
  256. if (is_int($value)) {
  257. return true;
  258. } else if (is_string($value)) {
  259. if ((string)(int)$value === $value) {
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. public static function filterNegativeInteger($value) {// An integer containing only negative values (..,-2,-1)
  266. if (V::filterInteger($value)) {
  267. if (intval($value) < 0) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. public static function filterNonNegativeInteger($value) {// An integer containing only non-negative values (0,1,2,..)
  274. if (V::filterInteger($value)) {
  275. if (intval($value) >= 0) {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. public static function filterNonPositiveInteger($value) {// An integer containing only non-positive values (..,-2,-1,0)
  282. if (V::filterInteger($value)) {
  283. if (intval($value) <= 0) {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. public static function filterPositiveInteger($value) {// An integer containing only positive values (1,2,..)
  290. if (V::filterInteger($value)) {
  291. if (intval($value) > 0) {
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. }