V.php 7.3 KB

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