PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/simpletest/compatibility.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 199 lines | 124 code | 8 blank | 67 comment | 27 complexity | 9a8e5dac227669efc911eae38b955e78 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @version $Id: compatibility.php 1723 2008-04-08 00:34:10Z lastcraft $
  6. */
  7. /**
  8. * Static methods for compatibility between different
  9. * PHP versions.
  10. * @package SimpleTest
  11. */
  12. class SimpleTestCompatibility
  13. {
  14. /**
  15. * Creates a copy whether in PHP5 or PHP4.
  16. * @param object $object Thing to copy.
  17. * @return object A copy.
  18. * @access public
  19. * @static
  20. */
  21. function copy($object)
  22. {
  23. if (version_compare(phpversion(), '5') >= 0)
  24. {
  25. eval('$copy = clone $object;');
  26. return $copy;
  27. }
  28. return $object;
  29. }
  30. /**
  31. * Identity test. Drops back to equality + types for PHP5
  32. * objects as the === operator counts as the
  33. * stronger reference constraint.
  34. * @param mixed $first Test subject.
  35. * @param mixed $second Comparison object.
  36. * @return boolean True if identical.
  37. * @access public
  38. * @static
  39. */
  40. function isIdentical($first, $second)
  41. {
  42. if (version_compare(phpversion(), '5') >= 0)
  43. {
  44. return SimpleTestCompatibility :: _isIdenticalType($first, $second);
  45. }
  46. if ($first != $second)
  47. {
  48. return false;
  49. }
  50. return ($first === $second);
  51. }
  52. /**
  53. * Recursive type test.
  54. * @param mixed $first Test subject.
  55. * @param mixed $second Comparison object.
  56. * @return boolean True if same type.
  57. * @access private
  58. * @static
  59. */
  60. function _isIdenticalType($first, $second)
  61. {
  62. if (gettype($first) != gettype($second))
  63. {
  64. return false;
  65. }
  66. if (is_object($first) && is_object($second))
  67. {
  68. if (get_class($first) != get_class($second))
  69. {
  70. return false;
  71. }
  72. return SimpleTestCompatibility :: _isArrayOfIdenticalTypes(get_object_vars($first), get_object_vars($second));
  73. }
  74. if (is_array($first) && is_array($second))
  75. {
  76. return SimpleTestCompatibility :: _isArrayOfIdenticalTypes($first, $second);
  77. }
  78. if ($first !== $second)
  79. {
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Recursive type test for each element of an array.
  86. * @param mixed $first Test subject.
  87. * @param mixed $second Comparison object.
  88. * @return boolean True if identical.
  89. * @access private
  90. * @static
  91. */
  92. function _isArrayOfIdenticalTypes($first, $second)
  93. {
  94. if (array_keys($first) != array_keys($second))
  95. {
  96. return false;
  97. }
  98. foreach (array_keys($first) as $key)
  99. {
  100. $is_identical = SimpleTestCompatibility :: _isIdenticalType($first[$key], $second[$key]);
  101. if (! $is_identical)
  102. {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. /**
  109. * Test for two variables being aliases.
  110. * @param mixed $first Test subject.
  111. * @param mixed $second Comparison object.
  112. * @return boolean True if same.
  113. * @access public
  114. * @static
  115. */
  116. function isReference(&$first, &$second)
  117. {
  118. if (version_compare(phpversion(), '5', '>=') && is_object($first))
  119. {
  120. return ($first === $second);
  121. }
  122. if (is_object($first) && is_object($second))
  123. {
  124. $id = uniqid("test");
  125. $first->$id = true;
  126. $is_ref = isset($second->$id);
  127. unset($first->$id);
  128. return $is_ref;
  129. }
  130. $temp = $first;
  131. $first = uniqid("test");
  132. $is_ref = ($first === $second);
  133. $first = $temp;
  134. return $is_ref;
  135. }
  136. /**
  137. * Test to see if an object is a member of a
  138. * class hiearchy.
  139. * @param object $object Object to test.
  140. * @param string $class Root name of hiearchy.
  141. * @return boolean True if class in hiearchy.
  142. * @access public
  143. * @static
  144. */
  145. function isA($object, $class)
  146. {
  147. if (version_compare(phpversion(), '5') >= 0)
  148. {
  149. if (! class_exists($class, false))
  150. {
  151. if (function_exists('interface_exists'))
  152. {
  153. if (! interface_exists($class, false))
  154. {
  155. return false;
  156. }
  157. }
  158. }
  159. eval("\$is_a = \$object instanceof $class;");
  160. return $is_a;
  161. }
  162. if (function_exists('is_a'))
  163. {
  164. return is_a($object, $class);
  165. }
  166. return ((strtolower($class) == get_class($object)) or (is_subclass_of($object, $class)));
  167. }
  168. /**
  169. * Sets a socket timeout for each chunk.
  170. * @param resource $handle Socket handle.
  171. * @param integer $timeout Limit in seconds.
  172. * @access public
  173. * @static
  174. */
  175. function setTimeout($handle, $timeout)
  176. {
  177. if (function_exists('stream_set_timeout'))
  178. {
  179. stream_set_timeout($handle, $timeout, 0);
  180. }
  181. elseif (function_exists('socket_set_timeout'))
  182. {
  183. socket_set_timeout($handle, $timeout, 0);
  184. }
  185. elseif (function_exists('set_socket_timeout'))
  186. {
  187. set_socket_timeout($handle, $timeout, 0);
  188. }
  189. }
  190. }
  191. ?>