Source for file Utilities.class.php

Documentation is available at Utilities.class.php

  1. <?php
  2. /**
  3. * NAJAX Utilities Namespace file.
  4. *
  5. * <p>This file defines the {@link NAJAX_Utilities} Class.</p>
  6. *
  7. * @author Stanimir Angeloff
  8. *
  9. * @package NAJAX
  10. *
  11. * @version 0.4.1.0
  12. *
  13. */
  14.  
  15. /**
  16. * NAJAX Utilities Class.
  17. *
  18. * <p>This class defines extended functions that
  19. * the NAJAX package uses and overrides some
  20. * deprecated functions, like gettype(...).</p>
  21. *
  22. * @author Stanimir Angeloff
  23. *
  24. * @package NAJAX
  25. *
  26. * @version 0.4.1.0
  27. *
  28. */
  29. class NAJAX_Utilities extends NAJAX_Observable
  30. {
  31. /**
  32. * Checks if an array is an associative array.
  33. *
  34. * @access public
  35. *
  36. * @param mixed $var The array to check.
  37. *
  38. * @return bool true if {@link $var} is an associative array, false
  39. * if {@link $var} is a sequential array.
  40. *
  41. * @static
  42. *
  43. */
  44. function isAssocArray($var)
  45. {
  46. // This code is based on mike-php's
  47. // comment in is_array function documentation.
  48. //
  49. // http://bg.php.net/is_array
  50. //
  51. // Thank you.
  52. //
  53.  
  54. if ( ! is_array($var)) {
  55.  
  56. return false;
  57. }
  58.  
  59. $arrayKeys = array_keys($var);
  60.  
  61. $sequentialKeys = range(0, sizeof($var));
  62.  
  63. if (function_exists('array_diff_assoc')) {
  64.  
  65. if (array_diff_assoc($arrayKeys, $sequentialKeys)) {
  66.  
  67. return true;
  68. }
  69.  
  70. } else {
  71.  
  72. if (
  73. (array_diff($arrayKeys, $sequentialKeys)) &&
  74. (array_diff($sequentialKeys, $arrayKeys))) {
  75.  
  76. return true;
  77. }
  78. }
  79.  
  80. return false;
  81. }
  82.  
  83. /**
  84. * Gets the type of a variable.
  85. *
  86. * @access public
  87. *
  88. * @param mixed $var The source variable.
  89. *
  90. * @return string Possibles values for the returned string are:
  91. * - "bool"
  92. * - "int"
  93. * - "float"
  94. * - "string"
  95. * - "s_array"
  96. * - "a_array"
  97. * - "object"
  98. * - "null"
  99. * - "unknown"
  100. *
  101. * @static
  102. *
  103. */
  104. function getType($var)
  105. {
  106. if (is_bool($var)) {
  107.  
  108. return 'bool';
  109.  
  110. } else if (is_int($var)) {
  111.  
  112. return 'int';
  113.  
  114. } else if (is_float($var)) {
  115.  
  116. return 'float';
  117.  
  118. } else if (is_string($var)) {
  119.  
  120. return 'string';
  121.  
  122. } else if (is_array($var)) {
  123.  
  124. if (NAJAX_Utilities::isAssocArray($var)) {
  125.  
  126. return 'a_array';
  127.  
  128. } else {
  129.  
  130. return 's_array';
  131. }
  132.  
  133. } else if (is_object($var)) {
  134.  
  135. return 'object';
  136.  
  137. } else if (is_null($var)) {
  138.  
  139. return 'null';
  140. }
  141.  
  142. return 'unknown';
  143. }
  144.  
  145. /**
  146. * Return current UNIX timestamp with microseconds.
  147. *
  148. * @access public
  149. *
  150. * @return float Returns the float 'sec,msec' where 'sec' is the
  151. * current time measured in the number of seconds since
  152. * the Unix Epoch (0:00:00 January 1, 1970 GMT), and
  153. * 'msec' is the microseconds part.
  154. *
  155. * @static
  156. *
  157. */
  158. function getMicroTime()
  159. {
  160. list($microTime, $time) = explode(" ", microtime());
  161.  
  162. return ((float) $microTime + (float) $time);
  163. }
  164.  
  165. /**
  166. * Registers NAJAX client header files.
  167. *
  168. * @access public
  169. *
  170. * @param string $base Base NAJAX folder.
  171. *
  172. * @param bool $optimized true to include optimized headers, false otherwise.
  173. *
  174. * @return string HTML code to include NAJAX client files.
  175. *
  176. * @static
  177. *
  178. */
  179. function header($base = '.', $optimized = true)
  180. {
  181. $returnValue = '<script type="text/javascript" src="' . $base . '/js/';
  182.  
  183. $returnValue .= 'najax';
  184.  
  185. if ($optimized) {
  186.  
  187. $returnValue .= '_optimized';
  188. }
  189.  
  190. $returnValue .= '.js"></script>';
  191.  
  192. if (array_key_exists('_NAJAX_EXTENSION_HEADERS', $GLOBALS)) {
  193.  
  194. foreach ($GLOBALS['_NAJAX_EXTENSION_HEADERS'] as $extension => $files) {
  195.  
  196. $extensionBase = $base . '/extensions/' . $extension . '/';
  197.  
  198. foreach ($files as $fileName) {
  199.  
  200. $returnValue .= '<script type="text/javascript" src="' . $extensionBase . ($optimized ? $fileName[1] : $fileName[0]) . '"></script>';
  201. }
  202. }
  203. }
  204.  
  205. return $returnValue;
  206. }
  207.  
  208. /**
  209. * Registers NACLES header data.
  210. *
  211. * <p>You should call this method after {@link NAJAX_Utilities::header}.</p>
  212. * <p>NACLES header data includes server time and callback URL.</p>
  213. *
  214. * @access public
  215. *
  216. * @param string $callbackUrl NACLES callback URL.
  217. *
  218. * @return string HTML code to initialize NACLES.
  219. *
  220. * @static
  221. *
  222. */
  223. function eventsHeader($callbackUrl = null)
  224. {
  225. if ($callbackUrl == null) {
  226.  
  227. $callbackUrl = $_SERVER['PHP_SELF'];
  228. }
  229.  
  230. $returnValue = '<script type="text/javascript">';
  231. $returnValue .= 'najax.events.callbackUrl = ' . NAJAX_Client::register($callbackUrl) . ';';
  232. $returnValue .= 'najax.events.lastRefresh = ' . NAJAX_Client::register(NAJAX_Utilities::getMicroTime()) . ';';
  233. $returnValue .= '</script>';
  234.  
  235. return $returnValue;
  236. }
  237.  
  238. /**
  239. * Registers NAJAX extension client header file.
  240. *
  241. * @access public
  242. *
  243. * @param string $extension The name of the NAJAX extension.
  244. *
  245. * @param string $fileName The extension JavaScript file name.
  246. * This file must be located in the
  247. * extension base folder.
  248. *
  249. * @param string $optimizedFileName The optimized extension JavaScript file name.
  250. * This file must be located in the
  251. * extension base folder.
  252. *
  253. *
  254. * @return bool true on success, false otherwise.
  255. *
  256. * @static
  257. *
  258. */
  259. function extensionHeader($extension, $fileName, $optimizedFileName = null)
  260. {
  261. if ( ! array_key_exists('_NAJAX_EXTENSION_HEADERS', $GLOBALS)) {
  262.  
  263. $GLOBALS['_NAJAX_EXTENSION_HEADERS'] = array();
  264. }
  265.  
  266. $extension = strtolower($extension);
  267.  
  268. if ( ! array_key_exists($extension, $GLOBALS['_NAJAX_EXTENSION_HEADERS'])) {
  269.  
  270. $GLOBALS['_NAJAX_EXTENSION_HEADERS'][$extension] = array();
  271. }
  272.  
  273. if (empty($optimizedFileName)) {
  274.  
  275. $optimizedFileName = $fileName;
  276. }
  277.  
  278. $GLOBALS['_NAJAX_EXTENSION_HEADERS'][$extension][] = array($fileName, $optimizedFileName);
  279.  
  280. return true;
  281. }
  282.  
  283. /**
  284. * Returns the input string with all alphabetic characters
  285. * converted to lower or upper case depending on the configuration.
  286. *
  287. * @param string $text The text to convert to lower/upper case.
  288. *
  289. * @return string The converted text.
  290. *
  291. * @static
  292. *
  293. */
  294. function caseConvert($text)
  295. {
  296. return strtolower($text);
  297. }
  298.  
  299. /**
  300. * Adds a {@link NAJAX_Utilities} events observer.
  301. *
  302. * @access public
  303. *
  304. * @param mixed $observer The observer object to add (must extend {@link NAJAX_Observer}).
  305. *
  306. * @return string true on success, false otherwise.
  307. *
  308. * @static
  309. *
  310. */
  311. function addObserver(&$observer)
  312. {
  313. return parent::addObserver($observer, 'NAJAX_Utilities');
  314. }
  315.  
  316. /**
  317. *
  318. * @access private
  319. *
  320. * @return bool
  321. *
  322. */
  323. function notifyObservers($event = 'default', $arg = null)
  324. {
  325. return parent::notifyObservers($event, $arg, 'NAJAX_Utilities');
  326. }
  327. }
  328. ?>

Documentation generated on Tue, 20 Sep 2005 21:40:43 +0300 by phpDocumentor 1.3.0RC3