2014-05-07 09:47:02 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2018-03-08 23:52:25 +01:00
|
|
|
* @copyright 2010-2018 PHPWord contributors
|
2014-05-07 09:47:02 +07:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
|
|
|
|
|
|
2021-01-02 08:26:46 +01:00
|
|
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
2014-05-15 14:41:08 +07:00
|
|
|
use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
|
2014-07-03 15:40:24 +04:00
|
|
|
use PhpOffice\PhpWord\Element\AbstractElement as Element;
|
2014-05-15 22:13:03 +07:00
|
|
|
use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement;
|
2014-05-07 09:47:02 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Container element writer (section, textrun, header, footnote, cell, etc.)
|
|
|
|
|
*
|
|
|
|
|
* @since 0.11.0
|
|
|
|
|
*/
|
|
|
|
|
class Container extends AbstractElement
|
|
|
|
|
{
|
2014-05-09 00:28:29 +07:00
|
|
|
/**
|
|
|
|
|
* Namespace; Can't use __NAMESPACE__ in inherited class (ODText)
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $namespace = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element';
|
|
|
|
|
|
2014-05-07 09:47:02 +07:00
|
|
|
/**
|
2014-07-03 15:40:24 +04:00
|
|
|
* Write element.
|
2014-05-07 09:47:02 +07:00
|
|
|
*/
|
|
|
|
|
public function write()
|
|
|
|
|
{
|
2014-05-08 19:25:29 +07:00
|
|
|
$container = $this->getElement();
|
2014-05-15 14:41:08 +07:00
|
|
|
if (!$container instanceof ContainerElement) {
|
2014-05-11 19:41:48 +07:00
|
|
|
return;
|
|
|
|
|
}
|
2014-05-09 14:57:06 +07:00
|
|
|
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
|
2018-01-16 20:19:54 -02:00
|
|
|
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun'));
|
2014-05-13 11:05:12 +07:00
|
|
|
$xmlWriter = $this->getXmlWriter();
|
2014-05-07 09:47:02 +07:00
|
|
|
|
2014-05-11 17:30:23 +07:00
|
|
|
// Loop through elements
|
|
|
|
|
$elements = $container->getElements();
|
|
|
|
|
$elementClass = '';
|
2014-05-13 11:05:12 +07:00
|
|
|
foreach ($elements as $element) {
|
2014-05-23 12:48:34 +07:00
|
|
|
$elementClass = $this->writeElement($xmlWriter, $element, $withoutP);
|
|
|
|
|
}
|
2014-05-18 22:23:28 +07:00
|
|
|
|
2014-05-23 12:48:34 +07:00
|
|
|
// Special case for Cell: They have to contain a w:p element at the end.
|
|
|
|
|
// The $elementClass contains the last element name. If it's empty string
|
|
|
|
|
// or Table, the last element is not w:p
|
|
|
|
|
$writeLastTextBreak = ($containerClass == 'Cell') && ($elementClass == '' || $elementClass == 'Table');
|
|
|
|
|
if ($writeLastTextBreak) {
|
|
|
|
|
$writerClass = $this->namespace . '\\TextBreak';
|
|
|
|
|
/** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */
|
|
|
|
|
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
|
|
|
|
|
$writer->write();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write individual element
|
|
|
|
|
*
|
2021-01-02 08:26:46 +01:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
2014-05-23 12:48:34 +07:00
|
|
|
* @param \PhpOffice\PhpWord\Element\AbstractElement $element
|
|
|
|
|
* @param bool $withoutP
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function writeElement(XMLWriter $xmlWriter, Element $element, $withoutP)
|
|
|
|
|
{
|
|
|
|
|
$elementClass = substr(get_class($element), strrpos(get_class($element), '\\') + 1);
|
|
|
|
|
$writerClass = $this->namespace . '\\' . $elementClass;
|
2014-05-18 22:23:28 +07:00
|
|
|
|
2014-05-23 12:48:34 +07:00
|
|
|
if (class_exists($writerClass)) {
|
|
|
|
|
/** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */
|
|
|
|
|
$writer = new $writerClass($xmlWriter, $element, $withoutP);
|
|
|
|
|
$writer->write();
|
2014-05-07 09:47:02 +07:00
|
|
|
}
|
2014-05-23 12:48:34 +07:00
|
|
|
|
|
|
|
|
return $elementClass;
|
2014-05-07 09:47:02 +07:00
|
|
|
}
|
|
|
|
|
}
|