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