Пример #1
0
 /**
  * Get varity response use fmt
  *
  * @param array $data
  * @param \Vine\Component\Http\RequestInterface $request
  *
  * @return \Vine\Component\Http\ResponseInterface
  */
 public static function getResponse(array $data, \Vine\Component\Http\RequestInterface $request)
 {
     /*{{{*/
     $fmtValue = $request->getParam(self::FMT_NAME);
     $response = null;
     $factory = new ResponseFactory();
     switch ($fmtValue) {
         case self::FMT_VALUE_DUMP:
             ob_start();
             var_dump($data);
             $content = ob_get_clean();
             $response = $factory->make($content);
             break;
         case self::FMT_VALUE_SERIALIZE:
             $content = serialize($data);
             $response = $factory->make($content);
             break;
         case self::FMT_VALUE_JSONP:
             $callback = $request->getParam(self::CALLBACK_NAME);
             $response = $factory->jsonp($callback, $data);
             break;
         case self::FMT_VALUE_JSON:
         default:
             $response = $factory->json($data);
     }
     return $response;
 }
Пример #2
0
 public function testHttpResponseFactory()
 {
     $responseFactory = new ResponseFactory();
     $response = $responseFactory->make(123);
     $this->assertEquals('123', $response->getContent());
     $responseFactory = new ResponseFactory();
     $response = $responseFactory->json(array('foo' => 'bar'));
     $this->assertEquals('{"foo":"bar"}', $response->getContent());
     $responseFactory = new ResponseFactory();
     $response = $responseFactory->jsonp('_callback', array('foo' => 'bar'));
     $this->assertEquals('/**/_callback({"foo":"bar"});', $response->getContent());
     $responseFactory = new ResponseFactory();
     $response = $responseFactory->redirect('http://www.baidu.com');
     $this->assertEquals('http://www.baidu.com', $response->getTargetUrl());
 }