Completed
Push — develop ( 8f4611...cfeb9d )
by Franck
17:57
created

XMLWriter::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 21
ccs 9
cts 11
cp 0.8182
rs 9.3142
cc 3
eloc 12
nc 4
nop 3
crap 3.054
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-2014 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 = './', $compatibility = false)
56
    {
57
        // Open temporary storage
58 1
        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
59 1
            $this->openMemory();
60
        } else {
61
            // Create temporary filename
62 1
            $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
63
64
            // Open storage
65 1
            $this->openUri($this->tempFileName);
66
        }
67
68 1
        if ($compatibility) {
69
            $this->setIndent(false);
70
            $this->setIndentString('');
71
        } else {
72 1
            $this->setIndent(true);
73 1
            $this->setIndentString('  ');
74
        }
75 1
    }
76
77
    /**
78
     * Destructor
79
     */
80 1
    public function __destruct()
81
    {
82
        // Unlink temporary files
83 1
        if ($this->tempFileName != '') {
84 1
            if (@unlink($this->tempFileName) === false) {
85
                throw new \Exception('The file '.$this->tempFileName.' could not be deleted.');
86
            }
87
        }
88 1
    }
89
90
    /**
91
     * Get written data
92
     *
93
     * @return string
94
     */
95 1
    public function getData()
96
    {
97 1
        if ($this->tempFileName == '') {
98 1
            return $this->outputMemory(true);
99
        } else {
100 1
            $this->flush();
101 1
            return file_get_contents($this->tempFileName);
102
        }
103
    }
104
105
106
    /**
107
     * Write simple element and attribute(s) block
108
     *
109
     * There are two options:
110
     * 1. If the `$attributes` is an array, then it's an associative array of attributes
111
     * 2. If not, then it's a simple attribute-value pair
112
     *
113
     * @param string $element
114
     * @param string|array $attributes
115
     * @param string $value
116
     * @return void
117
     */
118
    public function writeElementBlock($element, $attributes, $value = null)
119
    {
120
        $this->startElement($element);
121
        if (!is_array($attributes)) {
122
            $attributes = array($attributes => $value);
123
        }
124
        foreach ($attributes as $attribute => $value) {
125
            $this->writeAttribute($attribute, $value);
126
        }
127
        $this->endElement();
128
    }
129
130
    /**
131
     * Write element if ...
132
     *
133
     * @param bool $condition
134
     * @param string $element
135
     * @param string $attribute
136
     * @param mixed $value
137
     * @return void
138
     */
139
    public function writeElementIf($condition, $element, $attribute = null, $value = null)
140
    {
141
        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...
142
            if (is_null($attribute)) {
143
                $this->writeElement($element, $value);
144
            } else {
145
                $this->startElement($element);
146
                $this->writeAttribute($attribute, $value);
147
                $this->endElement();
148
            }
149
        }
150
    }
151
152
    /**
153
     * Write attribute if ...
154
     *
155
     * @param bool $condition
156
     * @param string $attribute
157
     * @param mixed $value
158
     * @return void
159
     */
160
    public function writeAttributeIf($condition, $attribute, $value)
161
    {
162
        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...
163
            $this->writeAttribute($attribute, $value);
164
        }
165
    }
166
}
167