public function check()

in src/parser/PhutilTypeSpec.php [55:149]


  public function check($value, $name = null) {
    switch ($this->type) {
      case 'int':
        if (!is_int($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'float':
        if (!is_float($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'bool':
        if (!is_bool($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'string':
        if (!is_string($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'regex':
       if (!is_string($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }

        $trap = new PhutilErrorTrap();
          $ok = @preg_match($value, '');
          $err = $trap->getErrorsAsString();
        $trap->destroy();

        if ($ok === false) {
          throw new PhutilTypeCheckException($this, $value, $name, $err);
        }
        break;
      case 'null':
        if (!is_null($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'list':
        if (!is_array($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        if ($value && !phutil_is_natural_list($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        try {
          foreach ($value as $v) {
            $this->subtypes[0]->check($v);
          }
        } catch (PhutilTypeCheckException $ex) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'map':
        if (!is_array($value)) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        try {
          foreach ($value as $k => $v) {
            $this->subtypes[0]->check($k);
            $this->subtypes[1]->check($v);
          }
        } catch (PhutilTypeCheckException $ex) {
          throw new PhutilTypeCheckException($this, $value, $name);
        }
        break;
      case 'or':
        foreach ($this->subtypes as $subtype) {
          try {
            $subtype->check($value);
            return;
          } catch (PhutilTypeCheckException $ex) {
            // Ignore.
          }
        }
        throw new PhutilTypeCheckException($this, $value, $name);
      case 'wild':
        return;
      default:
        if (class_exists($this->type, false)) {
          if ($value instanceof $this->type) {
            return;
          }
        } else if (interface_exists($this->type, false)) {
          if ($value instanceof $this->type) {
            return;
          }
        }

        throw new PhutilTypeCheckException($this, $value, $name);
    }
  }