Changeset View
Changeset View
Standalone View
Standalone View
src/parser/PhutilTypeSpec.php
Show First 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | switch ($this->type) { | ||||
if (!is_null($value)) { | if (!is_null($value)) { | ||||
throw new PhutilTypeCheckException($this, $value, $name); | throw new PhutilTypeCheckException($this, $value, $name); | ||||
} | } | ||||
break; | break; | ||||
case 'list': | case 'list': | ||||
if (!is_array($value)) { | if (!is_array($value)) { | ||||
throw new PhutilTypeCheckException($this, $value, $name); | throw new PhutilTypeCheckException($this, $value, $name); | ||||
} | } | ||||
if ($value && (array_keys($value) !== range(0, count($value) - 1))) { | if ($value && !phutil_is_natural_list($value)) { | ||||
throw new PhutilTypeCheckException($this, $value, $name); | throw new PhutilTypeCheckException($this, $value, $name); | ||||
} | } | ||||
try { | try { | ||||
foreach ($value as $v) { | foreach ($value as $v) { | ||||
$this->subtypes[0]->check($v); | $this->subtypes[0]->check($v); | ||||
} | } | ||||
} catch (PhutilTypeCheckException $ex) { | } catch (PhutilTypeCheckException $ex) { | ||||
throw new PhutilTypeCheckException($this, $value, $name); | throw new PhutilTypeCheckException($this, $value, $name); | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | public static function getTypeOf($value) { | ||||
} else if (is_string($value)) { | } else if (is_string($value)) { | ||||
return 'string'; | return 'string'; | ||||
} else if (is_null($value)) { | } else if (is_null($value)) { | ||||
return 'null'; | return 'null'; | ||||
} else if (is_object($value)) { | } else if (is_object($value)) { | ||||
return get_class($value); | return get_class($value); | ||||
} else if (is_array($value)) { | } else if (is_array($value)) { | ||||
$vtype = self::getTypeOfVector($value); | $vtype = self::getTypeOfVector($value); | ||||
if ($value && (array_keys($value) === range(0, count($value) - 1))) { | if ($value && phutil_is_natural_list($value)) { | ||||
return 'list<'.$vtype.'>'; | return 'list<'.$vtype.'>'; | ||||
} else { | } else { | ||||
$ktype = self::getTypeOfVector(array_keys($value)); | $ktype = self::getTypeOfVector(array_keys($value)); | ||||
return "map<{$ktype}, {$vtype}>"; | return "map<{$ktype}, {$vtype}>"; | ||||
} | } | ||||
} else { | } else { | ||||
return 'wild'; | return 'wild'; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,701 Lines • Show Last 20 Lines |