72 lines
2.5 KiB
PHP
Raw Normal View History

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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
2014-05-15 14:41:08 +07:00
use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
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
/**
* Write element
*/
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;
}
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
2014-05-11 11:08:39 +02:00
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false;
$xmlWriter = $this->getXmlWriter();
2014-05-07 09:47:02 +07:00
// Loop through elements
$elements = $container->getElements();
$elementClass = '';
foreach ($elements as $element) {
$elementClass = get_class($element);
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);
if (class_exists($writerClass)) {
$writer = new $writerClass($xmlWriter, $element, $withoutP);
$writer->write();
2014-05-07 09:47:02 +07:00
}
}
// Special case for Cell: They have to contain a w:p element at the end
if ($containerClass == 'Cell') {
if ($elementClass == '' || $elementClass == 'PhpOffice\\PhpWord\\Element\\Table') {
2014-05-09 00:28:29 +07:00
$writerClass = "{$this->namespace}\\TextBreak";
2014-05-07 09:47:02 +07:00
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
$writer->write();
}
}
}
}