Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
XMLWriter
94.44% covered (success)
94.44%
17 / 18
75.00% covered (warning)
75.00%
3 / 4
8.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 __destruct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __call
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 getData
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * This file is part of PHPProject - A pure PHP library for reading and writing
5 * presentations documents.
6 *
7 * PHPProject 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 * @link        https://github.com/PHPOffice/PHPProject
15 * @copyright   2009-2014 PHPProject contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpProject\Shared;
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 writeAttribute(string $name, mixed $value)
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 */
40class XMLWriter
41{
42    /** Temporary storage method */
43    const STORAGE_MEMORY = 1;
44    const STORAGE_DISK = 2;
45
46    /**
47     * Internal XMLWriter
48     *
49     * @var \XMLWriter
50     */
51    private $xmlWriter;
52
53    /**
54     * Temporary filename
55     *
56     * @var string
57     */
58    private $tempFileName = '';
59
60    /**
61     * Create a new \PhpOffice\PhpPowerpoint\Shared\XMLWriter instance
62     *
63     * @param int $pTemporaryStorage Temporary storage location
64     * @param string $pTemporaryStorageDir Temporary storage folder
65     */
66    public function __construct(int $pTemporaryStorage = self::STORAGE_MEMORY, string $pTemporaryStorageDir = './')
67    {
68        // Create internal XMLWriter
69        $this->xmlWriter = new \XMLWriter();
70
71        // Open temporary storage
72        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
73            $this->xmlWriter->openMemory();
74        } else {
75            // Create temporary filename
76            $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
77
78            // Open storage
79            $this->xmlWriter->openUri($this->tempFileName);
80        }
81
82        // Set default values
83        $this->xmlWriter->setIndent(true);
84    }
85
86    /**
87     * Destructor
88     */
89    public function __destruct()
90    {
91        // Desctruct XMLWriter
92        unset($this->xmlWriter);
93
94        // Unlink temporary files
95        if ($this->tempFileName != '') {
96            @unlink($this->tempFileName);
97        }
98    }
99
100    /**
101     * Catch function calls (and pass them to internal XMLWriter)
102     *
103     * @param string $function
104     * @param array $args
105     */
106    public function __call(string $function, array $args)
107    {
108        try {
109            @call_user_func_array(array(
110                $this->xmlWriter,
111                $function
112            ), $args);
113        } catch (\Exception $ex) {
114            // Do nothing!
115        }
116    }
117
118    /**
119     * Get written data
120     *
121     * @return string
122     */
123    public function getData(): string
124    {
125        if ($this->tempFileName == '') {
126            return $this->xmlWriter->outputMemory(true);
127        } else {
128            $this->xmlWriter->flush();
129            return file_get_contents($this->tempFileName);
130        }
131    }
132}