Completed
Push — develop ( b5198e...bb41d1 )
by Franck
13s
created

XMLWriter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 44.23%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 2
cbo 0
dl 0
loc 135
ccs 23
cts 52
cp 0.4423
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 4
A __destruct() 0 10 4
A writeElementBlock() 0 11 3
A writeElementIf() 0 12 3
A writeAttributeIf() 0 6 2
A getData() 0 9 2
1
<?php
2
/**
3
 * This file is part of PHPOffice Common
4
 *
5
 * PHPOffice Common is free software distributed under the terms of the GNU Lesser
6
 * General Public License version 3 as published by the Free Software Foundation.
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code. For the full list of
10
 * contributors, visit https://github.com/PHPOffice/Common/contributors.
11
 *
12
 * @link        https://github.com/PHPOffice/Common
13
 * @copyright   2009-2016 PHPOffice Common contributors
14
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15
 */
16
17
namespace PhpOffice\Common;
18
19
/**
20
 * XMLWriter
21
 *
22
 * @method bool endElement()
23
 * @method mixed flush(bool $empty = null)
24
 * @method bool openMemory()
25
 * @method string outputMemory(bool $flush = null)
26
 * @method bool setIndent(bool $indent)
27
 * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
28
 * @method bool startElement(string $name)
29
 * @method bool text(string $content)
30
 * @method bool writeAttribute(string $name, mixed $value)
31
 * @method bool writeCData(string $content)
32
 * @method bool writeComment(string $content)
33
 * @method bool writeElement(string $name, string $content = null)
34
 * @method bool writeRaw(string $content)
35
 */
36
class XMLWriter extends \XMLWriter
37
{
38
    /** Temporary storage method */
39
    const STORAGE_MEMORY = 1;
40
    const STORAGE_DISK = 2;
41
42
    /**
43
     * Temporary filename
44
     *
45
     * @var string
46
     */
47
    private $tempFileName = '';
48
49
    /**
50
     * Create a new \PhpOffice\PhpPowerpoint\Shared\XMLWriter instance
51
     *
52
     * @param int $pTemporaryStorage Temporary storage location
53
     * @param string $pTemporaryStorageDir Temporary storage folder
54
     */
55 1
    public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
56
    {
57
        // Open temporary storage
58 1
        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
59 1
            $this->openMemory();
60 1
        } else {
61 1
            if (!is_dir($pTemporaryStorageDir)) {
62 1
                $pTemporaryStorageDir = sys_get_temp_dir();
63 1
            }
64
            // Create temporary filename
65 1
            $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
66
67
            // Open storage
68 1
            $this->openUri($this->tempFileName);
69
        }
70
71 1
        if ($compatibility) {
72
            $this->setIndent(false);
73
            $this->setIndentString('');
74
        } else {
75 1
            $this->setIndent(true);
76 1
            $this->setIndentString('  ');
77
        }
78 1
    }
79
80
    /**
81
     * Destructor
82
     */
83 1
    public function __destruct()
84
    {
85
        // Unlink temporary files
86 1
        if (empty($this->tempFileName)) {
87 1
            return;
88
        }
89 1
        if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) {
90
            throw new \Exception('The file '.$this->tempFileName.' could not be deleted.');
91
        }
92 1
    }
93
94
    /**
95
     * Get written data
96
     *
97
     * @return string
98
     */
99 1
    public function getData()
100
    {
101 1
        if ($this->tempFileName == '') {
102 1
            return $this->outputMemory(true);
103
        }
104
105 1
        $this->flush();
106 1
        return file_get_contents($this->tempFileName);
107
    }
108
109
110
    /**
111
     * Write simple element and attribute(s) block
112
     *
113
     * There are two options:
114
     * 1. If the `$attributes` is an array, then it's an associative array of attributes
115
     * 2. If not, then it's a simple attribute-value pair
116
     *
117
     * @param string $element
118
     * @param string|array $attributes
119
     * @param string $value
120
     * @return void
121
     */
122
    public function writeElementBlock($element, $attributes, $value = null)
123
    {
124
        $this->startElement($element);
125
        if (!is_array($attributes)) {
126
            $attributes = array($attributes => $value);
127
        }
128
        foreach ($attributes as $attribute => $value) {
129
            $this->writeAttribute($attribute, $value);
130
        }
131
        $this->endElement();
132
    }
133
134
    /**
135
     * Write element if ...
136
     *
137
     * @param bool $condition
138
     * @param string $element
139
     * @param string $attribute
140
     * @param mixed $value
141
     * @return void
142
     */
143
    public function writeElementIf($condition, $element, $attribute = null, $value = null)
144
    {
145
        if ($condition == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
146
            if (is_null($attribute)) {
147
                $this->writeElement($element, $value);
148
            } else {
149
                $this->startElement($element);
150
                $this->writeAttribute($attribute, $value);
151
                $this->endElement();
152
            }
153
        }
154
    }
155
156
    /**
157
     * Write attribute if ...
158
     *
159
     * @param bool $condition
160
     * @param string $attribute
161
     * @param mixed $value
162
     * @return void
163
     */
164
    public function writeAttributeIf($condition, $attribute, $value)
165
    {
166
        if ($condition == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
167
            $this->writeAttribute($attribute, $value);
168
        }
169
    }
170
}
171