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

PHPOffice / PHPWord / 13025401639

29 Jan 2025 05:48AM UTC coverage: 96.825% (-0.4%) from 97.217%
13025401639

Pull #2562

github

web-flow
Merge ebe3e5dac into 2d2759585
Pull Request #2562: TemplateProcessor SetComplexBlock/Value and Sections

6 of 7 new or added lines in 1 file covered. (85.71%)

245 existing lines in 40 files now uncovered.

12227 of 12628 relevant lines covered (96.82%)

34.56 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
use ReturnTypeWillChange;
23

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

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

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

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

76
        if ($compatibility) {
247✔
77
            $this->setIndent(false);
239✔
78
            $this->setIndentString('');
239✔
79
        } else {
80
            $this->setIndent(true);
8✔
81
            $this->setIndentString('  ');
8✔
82
        }
83
    }
84

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

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

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

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

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

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

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

173
    /**
174
     * @param string $name
175
     * @param mixed $value
176
     *
177
     * @return bool
178
     */
179
    #[ReturnTypeWillChange]
180
    public function writeAttribute($name, $value)
181
    {
182
        if (is_float($value)) {
241✔
183
            $value = json_encode($value);
146✔
184
        }
185

186
        return parent::writeAttribute($name, $value ?? '');
241✔
187
    }
188
}
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