Example #1
0
 /**
  * @param $key
  * @param string $default
  * @param Validator|null $validator
  * @return mixed
  * @throws ReactHttpException
  */
 public function get($key, $default = "", $validator = null)
 {
     if (isset($this->parameters[$key])) {
         $value = $this->parameters[$key];
     } else {
         if (isset($_REQUEST[$key])) {
             $value = $_REQUEST[$key];
         } else {
             $value = $this->processDefaultValue($default);
         }
     }
     if ($validator instanceof Validator) {
         if ($validator->execute($value)) {
             return $value;
         } else {
             return $this->processDefaultValue($default);
         }
     } else {
         if (is_callable($validator)) {
             try {
                 if ($validator($value)) {
                     return $value;
                 } else {
                     return $this->processDefaultValue($default);
                 }
             } catch (Exception $e) {
                 throw ReactHttpException::internalError("Validator error");
             }
         }
     }
     return $value;
 }
Example #2
0
 /**
  * Get Uploaded File
  *
  * @param $key
  * @param null $default
  * @param null $validator
  * @return InputFile
  */
 static function file($key, $default = null, $validator = null)
 {
     if (!isset($_FILES[$key]) || $_FILES[$key]['error'] != UPLOAD_ERR_OK) {
         return self::processError($default);
     }
     $file = new InputFile($_FILES[$key]['tmp_name']);
     $file->setContentType($_FILES[$key]['type']);
     $file->setOriginalName($_FILES[$key]['name']);
     if ($validator instanceof Validator) {
         if ($validator->execute($file)) {
             return $file;
         } else {
             return self::processError($default);
         }
     } else {
         if (is_callable($validator)) {
             try {
                 if ($validator($file)) {
                     return $file;
                 } else {
                     return self::processError($default);
                 }
             } catch (Exception $e) {
                 throw ReactHttpException::internalError("Validator error");
             }
         }
     }
     return $file;
 }
Example #3
0
 public function perform(Request $request, Response $response, $jsonData)
 {
     $httpRequest = new HttpRequest($request, $jsonData);
     $method = $request->getMethod();
     $path = $request->getPath();
     if (array_key_exists($method, $this->controllers)) {
         $controllers = $this->controllers[$method];
         $input_paths = $this->getPaths($path);
         /**
          * @var  RoutePath $pathObject
          */
         $target_controller = null;
         $restParams = array();
         foreach ($controllers as $route_path => $pathObject) {
             $restInput = array();
             $check = $this->checkPaths($pathObject->path, $input_paths, $restInput);
             if (!$check) {
                 continue;
             } else {
                 $target_controller = $pathObject->controller;
                 $restParams = $restInput;
                 break;
             }
         }
         if ($target_controller == null) {
             // not found
             throw new ReactHttpException("URL not found!", 404);
         } else {
             foreach ($restParams as $key => $restParam) {
                 $httpRequest->set($key, $restParam);
             }
             $inputs = array($httpRequest, $response);
             $input_params = array_values($restParams);
             $inputs = array_merge($inputs, $input_params);
             if (is_callable($target_controller)) {
                 call_user_func_array($target_controller, $inputs);
             } else {
                 $function_array = preg_split("/@/", $target_controller);
                 if (!isset($function_array[1])) {
                     throw ReactHttpException::internalError('Routing Error');
                 }
                 $class_name = $function_array[0];
                 $method_name = $function_array[1];
                 //$response = $class_name::$method_name();
                 // Initialization controller object
                 $controller = new $class_name();
                 call_user_func_array(array($controller, $method_name), $inputs);
                 //$response = $controller->$method_name();
             }
         }
     } else {
         // not found
         throw new ReactHttpException("URL not found!", 404);
     }
 }