Esempio n. 1
0
 /**
  * @param string $key
  * @param $value
  * @return Response
  * @throws RequestException
  */
 public function set(string $key, $value) : self
 {
     if (is_object($value)) {
         // Convert object to array
         $value = json_decode(json_encode($value), true);
     }
     if (!is_scalar($value) && !is_array($value) && !is_null($value)) {
         throw RequestException::setBadData(__METHOD__, $key, gettype($value));
     }
     $this->count++;
     $this->data[$key] = $value;
     return $this;
 }
Esempio n. 2
0
 /**
  * Request constructor.
  * @param string $method
  * @param string $uri
  * @param Input $input
  * @param callable|null $callback
  */
 public function __construct(string $method, string $uri, Input $input, callable $callback = null)
 {
     // Get Router instance
     $router = Router::getInstance();
     // Method must be from GET, POST, PUT or DELETE
     $method = strtoupper($method);
     if (!in_array($method, ["GET", "POST", "PUT", "DELETE"])) {
         RequestException::badMethod($method);
     }
     // Resolve $uri to controller's instance
     $controller = $router->route($uri);
     // Save request information
     $this->controller = $controller;
     $this->input = $input;
     $this->method = $method;
     $this->uri = explode("/", trim($uri, "/"));
     $this->response = new Response($this);
     // Call init method of Controller
     call_user_func_array([$controller, "init"], [$this, $this->response]);
     // Callback
     if (isset($callback)) {
         call_user_func_array($callback, [$this, $this->response]);
     }
 }