Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractPart
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 write
n/a
0 / 0
n/a
0 / 0
0
 setParentWriter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParentWriter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getXmlWriter
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 writeText
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Writer\Word2007\Part;
20
21use PhpOffice\PhpWord\Exception\Exception;
22use PhpOffice\PhpWord\Settings;
23use PhpOffice\PhpWord\Shared\XMLWriter;
24use PhpOffice\PhpWord\Writer\AbstractWriter;
25
26/**
27 * Word2007 writer part abstract class.
28 */
29abstract class AbstractPart
30{
31    /**
32     * Parent writer.
33     *
34     * @var AbstractWriter
35     */
36    protected $parentWriter;
37
38    /**
39     * @var string Date format
40     */
41    protected $dateFormat = 'Y-m-d\TH:i:sP';
42
43    /**
44     * Write part.
45     *
46     * @return string
47     */
48    abstract public function write();
49
50    /**
51     * Set parent writer.
52     */
53    public function setParentWriter(?AbstractWriter $writer = null): void
54    {
55        $this->parentWriter = $writer;
56    }
57
58    /**
59     * Get parent writer.
60     *
61     * @return AbstractWriter
62     */
63    public function getParentWriter()
64    {
65        if (null !== $this->parentWriter) {
66            return $this->parentWriter;
67        }
68
69        throw new Exception('No parent WriterInterface assigned.');
70    }
71
72    /**
73     * Get XML Writer.
74     *
75     * @return XMLWriter
76     */
77    protected function getXmlWriter()
78    {
79        $useDiskCaching = false;
80        if (null !== $this->parentWriter) {
81            if ($this->parentWriter->isUseDiskCaching()) {
82                $useDiskCaching = true;
83            }
84        }
85        if ($useDiskCaching) {
86            return new XMLWriter(XMLWriter::STORAGE_DISK, $this->parentWriter->getDiskCachingDirectory(), Settings::hasCompatibility());
87        }
88
89        return new XMLWriter(XMLWriter::STORAGE_MEMORY, './', Settings::hasCompatibility());
90    }
91
92    /**
93     * Write an XML text, this will call text() or writeRaw() depending on the value of Settings::isOutputEscapingEnabled().
94     *
95     * @param string $content The text string to write
96     *
97     * @return bool Returns true on success or false on failure
98     */
99    protected function writeText($content)
100    {
101        if (Settings::isOutputEscapingEnabled()) {
102            return $this->getXmlWriter()->text($content);
103        }
104
105        return $this->getXmlWriter()->writeRaw($content);
106    }
107}