/** * Uses the reflected parameters, types and request parameters to execute * the controller * @param Controller $controller the controller to be executed * @param string $methodName the method on the controller that should be executed * @return Response */ private function executeController($controller, $methodName) { $arguments = array(); // valid types that will be casted $types = array('int', 'integer', 'bool', 'boolean', 'float'); foreach ($this->reflector->getParameters() as $param => $default) { // try to get the parameter from the request object and cast // it to the type annotated in the @param annotation $value = $this->request->getParam($param, $default); $type = $this->reflector->getType($param); // if this is submitted using GET or a POST form, 'false' should be // converted to false if (($type === 'bool' || $type === 'boolean') && $value === 'false' && ($this->request->method === 'GET' || strpos($this->request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false)) { $value = false; } elseif ($value !== null && in_array($type, $types)) { settype($value, $type); } $arguments[] = $value; } $response = call_user_func_array(array($controller, $methodName), $arguments); // format response if ($response instanceof DataResponse || !$response instanceof Response) { // get format from the url format or request format parameter $format = $this->request->getParam('format'); // if none is given try the first Accept header if ($format === null) { $headers = $this->request->getHeader('Accept'); $format = $controller->getResponderByHTTPHeader($headers); } $response = $controller->buildResponse($response, $format); } return $response; }
public function testCustomFormatter() { $response = $this->controller->custom('hi'); $response = $this->controller->buildResponse($response, 'json'); $this->assertEquals(array(2), $response->getData()); }