src/CmsBundle/Classes/PhpOffice/Common/XMLWriter.php line 176

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of PHPOffice Common
  4.  *
  5.  * PHPOffice Common is free software distributed under the terms of the GNU Lesser
  6.  * General Public License version 3 as published by the Free Software Foundation.
  7.  *
  8.  * For the full copyright and license information, please read the LICENSE
  9.  * file that was distributed with this source code. For the full list of
  10.  * contributors, visit https://github.com/PHPOffice/Common/contributors.
  11.  *
  12.  * @link        https://github.com/PHPOffice/Common
  13.  * @copyright   2009-2016 PHPOffice Common contributors
  14.  * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15.  */
  16. namespace App\CmsBundle\Classes\PhpOffice\Common;
  17. /**
  18.  * XMLWriter
  19.  *
  20.  * @method bool endElement()
  21.  * @method mixed flush(bool $empty = null)
  22.  * @method bool openMemory()
  23.  * @method string outputMemory(bool $flush = null)
  24.  * @method bool setIndent(bool $indent)
  25.  * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
  26.  * @method bool startElement(string $name)
  27.  * @method bool text(string $content)
  28.  * @method bool writeCData(string $content)
  29.  * @method bool writeComment(string $content)
  30.  * @method bool writeElement(string $name, string $content = null)
  31.  * @method bool writeRaw(string $content)
  32.  */
  33. class XMLWriter extends \XMLWriter
  34. {
  35.     /** Temporary storage method */
  36.     const STORAGE_MEMORY 1;
  37.     const STORAGE_DISK 2;
  38.     /**
  39.      * Temporary filename
  40.      *
  41.      * @var string
  42.      */
  43.     private $tempFileName '';
  44.     /**
  45.      * Create a new \PhpOffice\PhpPowerpoint\Shared\XMLWriter instance
  46.      *
  47.      * @param int $pTemporaryStorage Temporary storage location
  48.      * @param string $pTemporaryStorageDir Temporary storage folder
  49.      * @param bool $compatibility
  50.      */
  51.     public function __construct($pTemporaryStorage self::STORAGE_MEMORY$pTemporaryStorageDir null$compatibility false)
  52.     {
  53.         // Open temporary storage
  54.         if ($pTemporaryStorage == self::STORAGE_MEMORY) {
  55.             $this->openMemory();
  56.         } else {
  57.             if (!is_dir($pTemporaryStorageDir)) {
  58.                 $pTemporaryStorageDir sys_get_temp_dir();
  59.             }
  60.             // Create temporary filename
  61.             $this->tempFileName = @tempnam($pTemporaryStorageDir'xml');
  62.             // Open storage
  63.             $this->openUri($this->tempFileName);
  64.         }
  65.         if ($compatibility) {
  66.             $this->setIndent(false);
  67.             $this->setIndentString('');
  68.         } else {
  69.             $this->setIndent(true);
  70.             $this->setIndentString('  ');
  71.         }
  72.     }
  73.     /**
  74.      * Destructor
  75.      */
  76.     public function __destruct()
  77.     {
  78.         // Unlink temporary files
  79.         if (empty($this->tempFileName)) {
  80.             return;
  81.         }
  82.         if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) {
  83.             throw new \Exception('The file '.$this->tempFileName.' could not be deleted.');
  84.         }
  85.     }
  86.     /**
  87.      * Get written data
  88.      *
  89.      * @return string
  90.      */
  91.     public function getData()
  92.     {
  93.         if ($this->tempFileName == '') {
  94.             return $this->outputMemory(true);
  95.         }
  96.         $this->flush();
  97.         return file_get_contents($this->tempFileName);
  98.     }
  99.     /**
  100.      * Write simple element and attribute(s) block
  101.      *
  102.      * There are two options:
  103.      * 1. If the `$attributes` is an array, then it's an associative array of attributes
  104.      * 2. If not, then it's a simple attribute-value pair
  105.      *
  106.      * @param string $element
  107.      * @param string|array $attributes
  108.      * @param string $value
  109.      * @return void
  110.      */
  111.     public function writeElementBlock($element$attributes$value null)
  112.     {
  113.         $this->startElement($element);
  114.         if (!is_array($attributes)) {
  115.             $attributes = array($attributes => $value);
  116.         }
  117.         foreach ($attributes as $attribute => $value) {
  118.             $this->writeAttribute($attribute$value);
  119.         }
  120.         $this->endElement();
  121.     }
  122.     /**
  123.      * Write element if ...
  124.      *
  125.      * @param bool $condition
  126.      * @param string $element
  127.      * @param string $attribute
  128.      * @param mixed $value
  129.      * @return void
  130.      */
  131.     public function writeElementIf($condition$element$attribute null$value null)
  132.     {
  133.         if ($condition == true) {
  134.             if (is_null($attribute)) {
  135.                 $this->writeElement($element$value);
  136.             } else {
  137.                 $this->startElement($element);
  138.                 $this->writeAttribute($attribute$value);
  139.                 $this->endElement();
  140.             }
  141.         }
  142.     }
  143.     /**
  144.      * Write attribute if ...
  145.      *
  146.      * @param bool $condition
  147.      * @param string $attribute
  148.      * @param mixed $value
  149.      * @return void
  150.      */
  151.     public function writeAttributeIf($condition$attribute$value)
  152.     {
  153.         if ($condition == true) {
  154.             $this->writeAttribute($attribute$value);
  155.         }
  156.     }
  157.     /**
  158.      * @param string $name
  159.      * @param mixed $value
  160.      * @return bool
  161.      */
  162.     public function writeAttribute($name$value)
  163.     {
  164.         if (is_float($value)) {
  165.             $value json_encode($value);
  166.         }
  167.         return parent::writeAttribute($name$value);
  168.     }
  169. }