Exemplo n.º 1
0
 /**
  * Scan function entry
  *
  * @param mixed $name The name of the function to reflect or a closure.
  *
  * @return self
  * @throws CallableNotFoundException
  */
 public static function scan($name)
 {
     try {
         $fe = new \ReflectionFunction($name);
     } catch (\Exception $e) {
         throw Error::functionNotFound($name);
     }
     $info = new static();
     $info->import($fe);
     return $info;
 }
Exemplo n.º 2
0
 /**
  * Scan method
  *
  * @param mixed $class class name or object
  * @param string $method
  *
  * @return MethodInfo
  * @throws CallableNotFoundException
  */
 public static function scan($class, $method)
 {
     try {
         $me = new \ReflectionMethod($class, $method);
     } catch (\Exception $e) {
         throw Error::methodNotFound($class . "::" . $method);
     }
     $info = new static();
     $info->import($me);
     return $info;
 }
Exemplo n.º 3
0
 /**
  * @param array $params
  * @param Filter $filter
  *
  * @return array
  * @throws \Koda\Error\TypeCastingException
  * @throws InvalidArgumentException
  */
 public function filterArgs(array $params, Filter $filter)
 {
     $args = [];
     foreach ($this->args as $name => $arg) {
         if ($arg->inject) {
             $params[$name] = $filter->injection($arg, isset($params[$name]) ? $params[$name] : null);
         }
         if (isset($params[$name])) {
             $param = $params[$name];
             $args[] = $arg->filter($param, $filter);
             unset($params[$name]);
         } elseif (isset($params[$arg->position])) {
             $param = $params[$arg->position];
             $args[] = $arg->filter($param, $filter);
             unset($params[$arg->position]);
         } elseif ($arg->optional) {
             $args[] = $arg->default;
             continue;
         } else {
             throw Error::argumentRequired($arg);
         }
     }
     return $args;
 }
Exemplo n.º 4
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);
     }
 }
Exemplo n.º 5
0
 public function factory(ArgumentInfo $info, $value = null)
 {
     try {
         return call_user_func($this->factory, $info, $value);
     } catch (\Exception $e) {
         throw Error::factoryFailed($info, $e);
     }
 }