rs = $rs; $this->requestQueue = array(); $this->unserialized = $this->unserializeData(); } /** * un-serialize json data if any * @return null|array */ protected function unserializeData(){ if($json = ($this->rs->getCurrentRequest()->getContentType() !== "json")) return array(); $json = $this->rs->getCurrentRequest()->getContent(); if(($v = json_decode($json)) === -1){ return null; } if(!empty($json)){ $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder())); return $serializer->decode($json, 'json'); }else return array(); } /** * @return array */ public function getUnserialized() { return $this->unserialized; } /** * @param $parameter * @param bool $default * @return bool|mixed */ public function getContentValue($parameter, $default = false){ if(isset($this->unserialized[$parameter])) return $this->unserialized[$parameter]; return $default; } /** * Return the method of the actual request * * @return string */ protected function getCurrentMethod(){ return $this->rs->getCurrentRequest()->getMethod(); } /** * @param $method * @param $controllerPointer * @param $functionName * @param null|array $args */ public function map($method, $controllerPointer, $functionName, $args = null){ $this->addToQueue(strtoupper($method),$controllerPointer,$functionName, $args); } /** * @param $method * @param $controllerPointer * @param $functionName * @param $args */ protected function addToQueue($method, $controllerPointer, $functionName, $args): void { if(!isset($this->requestQueue[$method])) $this->requestQueue[$method] = array(); $cb = array( 'controller' => $controllerPointer, 'function' => $functionName, 'args' => $args, ); $this->requestQueue[$method][] = $cb; } /** * Exec given functions for actual http method * * @return JsonResponse */ public function exec(){ $method = strtoupper($this->rs->getCurrentRequest()->getMethod()); if(isset($this->requestQueue[$method]) && \is_array($this->requestQueue[$method])) { foreach ($this->requestQueue[$method] as $callback){ if(isset($callback['args'])){ return \call_user_func(array($callback['controller'],$callback['function']),$callback['args']); }else{ return \call_user_func(array($callback['controller'],$callback['function'])); } } } return new JsonResponse(array()); } public function error($message = 'an error occured', $name = 'error', $description = null){ $array = array(); if(null === $description) $array[$name] = $message; else $array[$name] = array('message' => $message, 'description' => $description); $response = new JsonResponse($array); $response->setStatusCode(600); return $response;//new JsonResponse($array); } }