Example #1
0
 /**
  * This method renders the data for the writer.
  *
  * @access public
  * @return string                                           the data as string
  * @throws \Exception                                       indicates a problem occurred
  *                                                          when generating the template
  */
 public function render()
 {
     $delimiter = $this->metadata['delimiter'];
     $enclosure = $this->metadata['enclosure'];
     $escape = $this->metadata['escape'];
     $eol = $this->metadata['eol'];
     $encoding = $this->metadata['encoding'];
     ob_start();
     try {
         if (!empty($this->metadata['template'])) {
             $file = new IO\File($this->metadata['template']);
             $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader($file->getFilePath()), 'escape' => function ($field) use($delimiter, $enclosure, $escape, $encoding) {
                 $value = Core\Data\Charset::encode($field, $encoding[0], $encoding[1]);
                 if ($enclosure != '' && (strpos($value, $delimiter) !== false || strpos($value, $enclosure) !== false || strpos($value, "\n") !== false || strpos($value, "\r") !== false || strpos($value, "\t") !== false || strpos($value, ' ') !== false)) {
                     $literal = $enclosure;
                     $escaped = 0;
                     $length = strlen($value);
                     for ($i = 0; $i < $length; $i++) {
                         if ($value[$i] == $escape) {
                             $escaped = 1;
                         } else {
                             if (!$escaped && $value[$i] == $enclosure) {
                                 $literal .= $enclosure;
                             } else {
                                 $escaped = 0;
                             }
                         }
                         $literal .= $value[$i];
                     }
                     $literal .= $enclosure;
                     return $literal;
                 } else {
                     return Core\Convert::toString($value);
                 }
             }));
             echo $mustache->render($file->getFileName(), $this->data);
         } else {
             if ($this->metadata['header']) {
                 if (!empty($this->metadata['headings'])) {
                     echo static::format($this->metadata['headings'], $delimiter, $enclosure, $escape, $eol, $encoding);
                 } else {
                     if (!empty($this->data)) {
                         echo static::format(array_keys($this->data[0]), $delimiter, $enclosure, $escape, $eol, $encoding);
                     }
                 }
             }
             foreach ($this->data as $values) {
                 echo static::format($values, $delimiter, $enclosure, $escape, $eol, $encoding);
             }
         }
     } catch (\Exception $ex) {
         ob_end_clean();
         throw $ex;
     }
     $template = ob_get_clean();
     return $template;
 }
Example #2
0
 /**
  * This method renders the data for the writer.
  *
  * @access public
  * @return string                                           the data as string
  * @throws \Exception                                       indicates a problem occurred
  *                                                          when generating the template
  */
 public function render()
 {
     $metadata = $this->metadata;
     $declaration = $metadata['declaration'] ? Core\Data\XML::declaration($metadata['encoding'][1], $metadata['standalone']) . $metadata['eol'] : '';
     if (!empty($metadata['template'])) {
         $file = new IO\File($metadata['template']);
         $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader($file->getFilePath()), 'escape' => function ($string) use($metadata) {
             $string = Core\Data\Charset::encode($string, $metadata['encoding'][0], $metadata['encoding'][1]);
             $string = Core\Data\XML::entities($string);
             return $string;
         }));
         ob_start();
         try {
             echo $declaration;
             echo $mustache->render($file->getFileName(), $this->data);
         } catch (\Exception $ex) {
             ob_end_clean();
             throw $ex;
         }
         $template = ob_get_clean();
         if (!empty($metadata['minify'])) {
             $template = Minify\XML::minify($template, $metadata['minify']);
         }
         return $template;
     } else {
         ob_start();
         try {
             $document = new \DOMDocument();
             $document->formatOutput = true;
             $this->toXML($document, $document, $this->data);
             echo $declaration;
             echo $document->saveXML();
         } catch (\Exception $ex) {
             ob_end_clean();
             throw $ex;
         }
         $template = ob_get_clean();
         return $template;
     }
 }
Example #3
0
 /**
  * This method renders the data for the writer.
  *
  * @access public
  * @return string                                           the data as string
  * @throws \Exception                                       indicates a problem occurred
  *                                                          when generating the template
  */
 public function render()
 {
     $declaration = $this->metadata['declaration'] ? '<!DOCTYPE html>' . "\n" : '';
     if (!empty($this->metadata['template'])) {
         $file = new IO\File($this->metadata['template']);
         $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader($file->getFilePath()), 'escape' => function ($string) {
             return htmlentities($string);
         }));
         ob_start();
         try {
             echo $declaration;
             echo $mustache->render($file->getFileName(), $this->data);
         } catch (\Exception $ex) {
             ob_end_clean();
             throw $ex;
         }
         $template = ob_get_clean();
         //if (!empty($this->metadata['minify'])) {
         //	$template = Minify\HTML::minify($template, $this->metadata['minify']);
         //}
         return $template;
     }
     return $declaration;
 }