Esempio n. 1
0
 static function typeof($v)
 {
     if ($v === null) {
         return ValueType::$TNull;
     }
     if (is_array($v)) {
         if (is_callable($v)) {
             return ValueType::$TFunction;
         }
         return ValueType::TClass(_hx_qtype("Array"));
     }
     if (is_string($v)) {
         if (_hx_is_lambda($v)) {
             return ValueType::$TFunction;
         }
         return ValueType::TClass(_hx_qtype("String"));
     }
     if (is_bool($v)) {
         return ValueType::$TBool;
     }
     if (is_int($v)) {
         return ValueType::$TInt;
     }
     if (is_float($v)) {
         return ValueType::$TFloat;
     }
     if ($v instanceof _hx_anonymous) {
         return ValueType::$TObject;
     }
     if ($v instanceof _hx_enum) {
         return ValueType::$TObject;
     }
     if ($v instanceof _hx_class) {
         return ValueType::$TObject;
     }
     $c = _hx_ttype(get_class($v));
     if ($c instanceof _hx_enum) {
         return ValueType::TEnum($c);
     }
     if ($c instanceof _hx_class) {
         return ValueType::TClass($c);
     }
     return ValueType::$TUnknown;
 }
Esempio n. 2
0
function _hx_string_rec($o, $s)
{
    if ($o === null) {
        return 'null';
    }
    if (strlen($s) >= 5) {
        return '<...>';
    }
    if (is_int($o) || is_float($o)) {
        return '' . $o;
    }
    if (is_bool($o)) {
        return $o ? 'true' : 'false';
    }
    if (is_object($o)) {
        $c = get_class($o);
        if ($o instanceof Enum) {
            $b = $o->tag;
            if (!empty($o->params)) {
                $s .= "\t";
                $b .= '(';
                for ($i = 0; $i < count($o->params); $i++) {
                    if ($i > 0) {
                        $b .= ',' . _hx_string_rec($o->params[$i], $s);
                    } else {
                        $b .= _hx_string_rec($o->params[$i], $s);
                    }
                }
                $b .= ')';
            }
            return $b;
        } else {
            if ($o instanceof _hx_anonymous) {
                if ($o->toString && is_callable($o->toString)) {
                    return call_user_func($o->toString);
                }
                $rfl = new ReflectionObject($o);
                $b2 = "{\n";
                $s .= "\t";
                $properties = $rfl->getProperties();
                for ($i = 0; $i < count($properties); $i++) {
                    $prop = $properties[$i];
                    $f = $prop->getName();
                    if ($i > 0) {
                        $b2 .= ", \n";
                    }
                    $b2 .= $s . $f . ' : ' . _hx_string_rec($o->{$f}, $s);
                }
                $s = substr($s, 1);
                $b2 .= "\n" . $s . '}';
                return $b2;
            } else {
                if ($o instanceof _hx_type) {
                    return $o->__qname__;
                } else {
                    if (is_callable(array($o, 'toString'))) {
                        return $o->toString();
                    } else {
                        if (is_callable(array($o, '__toString'))) {
                            return $o->__toString();
                        } else {
                            return '[' . _hx_ttype($c) . ']';
                        }
                    }
                }
            }
        }
    }
    if (is_string($o)) {
        if (_hx_is_lambda($o)) {
            return '<function>';
        } else {
            return $o;
        }
    }
    if (is_array($o)) {
        if (is_callable($o)) {
            return '<function>';
        }
        $str = '[';
        $s .= "\t";
        $first = true;
        $assoc = true;
        foreach ($o as $k => $v) {
            if ($first && $k === 0) {
                $assoc = false;
            }
            $str .= ($first ? '' : ',') . ($assoc ? _hx_string_rec($k, $s) . '=>' . _hx_string_rec($o[$k], $s) : _hx_string_rec($o[$k], $s));
            $first = false;
        }
        $str .= ']';
        return $str;
    }
    return '';
}
function _hx_string_rec($o, $s)
{
    if ($o === null) {
        return 'null';
    }
    if (strlen($s) >= 5) {
        return '<...>';
    }
    if (is_int($o) || is_float($o)) {
        return $o;
    }
    if (is_bool($o)) {
        return $o ? 'true' : 'false';
    }
    if (is_object($o)) {
        $c = get_class($o);
        if ($o instanceof Enum) {
            $b = $o->tag;
            if (!empty($o->params)) {
                $s .= "\t";
                $b .= '(';
                for ($i = 0; $i < count($o->params); $i++) {
                    if ($i > 0) {
                        $b .= ', ' . _hx_string_rec($o->params[$i], $s);
                    } else {
                        $b .= _hx_string_rec($o->params[$i], $s);
                    }
                }
                $b .= ')';
            }
            return $b;
        } else {
            if ($o instanceof _hx_anonymous) {
                $rfl = new ReflectionObject($o);
                $b2 = "{\n";
                $s .= "\t";
                $properties = $rfl->getProperties();
                for ($i = 0; $i < count($properties); $i++) {
                    $prop = $properties[$i];
                    $f = $prop->getName();
                    if ($i > 0) {
                        $b2 .= ", \n";
                    }
                    $b2 .= $s . $f . ' : ' . _hx_string_rec($o->{$f}, $s);
                }
                $s = substr($s, 1);
                $b2 .= "\n" . $s . '}';
                return $b2;
            } else {
                if ($o instanceof _hx_type) {
                    return $o->__qname__;
                } else {
                    if (is_callable(array($o, 'toString'))) {
                        return $o->toString();
                    } else {
                        if (is_callable(array($o, '__toString'))) {
                            return $o->__toString();
                        } else {
                            return '[' . _hx_ttype($c) . ']';
                        }
                    }
                }
            }
        }
    }
    if (is_string($o)) {
        if (_hx_is_lambda($o)) {
            return '«function»';
        }
        if (strlen($s) > 0) {
            return '"' . str_replace('"', '\\"', $o) . '"';
        } else {
            return $o;
        }
    }
    if (is_array($o)) {
        if (is_callable($o)) {
            return '«function»';
        }
        $str = '[';
        $s .= "\t";
        for ($i = 0; $i < count($o); $i++) {
            $str .= ($i > 0 ? ', ' : '') . _hx_string_rec($o[$i], $s);
        }
        $str .= ']';
        return $str;
    }
    return '';
}