PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/base/Utils.php

https://github.com/rsms/phpab
PHP | 341 lines | 142 code | 25 blank | 174 comment | 17 complexity | 696053f1ba8a39835b2ff05082870f31 MD5 | raw file
  1. <?
  2. /*
  3. Copyright (c) 2005-2007, Rasmus Andersson
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /**
  21. * General utilities
  22. *
  23. * @version $Id$
  24. * @author Rasmus Andersson http://hunch.se/
  25. * @package ab
  26. * @subpackage util
  27. * @deprecated Replaced & decentralised (see method details for information about what you should use instead)
  28. */
  29. class Utils {
  30. /**
  31. * Convert absolute path to a relative path, based in <samp>$relativeToBase</samp>
  32. *
  33. * <b>Example</b>
  34. * <code>
  35. * print Utils::relativePath('/absolute/path/to/foo.bar', '/absolute/path');
  36. * prints "to/foo.bar"
  37. * </code>
  38. *
  39. * @param string
  40. * @param string
  41. * @return string
  42. * @deprecated Use {@link File::relativePath()} instead
  43. */
  44. public static function relativePath( $absolutePath, $relativeToBase ) {
  45. return File::relativePath($absolutePath, $relativeToBase);
  46. }
  47. /**
  48. * Print something to stderr
  49. *
  50. * @param mixed
  51. * @return void
  52. * @deprecated Use {@link IO::writeError()} instead
  53. */
  54. public static function printError($str)
  55. {
  56. IO::writeError($str);
  57. }
  58. /**
  59. * Print a dump of something
  60. * @deprecated Will be removed in future version
  61. */
  62. public static function dump( $data, $indentLevel = 0 ) {
  63. self::dumpWalker($data, $indentLevel+1);
  64. }
  65. /**
  66. * Print a dump of the stack
  67. * @deprecated Will be removed in future version
  68. */
  69. private static function dumpWalker(&$v, $level) {
  70. if(is_array($v)) {
  71. print 'Array(' . count($v) . ") {\n";
  72. foreach($v as $k => $v2) {
  73. print str_repeat(' ', $level) . '[' . self::getType($k) . ' ' . var_export($k,1) . '] = ';
  74. self::dumpWalker($v[$k], $level+1);
  75. }
  76. print str_repeat(' ', $level-1) . '}';
  77. }
  78. else {
  79. print self::getType($v) . ' ';
  80. if(is_object($v)) {
  81. if(method_exists($v, '__toString')) {
  82. $ov = $v->__toString();
  83. if(is_string($ov)) print "\"$ov\"";
  84. else print $ov;
  85. }
  86. elseif(method_exists($v, 'toString')) {
  87. $ov = $v->toString();
  88. if(is_string($ov)) print "\"$ov\"";
  89. else print $ov;
  90. }
  91. else
  92. echo $v;
  93. }
  94. else
  95. var_export($v);
  96. }
  97. print "\n";
  98. }
  99. /**
  100. * Get variable type name
  101. *
  102. * @param mixed
  103. * @return string
  104. * @deprecated Will be removed in future version
  105. */
  106. public static function getType( $v ) {
  107. if(is_object($v))
  108. return get_class($v);
  109. return self::normalizeTypeName(gettype($v));
  110. }
  111. /**
  112. * Normalize type name
  113. *
  114. * @param string
  115. * @return string
  116. * @deprecated Will be removed in future version
  117. */
  118. public static function normalizeTypeName( $t ) {
  119. $t = strtolower(substr($t,0,3));
  120. switch($t) {
  121. case 'str': return 'string';
  122. case 'int': return 'integer';
  123. case 'flo':
  124. case 'dou':
  125. case 'rea': return 'double';
  126. case 'voi': return 'void';
  127. case 'boo': return 'boolean';
  128. case 'mix': return 'mixed';
  129. case 'arr': return 'array';
  130. case 'obj': return 'object';
  131. case 'res': return 'resource';
  132. case 'nul': return 'null';
  133. }
  134. return $t;
  135. }
  136. /**
  137. * Convert an object of one class to another class while keeping it's state
  138. *
  139. * @param object
  140. * @param classname
  141. * @return object
  142. * @throws ClassCastException
  143. * @deprecated Use {@link PHP::classcast()} instead
  144. */
  145. public static function classcast($obj, $toClassName) {
  146. return PHP::classcast($obj, $toClassName);
  147. }
  148. /**
  149. * @param mixed
  150. * @param string Any string value accepted by PHP settype()
  151. * @return bool Success
  152. * @deprecated Use {@link PHP::typecast()} instead
  153. */
  154. public static function typecast($v, $type) {
  155. PHP::typecast($v, $type);
  156. }
  157. /**
  158. * Convenience function to load a xml file into an array
  159. *
  160. * @param string
  161. * @return array XMLDOM
  162. * @deprecated Will be removed in future version. Considering using the convenience functions provided by {@link SimpleXMLParser}
  163. */
  164. public static function loadXML( $file ) {
  165. $xp = new SimpleXMLParser();
  166. $xp->loadFile($file);
  167. return $xp->toArray();
  168. }
  169. /**
  170. * Load and unserialize file
  171. *
  172. * @param string
  173. * @return mixed
  174. * @throws IOException
  175. * @deprecated Use {@link IO::unserializeFile()} instead
  176. */
  177. public static function unserializeFile( $file ) {
  178. return IO::unserializeFile($file);
  179. }
  180. /**
  181. * Serialize data and write it to a file
  182. *
  183. * @param mixed
  184. * @param string
  185. * @return void
  186. * @throws IOException
  187. * @deprecated Use {@link IO::serialize()} instead
  188. */
  189. public static function serializeToFile( $data, $file ) {
  190. IO::serialize($data, $file);
  191. }
  192. /**
  193. * Escape attribute value
  194. *
  195. * @param string
  196. * @return string
  197. * @deprecated Use {@link XML::escape()} instead
  198. */
  199. public static function xmlEscape($str) {
  200. return XML::escape($str);
  201. }
  202. /**
  203. * Unescape attribute value
  204. *
  205. * @param string
  206. * @return string
  207. * @deprecated Use {@link XML::unescape()} instead
  208. */
  209. public static function xmlUnescape($str) {
  210. return XML::unescape($str);
  211. }
  212. /**
  213. * Escape text node
  214. *
  215. * @param string
  216. * @return string
  217. * @deprecated Use {@link XML::escapeText()} instead
  218. */
  219. public static function xmlEscapeText($str) {
  220. return XML::escapeText($str);
  221. }
  222. /**
  223. * Unescape text node
  224. *
  225. * @param string
  226. * @return string
  227. * @deprecated Use {@link XML::escapeText()} instead
  228. */
  229. public static function xmlUnescapeText($str) {
  230. return XML::unescapeText($str);
  231. }
  232. /**
  233. * @param string
  234. * @return string
  235. * @deprecated Use {@link IMAP::mimeStringDecode()} instead
  236. */
  237. public static function mimeStringDecode( $str ) {
  238. # we can't just redirect to IMAP::mimeStringDecode() because that's a separate library.
  239. $s = imap_mime_header_decode($str);
  240. return utf8_encode($s[0]->text);
  241. }
  242. private static $l2h = array('a'=>'A','b'=>'B','c'=>'C','d'=>'D','e'=>'E','f'=>'F','g'=>'G','h'=>'H','i'=>'I','j'=>'J','k'=>'K','l'=>'L','m'=>'M','n'=>'N','o'=>'O','p'=>'P','q'=>'Q','r'=>'R','s'=>'S','t'=>'T','u'=>'U','v'=>'V','w'=>'W','x'=>'X','y'=>'Y','z'=>'Z',"\xe5"=>"\xc5","\xe4"=>"\xc4","\xf6"=>"\xd6","\xe6"=>"\xc6","\xf8"=>"\xd8","\xe9"=>"\xc9","\xe8"=>"\xc8","\xe1"=>"\xc1","\xe0"=>"\xc0","\xfc"=>"\xdc","\xfb"=>"\xdb","\xf4"=>"\xd4","\xe7"=>"\xc7");
  243. private static $h2l = array('A'=>'a','B'=>'b','C'=>'c','D'=>'d','E'=>'e','F'=>'f','G'=>'g','H'=>'h','I'=>'i','J'=>'j','K'=>'k','L'=>'l','M'=>'m','N'=>'n','O'=>'o','P'=>'p','Q'=>'q','R'=>'r','S'=>'s','T'=>'t','U'=>'u','V'=>'v','W'=>'w','X'=>'x','Y'=>'y','Z'=>'z',"\xc5"=>"\xe5","\xc4"=>"\xe4","\xd6"=>"\xf6","\xc6"=>"\xe6","\xd8"=>"\xf8","\xc9"=>"\xe9","\xc8"=>"\xe8","\xc1"=>"\xe1","\xc0"=>"\xe0","\xdc"=>"\xfc","\xdb"=>"\xfb","\xd4"=>"\xf4","\xc7"=>"\xe7");
  244. /**
  245. * @param char
  246. * @return char
  247. * @deprecated Will be removed in future version.
  248. */
  249. public static function chrToLower($ch)
  250. {
  251. if(isset(self::$h2l[$ch]))
  252. return self::$h2l[$ch];
  253. return $ch;
  254. }
  255. /**
  256. * @param char
  257. * @return char
  258. * @deprecated Will be removed in future version.
  259. */
  260. public static function chrToUpper($ch)
  261. {
  262. if(isset(self::$l2h[$ch]))
  263. return self::$l2h[$ch];
  264. return $ch;
  265. }
  266. /**
  267. * @param string
  268. * @param bool
  269. * @return string
  270. * @deprecated Will be removed in future version.
  271. */
  272. public static function strToLower($str, $utf8 = false) {
  273. if($utf8)
  274. return utf8_encode(strtr(strtolower(utf8_decode($str)),"\xc5\xc4\xd6\xc6\xd1\xd8\xd5\xdc\xc9\xca","\xe5\xe4\xf6\xe6\xf1\xf8\xf5\xfc\xe9\xea"));
  275. else
  276. return strtr(strtolower($str),"\xc5\xc4\xd6\xc6\xd1\xd8\xd5\xdc\xc9\xca","\xe5\xe4\xf6\xe6\xf1\xf8\xf5\xfc\xe9\xea");
  277. }
  278. /**
  279. * @param string
  280. * @return string
  281. * @deprecated Will be removed in future version.
  282. */
  283. public static function strToUpper($str, $length = -1)
  284. {
  285. return strtr(strtoupper($str),"\xe5\xe4\xf6\xe6\xf1\xf8\xf5\xfc\xe9\xea","\xc5\xc4\xd6\xc6\xd1\xd8\xd5\xdc\xc9\xca");
  286. }
  287. /**
  288. * @param array
  289. * @param string
  290. * @param string
  291. * @return string "{key=>value, key=>value...}"
  292. * @deprecated Will be removed in future version.
  293. */
  294. public static function hash_to_string($array, $concator = '=>', $separator = ', ')
  295. {
  296. if(!is_array($array)) return '';
  297. if(!$array) return '{}';
  298. $str = '{';
  299. foreach($array as $k => $v) {
  300. $str .= (is_int($k) || is_double($k)) ? $k : "'".strval($k)."'";
  301. $str .= $concator;
  302. if(is_array($v))
  303. $str .= self::hash_to_string($v);
  304. elseif(is_int($v) || is_double($v))
  305. $str .= $v;
  306. else
  307. $str .= "'".strval($v)."'";
  308. $str .= $separator;
  309. }
  310. return ($separator ? substr($str, 0, -strlen($separator)) : $str) . '}';
  311. }
  312. }
  313. ?>