/projects/haxeindex/bin/SilexIndex.phpdir/framework/hx/Type.class.php

https://github.com/silexlabs/Silex-v1 · PHP · 298 lines · 295 code · 3 blank · 0 comment · 73 complexity · a4eaf4e26a83e65a86325fb86a503816 MD5 · raw file

  1. <?php
  2. class Type {
  3. public function __construct(){}
  4. static function getClass($o) {
  5. if($o === null) {
  6. return null;
  7. }
  8. if(is_array($o)) {
  9. if(count($o) === 2 && is_callable($o)) {
  10. return null;
  11. }
  12. return _hx_ttype("Array");
  13. }
  14. if(is_string($o)) {
  15. if(_hx_is_lambda($o)) {
  16. return null;
  17. }
  18. return _hx_ttype("String");
  19. }
  20. if(!is_object($o)) {
  21. return null;
  22. }
  23. $c = get_class($o);
  24. if($c === false || $c === "_hx_anonymous" || is_subclass_of($c, "enum")) {
  25. return null;
  26. } else {
  27. return _hx_ttype($c);
  28. }
  29. }
  30. static function getEnum($o) {
  31. if(!$o instanceof Enum) {
  32. return null;
  33. } else {
  34. return _hx_ttype(get_class($o));
  35. }
  36. }
  37. static function getSuperClass($c) {
  38. $s = get_parent_class($c->__tname__);
  39. if($s === false) {
  40. return null;
  41. } else {
  42. return _hx_ttype($s);
  43. }
  44. }
  45. static function getClassName($c) {
  46. if($c === null) {
  47. return null;
  48. }
  49. return $c->__qname__;
  50. }
  51. static function getEnumName($e) {
  52. return $e->__qname__;
  53. }
  54. static function resolveClass($name) {
  55. $c = _hx_qtype($name);
  56. if($c instanceof _hx_class || $c instanceof _hx_interface) {
  57. return $c;
  58. } else {
  59. return null;
  60. }
  61. }
  62. static function resolveEnum($name) {
  63. $e = _hx_qtype($name);
  64. if($e instanceof _hx_enum) {
  65. return $e;
  66. } else {
  67. return null;
  68. }
  69. }
  70. static function createInstance($cl, $args) {
  71. if($cl->__qname__ === "Array") {
  72. return new _hx_array(array());
  73. }
  74. if($cl->__qname__ === "String") {
  75. return $args[0];
  76. }
  77. $c = $cl->__rfl__();
  78. if($c === null) {
  79. return null;
  80. }
  81. return $inst = $c->getConstructor() ? $c->newInstanceArgs($args->»a) : $c->newInstanceArgs();
  82. }
  83. static function createEmptyInstance($cl) {
  84. if($cl->__qname__ === "Array") {
  85. return new _hx_array(array());
  86. }
  87. if($cl->__qname__ === "String") {
  88. return "";
  89. }
  90. try {
  91. php_Boot::$skip_constructor = true;
  92. $rfl = $cl->__rfl__();
  93. if($rfl === null) {
  94. return null;
  95. }
  96. $m = $rfl->getConstructor();
  97. $nargs = $m->getNumberOfRequiredParameters();
  98. $i = null;
  99. if($nargs > 0) {
  100. $args = array_fill(0, $m->getNumberOfRequiredParameters(), null);
  101. $i = $rfl->newInstanceArgs($args);
  102. } else {
  103. $i = $rfl->newInstanceArgs(array());
  104. }
  105. php_Boot::$skip_constructor = false;
  106. return $i;
  107. }catch(Exception $»e) {
  108. $_ex_ = ($»e instanceof HException) ? $»e->e : $»e;
  109. $e = $_ex_;
  110. {
  111. php_Boot::$skip_constructor = false;
  112. throw new HException("Unable to instantiate " . Std::string($cl));
  113. }
  114. }
  115. return null;
  116. }
  117. static function createEnum($e, $constr, $params = null) {
  118. $f = Reflect::field($e, $constr);
  119. if($f === null) {
  120. throw new HException("No such constructor " . $constr);
  121. }
  122. if(Reflect::isFunction($f)) {
  123. if($params === null) {
  124. throw new HException("Constructor " . $constr . " need parameters");
  125. }
  126. return Reflect::callMethod($e, $f, $params);
  127. }
  128. if($params !== null && $params->length !== 0) {
  129. throw new HException("Constructor " . $constr . " does not need parameters");
  130. }
  131. return $f;
  132. }
  133. static function createEnumIndex($e, $index, $params = null) {
  134. $c = _hx_array_get(Type::getEnumConstructs($e), $index);
  135. if($c === null) {
  136. throw new HException(_hx_string_rec($index, "") . " is not a valid enum constructor index");
  137. }
  138. return Type::createEnum($e, $c, $params);
  139. }
  140. static function getInstanceFields($c) {
  141. if($c->__qname__ === "String") {
  142. return new _hx_array(array("substr", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "toLowerCase", "toUpperCase", "toString", "length"));
  143. }
  144. if($c->__qname__ === "Array") {
  145. return new _hx_array(array("push", "concat", "join", "pop", "reverse", "shift", "slice", "sort", "splice", "toString", "copy", "unshift", "insert", "remove", "iterator", "length"));
  146. }
  147. $rfl = $c->__rfl__();
  148. if($rfl === null) return new _hx_array(array());
  149. $r = array();
  150. $internals = array('__construct', '__call', '__get', '__set', '__isset', '__unset', '__toString');
  151. $ms = $rfl->getMethods();
  152. while(list(, $m) = each($ms)) {
  153. $n = $m->getName();
  154. if(!$m->isStatic() && ! in_array($n, $internals)) $r[] = $n;
  155. }
  156. $ps = $rfl->getProperties();
  157. while(list(, $p) = each($ps))
  158. if(!$p->isStatic()) $r[] = $p->getName();
  159. return new _hx_array(array_values(array_unique($r)));
  160. }
  161. static function getClassFields($c) {
  162. if($c->__qname__ === "String") {
  163. return new _hx_array(array("fromCharCode"));
  164. }
  165. if($c->__qname__ === "Array") {
  166. return new _hx_array(array());
  167. }
  168. $rfl = $c->__rfl__();
  169. if($rfl === null) return new _hx_array(array());
  170. $ms = $rfl->getMethods();
  171. $r = array();
  172. while(list(, $m) = each($ms))
  173. if($m->isStatic()) $r[] = $m->getName();
  174. $ps = $rfl->getProperties();
  175. while(list(, $p) = each($ps))
  176. if($p->isStatic()) $r[] = $p->getName();
  177. ;
  178. return new _hx_array(array_unique($r));
  179. }
  180. static function getEnumConstructs($e) {
  181. if($e->__tname__ == 'Bool') {
  182. return new _hx_array(array("true", "false"));
  183. }
  184. if($e->__tname__ == 'Void') {
  185. return new _hx_array(array());
  186. }
  187. return new _hx_array($e->__constructors);
  188. }
  189. static function typeof($v) {
  190. if($v === null) {
  191. return ValueType::$TNull;
  192. }
  193. if(is_array($v)) {
  194. if(is_callable($v)) {
  195. return ValueType::$TFunction;
  196. }
  197. return ValueType::TClass(_hx_qtype("Array"));
  198. }
  199. if(is_string($v)) {
  200. if(_hx_is_lambda($v)) {
  201. return ValueType::$TFunction;
  202. }
  203. return ValueType::TClass(_hx_qtype("String"));
  204. }
  205. if(is_bool($v)) {
  206. return ValueType::$TBool;
  207. }
  208. if(is_int($v)) {
  209. return ValueType::$TInt;
  210. }
  211. if(is_float($v)) {
  212. return ValueType::$TFloat;
  213. }
  214. if($v instanceof _hx_anonymous) {
  215. return ValueType::$TObject;
  216. }
  217. if($v instanceof _hx_enum) {
  218. return ValueType::$TObject;
  219. }
  220. if($v instanceof _hx_class) {
  221. return ValueType::$TObject;
  222. }
  223. $c = _hx_ttype(get_class($v));
  224. if($c instanceof _hx_enum) {
  225. return ValueType::TEnum($c);
  226. }
  227. if($c instanceof _hx_class) {
  228. return ValueType::TClass($c);
  229. }
  230. return ValueType::$TUnknown;
  231. }
  232. static function enumEq($a, $b) {
  233. if($a == $b) {
  234. return true;
  235. }
  236. try {
  237. if(!_hx_equal($a->index, $b->index)) {
  238. return false;
  239. }
  240. {
  241. $_g1 = 0; $_g = count($a->params);
  242. while($_g1 < $_g) {
  243. $i = $_g1++;
  244. if(Type::getEnum($a->params[$i]) !== null) {
  245. if(!Type::enumEq($a->params[$i], $b->params[$i])) {
  246. return false;
  247. }
  248. } else {
  249. if(!_hx_equal($a->params[$i], $b->params[$i])) {
  250. return false;
  251. }
  252. }
  253. unset($i);
  254. }
  255. }
  256. }catch(Exception $»e) {
  257. $_ex_ = ($»e instanceof HException) ? $»e->e : $»e;
  258. $e = $_ex_;
  259. {
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. static function enumConstructor($e) {
  266. return $e->tag;
  267. }
  268. static function enumParameters($e) {
  269. if(_hx_field($e, "params") === null) {
  270. return new _hx_array(array());
  271. } else {
  272. return new _hx_array($e->params);
  273. }
  274. }
  275. static function enumIndex($e) {
  276. return $e->index;
  277. }
  278. static function allEnums($e) {
  279. $all = new _hx_array(array());
  280. {
  281. $_g = 0; $_g1 = Type::getEnumConstructs($e);
  282. while($_g < $_g1->length) {
  283. $c = $_g1[$_g];
  284. ++$_g;
  285. $v = Reflect::field($e, $c);
  286. if(!Reflect::isFunction($v)) {
  287. $all->push($v);
  288. }
  289. unset($v,$c);
  290. }
  291. }
  292. return $all;
  293. }
  294. function __toString() { return 'Type'; }
  295. }