Esempio n. 1
0
 /**
  * getProperties
  *
  * @param Request  $request
  * @param Response $response
  *
  * @return array
  * @throws ResponseFormatException
  */
 protected function getProperties(Request $request, Response $response)
 {
     $options = $this->getOptions($request);
     $dataModel = $this->getDataModel($response);
     $fileBase64Property = $options->get('fileBase64Property');
     if (empty($fileBase64Property)) {
         throw new ResponseFormatException('FileDataResponseFormat requires fileBase64Property option to be set');
     }
     $propertyList = [$fileBase64Property => true];
     $fileContentTypeProperty = $options->get('fileContentTypeProperty');
     if (!empty($fileContentTypeProperty)) {
         $propertyList[$fileContentTypeProperty] = true;
     }
     $fileNameProperty = $options->get('fileNameProperty');
     if (!empty($fileNameProperty)) {
         $propertyList[$fileNameProperty] = true;
     }
     $extractorOptions = new BasicOptions(['propertyList' => $propertyList]);
     $properties = $this->extractor->extract($dataModel, $extractorOptions);
     return ['file' => base64_decode($this->getProperty($properties, $fileBase64Property)), 'contentType' => $this->getProperty($properties, $fileContentTypeProperty, self::DOWNLOAD_HEADER), 'fileName' => $this->getProperty($properties, $fileNameProperty)];
 }
Esempio n. 2
0
 /**
  * __invoke
  *
  * @param Request       $request
  * @param Response      $response
  * @param callable|null $next
  *
  * @return static
  * @throws ResponseFormatException
  */
 public function __invoke(Request $request, Response $response, callable $next = null)
 {
     if (!$this->isValidAcceptType($request)) {
         return $next($request, $response);
     }
     // @todo finish me
     throw new ResponseFormatException(get_class($this) . ' not ready for use');
     $dataModel = $this->getDataModel($response, null);
     if (!is_array($dataModel)) {
         throw new ResponseFormatException(get_class($this) . ' requires dataModel to be an array');
     }
     $templateConfig = $this->getTemplateConfig($request);
     $propertyList = $this->getFormatProperties($templateConfig);
     $extractorOptions = new BasicOptions(['propertyList' => $propertyList]);
     $properties = $this->extractor->extract($dataModel, $extractorOptions);
     $containerTag = $this->getOption($request, 'containerTag', 'div');
     $containerAttributes = $this->getOption($request, 'containerAttributes', []);
     $body = $response->getBody();
     $content = $this->buildMarkup($properties, $templateConfig, $containerTag, $containerAttributes);
     $body->write($content);
     return $response->withBody($body)->withHeader('Content-Type', 'text/html');
 }