2012-05-06 18:25:40 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2012-05-06 18:25:40 +02:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2012-05-06 18:25:40 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Shared;
|
|
|
|
|
|
|
|
|
|
use PhpOffice\PhpWord\Settings;
|
|
|
|
|
|
2014-03-28 13:50:53 +07:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-12-11 14:45:44 -05:00
|
|
|
if (!defined('DATE_W3C')) {
|
|
|
|
|
define('DATE_W3C', 'Y-m-d\TH:i:sP');
|
2012-05-06 18:25:40 +02:00
|
|
|
}
|
2014-03-28 13:50:53 +07:00
|
|
|
// @codeCoverageIgnoreEnd
|
2012-05-06 18:25:40 +02:00
|
|
|
|
2013-12-11 14:51:35 -05:00
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* XMLWriter wrapper
|
|
|
|
|
*
|
2014-03-30 01:17:22 +07:00
|
|
|
* @method bool writeElement(string $name, string $content = null)
|
2013-12-11 14:51:35 -05:00
|
|
|
* @method bool startElement(string $name)
|
|
|
|
|
* @method bool writeAttribute(string $name, string $value)
|
|
|
|
|
* @method bool endElement()
|
2014-04-01 15:01:30 +07:00
|
|
|
* @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
|
|
|
|
|
* @method bool text(string $content)
|
2013-12-11 14:51:35 -05:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
class XMLWriter
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-04-25 10:33:45 +07:00
|
|
|
/** Temporary storage location */
|
2013-12-11 14:45:44 -05:00
|
|
|
const STORAGE_MEMORY = 1;
|
|
|
|
|
const STORAGE_DISK = 2;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal XMLWriter
|
|
|
|
|
*
|
2014-03-23 10:32:08 +04:00
|
|
|
* @var \XMLWriter
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-04-04 00:29:57 +07:00
|
|
|
private $xmlWriter;
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Temporary filename
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-04-04 00:29:57 +07:00
|
|
|
private $tempFile = '';
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Create new XMLWriter
|
|
|
|
|
*
|
2014-04-25 10:33:45 +07:00
|
|
|
* @param int $tempLocation Temporary storage location
|
|
|
|
|
* @param string $tempFolder Temporary storage folder
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-04-25 10:33:45 +07:00
|
|
|
public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
|
|
|
|
// Create internal XMLWriter
|
2014-04-04 00:29:57 +07:00
|
|
|
$this->xmlWriter = new \XMLWriter();
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Open temporary storage
|
2014-04-25 10:33:45 +07:00
|
|
|
if ($tempLocation == self::STORAGE_MEMORY) {
|
2014-04-04 00:29:57 +07:00
|
|
|
$this->xmlWriter->openMemory();
|
2013-12-11 14:45:44 -05:00
|
|
|
} else {
|
|
|
|
|
// Create temporary filename
|
2014-04-25 10:33:45 +07:00
|
|
|
$this->tempFile = @tempnam($tempFolder, 'xml');
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Open storage
|
2014-04-04 00:29:57 +07:00
|
|
|
if ($this->xmlWriter->openUri($this->tempFile) === false) {
|
2013-12-11 14:45:44 -05:00
|
|
|
// Fallback to memory...
|
2014-04-04 00:29:57 +07:00
|
|
|
$this->xmlWriter->openMemory();
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 00:17:48 -06:00
|
|
|
// Set xml Compatibility
|
2014-05-04 15:13:31 +07:00
|
|
|
$compatibility = Settings::hasCompatibility();
|
2014-03-13 00:17:48 -06:00
|
|
|
if ($compatibility) {
|
2014-04-04 00:29:57 +07:00
|
|
|
$this->xmlWriter->setIndent(false);
|
|
|
|
|
$this->xmlWriter->setIndentString('');
|
2014-03-13 00:17:48 -06:00
|
|
|
} else {
|
2014-04-04 00:29:57 +07:00
|
|
|
$this->xmlWriter->setIndent(true);
|
|
|
|
|
$this->xmlWriter->setIndentString(' ');
|
2014-03-13 00:17:48 -06:00
|
|
|
}
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
public function __destruct()
|
|
|
|
|
{
|
2014-04-16 17:22:30 +04:00
|
|
|
// Destruct XMLWriter
|
2014-04-04 00:29:57 +07:00
|
|
|
unset($this->xmlWriter);
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Unlink temporary files
|
2014-04-04 00:29:57 +07:00
|
|
|
if ($this->tempFile != '') {
|
|
|
|
|
@unlink($this->tempFile);
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Catch function calls (and pass them to internal XMLWriter)
|
|
|
|
|
*
|
2014-03-17 07:26:25 +07:00
|
|
|
* @param mixed $function
|
|
|
|
|
* @param mixed $args
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public function __call($function, $args)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2014-04-04 00:29:57 +07:00
|
|
|
@call_user_func_array(array($this->xmlWriter, $function), $args);
|
2014-03-23 10:32:08 +04:00
|
|
|
} catch (\Exception $ex) {
|
2013-12-11 14:45:44 -05:00
|
|
|
// Do nothing!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 00:29:57 +07:00
|
|
|
/**
|
|
|
|
|
* Get written data
|
|
|
|
|
*
|
|
|
|
|
* @return string XML data
|
|
|
|
|
*/
|
|
|
|
|
public function getData()
|
|
|
|
|
{
|
|
|
|
|
if ($this->tempFile == '') {
|
|
|
|
|
return $this->xmlWriter->outputMemory(true);
|
|
|
|
|
} else {
|
|
|
|
|
$this->xmlWriter->flush();
|
|
|
|
|
return file_get_contents($this->tempFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
|
|
|
|
* Fallback method for writeRaw, introduced in PHP 5.2
|
|
|
|
|
*
|
|
|
|
|
* @param string $text
|
2014-04-04 00:29:57 +07:00
|
|
|
* @return bool
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public function writeRaw($text)
|
|
|
|
|
{
|
2014-04-04 00:29:57 +07:00
|
|
|
if (isset($this->xmlWriter) && is_object($this->xmlWriter) && (method_exists($this->xmlWriter, 'writeRaw'))) {
|
|
|
|
|
return $this->xmlWriter->writeRaw($text);
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->text($text);
|
|
|
|
|
}
|
2014-05-07 09:47:02 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write element if ...
|
|
|
|
|
*
|
|
|
|
|
* @param bool $condition
|
|
|
|
|
* @param string $element
|
|
|
|
|
* @param string $attribute
|
|
|
|
|
* @param string $value
|
|
|
|
|
*/
|
|
|
|
|
public function writeElementIf($condition, $element, $attribute = null, $value = null)
|
|
|
|
|
{
|
|
|
|
|
if ($condition) {
|
|
|
|
|
if (is_null($attribute)) {
|
|
|
|
|
$this->xmlWriter->writeElement($element, $value);
|
|
|
|
|
} else {
|
|
|
|
|
$this->xmlWriter->startElement($element);
|
|
|
|
|
$this->xmlWriter->writeAttribute($attribute, $value);
|
|
|
|
|
$this->xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write attribute if ...
|
|
|
|
|
*
|
|
|
|
|
* @param bool $condition
|
|
|
|
|
* @param string $attribute
|
|
|
|
|
* @param string $value
|
|
|
|
|
*/
|
|
|
|
|
public function writeAttributeIf($condition, $attribute, $value)
|
|
|
|
|
{
|
|
|
|
|
if ($condition) {
|
|
|
|
|
$this->xmlWriter->writeAttribute($attribute, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-11 19:26:56 +07:00
|
|
|
}
|