Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Container
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
 writeElement
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Writer\Word2007\Element;
19
20use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
21use PhpOffice\PhpWord\Element\AbstractElement as Element;
22use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement;
23use PhpOffice\PhpWord\Shared\XMLWriter;
24
25/**
26 * Container element writer (section, textrun, header, footnote, cell, etc.).
27 *
28 * @since 0.11.0
29 */
30class Container extends AbstractElement
31{
32    /**
33     * Namespace; Can't use __NAMESPACE__ in inherited class (ODText).
34     *
35     * @var string
36     */
37    protected $namespace = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element';
38
39    /**
40     * @var array<string>
41     */
42    protected $containerWithoutP = ['TextRun', 'Footnote', 'Endnote', 'ListItemRun'];
43
44    /**
45     * Write element.
46     */
47    public function write(): void
48    {
49        $container = $this->getElement();
50        if (!$container instanceof ContainerElement) {
51            return;
52        }
53        $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
54        $withoutP = in_array($containerClass, $this->containerWithoutP);
55        $xmlWriter = $this->getXmlWriter();
56
57        // Loop through elements
58        $elements = $container->getElements();
59        $elementClass = '';
60        foreach ($elements as $element) {
61            $elementClass = $this->writeElement($xmlWriter, $element, $withoutP);
62        }
63
64        // Special case for Cell: They have to contain a w:p element at the end.
65        // The $elementClass contains the last element name. If it's empty string
66        // or Table, the last element is not w:p
67        $writeLastTextBreak = ($containerClass == 'Cell') && ($elementClass == '' || $elementClass == 'Table');
68        if ($writeLastTextBreak) {
69            $writerClass = $this->namespace . '\\TextBreak';
70            /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */
71            $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
72            $writer->write();
73        }
74    }
75
76    /**
77     * Write individual element.
78     */
79    private function writeElement(XMLWriter $xmlWriter, Element $element, bool $withoutP): string
80    {
81        $elementClass = substr(get_class($element), strrpos(get_class($element), '\\') + 1);
82        $writerClass = $this->namespace . '\\' . $elementClass;
83
84        if (class_exists($writerClass)) {
85            /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */
86            $writer = new $writerClass($xmlWriter, $element, $withoutP);
87            $writer->setPart($this->getPart());
88            $writer->write();
89        }
90
91        return $elementClass;
92    }
93}