• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 16478895114

23 Jul 2025 06:32PM UTC coverage: 96.895% (+0.1%) from 96.757%
16478895114

Pull #2567

github

web-flow
Merge b7b30ac1a into 0ab0b4940
Pull Request #2567: WIP Do Not Install

300 of 307 new or added lines in 26 files covered. (97.72%)

164 existing lines in 23 files now uncovered.

12701 of 13108 relevant lines covered (96.9%)

37.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/PhpWord/Shared/XMLWriter.php
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

19
namespace PhpOffice\PhpWord\Shared;
20

21
use Exception;
22

23
/**
24
 * XMLWriter.
25
 *
26
 * @method bool endElement()
27
 * @method mixed flush(bool $empty = null)
28
 * @method bool openMemory()
29
 * @method string outputMemory(bool $flush = null)
30
 * @method bool setIndent(bool $indent)
31
 * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
32
 * @method bool startElement(string $name)
33
 * @method bool text(string $content)
34
 * @method bool writeCData(string $content)
35
 * @method bool writeComment(string $content)
36
 * @method bool writeElement(string $name, string $content = null)
37
 * @method bool writeRaw(string $content)
38
 */
39
class XMLWriter extends \XMLWriter
40
{
41
    /** Temporary storage method */
42
    const STORAGE_MEMORY = 1;
43
    const STORAGE_DISK = 2;
44

45
    /**
46
     * Temporary filename.
47
     *
48
     * @var string
49
     */
50
    private $tempFileName = '';
51

52
    /**
53
     * Create a new \PhpOffice\PhpWord\Shared\XMLWriter instance.
54
     *
55
     * @param int $pTemporaryStorage Temporary storage location
56
     * @param string $pTemporaryStorageDir Temporary storage folder
57
     * @param bool $compatibility
58
     */
59
    public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
60
    {
61
        // Open temporary storage
62
        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
282✔
63
            $this->openMemory();
279✔
64
        } else {
65
            if (!$pTemporaryStorageDir || !is_dir($pTemporaryStorageDir)) {
4✔
66
                $pTemporaryStorageDir = sys_get_temp_dir();
1✔
67
            }
68
            // Create temporary filename
69
            $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
4✔
70

71
            // Open storage
72
            $this->openUri($this->tempFileName);
4✔
73
        }
74

75
        if ($compatibility) {
282✔
76
            $this->setIndent(false);
257✔
77
            $this->setIndentString('');
257✔
78
        } else {
79
            $this->setIndent(true);
25✔
80
            $this->setIndentString('  ');
25✔
81
        }
82
    }
83

84
    /**
85
     * Destructor.
86
     */
87
    public function __destruct()
88
    {
89
        // Unlink temporary files
90
        if (empty($this->tempFileName)) {
274✔
91
            return;
271✔
92
        }
93
        if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) {
4✔
UNCOV
94
            throw new Exception('The file ' . $this->tempFileName . ' could not be deleted.');
×
95
        }
96
    }
97

98
    /**
99
     * Get written data.
100
     *
101
     * @return string
102
     */
103
    public function getData()
104
    {
105
        if ($this->tempFileName == '') {
282✔
106
            return $this->outputMemory(true);
279✔
107
        }
108

109
        $this->flush();
4✔
110

111
        return file_get_contents($this->tempFileName);
4✔
112
    }
113

114
    /**
115
     * Write simple element and attribute(s) block.
116
     *
117
     * There are two options:
118
     * 1. If the `$attributes` is an array, then it's an associative array of attributes
119
     * 2. If not, then it's a simple attribute-value pair
120
     *
121
     * @param string $element
122
     * @param array|string $attributes
123
     * @param string $value
124
     */
125
    public function writeElementBlock($element, $attributes, $value = null): void
126
    {
127
        $this->startElement($element);
26✔
128
        if (!is_array($attributes)) {
26✔
129
            $attributes = [$attributes => $value];
16✔
130
        }
131
        foreach ($attributes as $attribute => $value) {
26✔
132
            $this->writeAttribute($attribute, $value);
26✔
133
        }
134
        $this->endElement();
26✔
135
    }
136

137
    /**
138
     * Write element if ...
139
     *
140
     * @param bool $condition
141
     * @param string $element
142
     * @param string $attribute
143
     * @param mixed $value
144
     */
145
    public function writeElementIf($condition, $element, $attribute = null, $value = null): void
146
    {
147
        if ($condition == true) {
162✔
148
            if (null === $attribute) {
91✔
149
                $this->writeElement($element, $value);
42✔
150
            } else {
151
                $this->startElement($element);
60✔
152
                $this->writeAttribute($attribute, $value);
60✔
153
                $this->endElement();
60✔
154
            }
155
        }
156
    }
157

158
    /**
159
     * Write attribute if ...
160
     *
161
     * @param bool $condition
162
     * @param string $attribute
163
     * @param mixed $value
164
     */
165
    public function writeAttributeIf($condition, $attribute, $value): void
166
    {
167
        if ($condition == true) {
151✔
168
            $this->writeAttribute($attribute, $value);
113✔
169
        }
170
    }
171

172
    /**
173
     * @param string $name
174
     * @param mixed $value
175
     */
176
    public function writeAttribute($name, $value): bool
177
    {
178
        if (is_float($value)) {
269✔
179
            $value = (string) $value;
156✔
180
        }
181

182
        return parent::writeAttribute($name, $value ?? '');
269✔
183
    }
184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc