示例#1
0
 /**
  * Type casting
  *
  * @param mixed $value
  * @param Filter $filter
  *
  * @throws TypeCastingException
  */
 public function toType(&$value, Filter $filter = null)
 {
     $type = gettype($value);
     switch ($this->type) {
         case "callable":
             if (!is_callable($value)) {
                 throw Error::invalidType($this, $type);
             }
             return;
         case "object":
             if (is_a($value, $this->class)) {
                 return;
             } elseif ($filter && $filter->factory) {
                 $value = $filter->factory($this, $value);
                 if (!is_a($value, $this->class)) {
                     throw Error::invalidType($this, $type);
                 }
                 return;
             } else {
                 throw Error::invalidType($this, $type);
             }
         case "array":
             if (!is_array($value)) {
                 throw Error::invalidType($this, $type);
             }
             return;
     }
     if ($type == "object" || $type == "array") {
         throw Error::invalidType($this, $type);
     }
     switch ($this->type) {
         case "int":
         case "float":
             if (!is_numeric($value)) {
                 throw Error::invalidType($this, $type);
             } else {
                 settype($value, $this->type);
             }
             break;
         default:
             settype($value, $this->type);
     }
 }
示例#2
0
 /**
  * Import callable info from reflection
  *
  * @param \ReflectionFunctionAbstract $method
  *
  * @return static
  */
 protected function _importFromReflection(\ReflectionFunctionAbstract $method)
 {
     $doc = $method->getDocComment();
     $doc_params = [];
     $this->return = new ReturnInfo($this);
     if ($doc) {
         $doc = preg_replace('/^\\s*(\\*\\s*)+/mS', '', trim($doc, "*/ \t\n\r"));
         if (strpos($doc, "@") !== false) {
             $doc = explode("@", $doc, 2);
             if ($doc[0] = trim($doc[0])) {
                 $this->desc = $doc[0];
             }
             if ($doc[1]) {
                 foreach (preg_split('/\\r?\\n@/mS', $doc[1]) as $param) {
                     $param = preg_split('/\\s+/S', $param, 2);
                     if (!isset($param[1])) {
                         $param[1] = "";
                     }
                     switch (strtolower($param[0])) {
                         case 'description':
                             if (empty($this->desc)) {
                                 $this->desc = $param[1];
                             }
                             break;
                         case 'param':
                             if (preg_match('/^(.*?)\\s+\\$(\\w+)\\s*(?:\\(([^\\)]+)\\))?/mS', $param[1], $matches)) {
                                 $this->params[$matches[2]] = ["type" => $matches[1], "desc" => trim(substr($param[1], strlen($matches[0]))), "filters" => isset($matches[3]) ? Filter::parseDoc($matches[3]) : []];
                             }
                             break;
                         case 'return':
                             $return = preg_split('~\\s+~mS', $param[1], 2);
                             if ($return) {
                                 $this->return->type = $return[0];
                                 if (count($return) == 2) {
                                     $this->return->desc = $return[1];
                                 }
                             }
                             break;
                         default:
                             if (isset($this->options[$param[0]])) {
                                 $this->options[$param[0]][] = $param[1];
                             } else {
                                 $this->options[$param[0]] = [$param[1]];
                             }
                     }
                 }
             }
         } else {
             $this->desc = $doc;
         }
     }
     foreach ($method->getParameters() as $param) {
         $this->args[$param->name] = $arg = new ArgumentInfo($this);
         $arg->import($param);
     }
     return $this;
 }