It is used by Response to format response data.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Component, implements yii\web\ResponseFormatterInterface
Ejemplo n.º 1
0
 /**
  * @param mixed  $data the data to be formatted
  * @param string $xml  the expected XML body
  * @dataProvider formatObjectDataProvider
  */
 public function testFormatObjects($data, $xml)
 {
     $head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     $this->response->data = $data;
     $this->formatter->format($this->response);
     $this->assertEquals($head . $xml, $this->response->content);
 }
Ejemplo n.º 2
0
 /**
  * Show export form or generate export file on post
  * @return string
  */
 public function run()
 {
     /** @var Module $module */
     $module = $this->controller->module;
     $model = new ExportForm(['format' => $module->defaultExportFormat]);
     if ($model->load(Yii::$app->request->post())) {
         $fileName = Yii::t('language', 'translations') . '.' . $model->format;
         Yii::$app->response->format = $model->format;
         Yii::$app->response->formatters = [Response::FORMAT_XML => ['class' => XmlResponseFormatter::className(), 'rootTag' => 'translations'], Response::FORMAT_JSON => ['class' => JsonResponseFormatter::className()]];
         Yii::$app->response->setDownloadHeaders($fileName);
         return $model->getExportData();
     } else {
         if (empty($model->languages)) {
             $model->exportLanguages = $model->getDefaultExportLanguages($module->defaultExportStatus);
         }
         return $this->controller->render('export', ['model' => $model]);
     }
 }
Ejemplo n.º 3
0
 /**
  * Prepares for sending the response.
  * The default implementation will convert [[data]] into [[content]] and set headers accordingly.
  * @throws InvalidConfigException if the formatter for the specified format is invalid or [[format]] is not supported
  */
 protected function prepare()
 {
     if ($this->stream !== null || $this->data === null) {
         return;
     }
     if (isset($this->formatters[$this->format])) {
         $formatter = $this->formatters[$this->format];
         if (!is_object($formatter)) {
             $formatter = Yii::createObject($formatter);
         }
         if ($formatter instanceof ResponseFormatterInterface) {
             $formatter->format($this);
         } else {
             throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
         }
     } else {
         switch ($this->format) {
             case self::FORMAT_HTML:
                 $this->getHeaders()->setDefault('Content-Type', 'text/html; charset=' . $this->charset);
                 $this->content = $this->data;
                 break;
             case self::FORMAT_RAW:
                 $this->content = $this->data;
                 break;
             case self::FORMAT_JSON:
                 $this->getHeaders()->set('Content-Type', 'application/json; charset=UTF-8');
                 $this->content = Json::encode($this->data);
                 break;
             case self::FORMAT_JSONP:
                 $this->getHeaders()->set('Content-Type', 'text/javascript; charset=' . $this->charset);
                 if (is_array($this->data) && isset($this->data['data'], $this->data['callback'])) {
                     $this->content = sprintf('%s(%s);', $this->data['callback'], Json::encode($this->data['data']));
                 } else {
                     $this->content = '';
                     Yii::warning("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.", __METHOD__);
                 }
                 break;
             case self::FORMAT_XML:
                 Yii::createObject(XmlResponseFormatter::className())->format($this);
                 break;
             default:
                 throw new InvalidConfigException("Unsupported response format: {$this->format}");
         }
     }
     if (is_array($this->content)) {
         $this->content = 'array()';
     } elseif (is_object($this->content)) {
         $this->content = method_exists($this->content, '__toString') ? $this->content->__toString() : get_class($this->content);
     }
 }