Completed
Push — master ( 606103...99816b )
by Adrien
02:35
created

SharedStringsHelper::getSharedStringsFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Box\Spout\Reader\XLSX\Helper;
4
5
use Box\Spout\Common\Exception\IOException;
6
use Box\Spout\Reader\Exception\XMLProcessingException;
7
use Box\Spout\Reader\Wrapper\XMLReader;
8
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory;
9
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyInterface;
10
11
/**
12
 * Class SharedStringsHelper
13
 * This class provides helper functions for reading sharedStrings XML file
14
 *
15
 * @package Box\Spout\Reader\XLSX\Helper
16
 */
17
class SharedStringsHelper
18
{
19
    /** Path of sharedStrings XML file inside the XLSX file */
20
    const SHARED_STRINGS_XML_FILE_PATH = 'xl/sharedStrings.xml';
21
22
    /** Main namespace for the sharedStrings.xml file */
23
    const MAIN_NAMESPACE_FOR_SHARED_STRINGS_XML = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
24
25
    /** Definition of XML nodes names used to parse data */
26
    const XML_NODE_SST = 'sst';
27
    const XML_NODE_SI = 'si';
28
    const XML_NODE_R = 'r';
29
    const XML_NODE_T = 't';
30
31
    /** Definition of XML attributes used to parse data */
32
    const XML_ATTRIBUTE_COUNT = 'count';
33
    const XML_ATTRIBUTE_UNIQUE_COUNT = 'uniqueCount';
34
    const XML_ATTRIBUTE_XML_SPACE = 'xml:space';
35
    const XML_ATTRIBUTE_VALUE_PRESERVE = 'preserve';
36
37
    /** @var string Path of the XLSX file being read */
38
    protected $filePath;
39
40
    /** @var string Temporary folder where the temporary files to store shared strings will be stored */
41
    protected $tempFolder;
42
43
    /** @var CachingStrategyInterface The best caching strategy for storing shared strings */
44
    protected $cachingStrategy;
45
46
    /**
47
     * @param string $filePath Path of the XLSX file being read
48
     * @param string|null|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored
49
     */
50 123
    public function __construct($filePath, $tempFolder = null)
51
    {
52 123
        $this->filePath = $filePath;
53 123
        $this->tempFolder = $tempFolder;
54 123
    }
55
56
    /**
57
     * Returns whether the XLSX file contains a shared strings XML file
58
     *
59
     * @return bool
60
     */
61 108
    public function hasSharedStrings()
62
    {
63 105
        $hasSharedStrings = false;
64 105
        $zip = new \ZipArchive();
65
66 105
        if ($zip->open($this->filePath) === true) {
67 105
            $hasSharedStrings = ($zip->locateName(self::SHARED_STRINGS_XML_FILE_PATH) !== false);
68 105
            $zip->close();
69 108
        }
70
71 105
        return $hasSharedStrings;
72
    }
73
74
    /**
75
     * Builds an in-memory array containing all the shared strings of the sheet.
76
     * All the strings are stored in a XML file, located at 'xl/sharedStrings.xml'.
77
     * It is then accessed by the sheet data, via the string index in the built table.
78
     *
79
     * More documentation available here: http://msdn.microsoft.com/en-us/library/office/gg278314.aspx
80
     *
81
     * The XML file can be really big with sheets containing a lot of data. That is why
82
     * we need to use a XML reader that provides streaming like the XMLReader library.
83
     *
84
     * @return void
85
     * @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml can't be read
86
     */
87 105
    public function extractSharedStrings()
88
    {
89 105
        $xmlReader = new XMLReader();
90 105
        $sharedStringIndex = 0;
91
92 105
        if ($xmlReader->openFileInZip($this->filePath, self::SHARED_STRINGS_XML_FILE_PATH) === false) {
93
            throw new IOException('Could not open "' . self::SHARED_STRINGS_XML_FILE_PATH . '".');
94
        }
95
96
        try {
97 105
            $sharedStringsUniqueCount = $this->getSharedStringsUniqueCount($xmlReader);
98 102
            $this->cachingStrategy = $this->getBestSharedStringsCachingStrategy($sharedStringsUniqueCount);
99
100 102
            $xmlReader->readUntilNodeFound(self::XML_NODE_SI);
101
102 102
            while ($xmlReader->name === self::XML_NODE_SI) {
103 72
                $this->processSharedStringsItem($xmlReader, $sharedStringIndex);
104 72
                $sharedStringIndex++;
105
106
                // jump to the next '<si>' tag
107 72
                $xmlReader->next(self::XML_NODE_SI);
108 72
            }
109
110 102
            $this->cachingStrategy->closeCache();
111
112 105
        } catch (XMLProcessingException $exception) {
113 3
            throw new IOException("The sharedStrings.xml file is invalid and cannot be read. [{$exception->getMessage()}]");
114
        }
115
116 102
        $xmlReader->close();
117 102
    }
118
119
    /**
120
     * Returns the shared strings unique count, as specified in <sst> tag.
121
     *
122
     * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader instance
123
     * @return int|null Number of unique shared strings in the sharedStrings.xml file
124
     * @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml is invalid and can't be read
125
     */
126 105
    protected function getSharedStringsUniqueCount($xmlReader)
127
    {
128 105
        $xmlReader->next(self::XML_NODE_SST);
129
130
        // Iterate over the "sst" elements to get the actual "sst ELEMENT" (skips any DOCTYPE)
131 102
        while ($xmlReader->name === self::XML_NODE_SST && $xmlReader->nodeType !== XMLReader::ELEMENT) {
132 3
            $xmlReader->read();
133 3
        }
134
135 102
        $uniqueCount = $xmlReader->getAttribute(self::XML_ATTRIBUTE_UNIQUE_COUNT);
136
137
        // some software do not add the "uniqueCount" attribute but only use the "count" one
138
        // @see https://github.com/box/spout/issues/254
139 102
        if ($uniqueCount === null) {
140 9
            $uniqueCount = $xmlReader->getAttribute(self::XML_ATTRIBUTE_COUNT);
141 9
        }
142
143 102
        return ($uniqueCount !== null) ? intval($uniqueCount) : null;
144
    }
145
146
    /**
147
     * Returns the best shared strings caching strategy.
148
     *
149
     * @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown)
150
     * @return CachingStrategyInterface
151
     */
152 102
    protected function getBestSharedStringsCachingStrategy($sharedStringsUniqueCount)
153
    {
154 102
        return CachingStrategyFactory::getInstance()
155 102
                ->getBestCachingStrategy($sharedStringsUniqueCount, $this->tempFolder);
156
    }
157
158
    /**
159
     * Processes the shared strings item XML node which the given XML reader is positioned on.
160
     *
161
     * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XML Reader positioned on a "<si>" node
162
     * @param int $sharedStringIndex Index of the processed shared strings item
163
     * @return void
164
     */
165 72
    protected function processSharedStringsItem($xmlReader, $sharedStringIndex)
166
    {
167 72
        $sharedStringValue = '';
168
169
        // NOTE: expand() will automatically decode all XML entities of the child nodes
170 72
        $siNode = $xmlReader->expand();
171 72
        $textNodes = $siNode->getElementsByTagName(self::XML_NODE_T);
172
173 72
        foreach ($textNodes as $textNode) {
174 72
            if ($this->shouldExtractTextNodeValue($textNode)) {
175 72
                $textNodeValue = $textNode->nodeValue;
176 72
                $shouldPreserveWhitespace = $this->shouldPreserveWhitespace($textNode);
177
178 72
                $sharedStringValue .= ($shouldPreserveWhitespace) ? $textNodeValue : trim($textNodeValue);
179 72
            }
180 72
        }
181
182 72
        $this->cachingStrategy->addStringForIndex($sharedStringValue, $sharedStringIndex);
183 72
    }
184
185
    /**
186
     * Not all text nodes' values must be extracted.
187
     * Some text nodes are part of a node describing the pronunciation for instance.
188
     * We'll only consider the nodes whose parents are "<si>" or "<r>".
189
     *
190
     * @param \DOMElement $textNode Text node to check
191
     * @return bool Whether the given text node's value must be extracted
192
     */
193 72
    protected function shouldExtractTextNodeValue($textNode)
194
    {
195 72
        $parentTagName = $textNode->parentNode->localName;
196 72
        return ($parentTagName === self::XML_NODE_SI || $parentTagName === self::XML_NODE_R);
197
    }
198
199
    /**
200
     * If the text node has the attribute 'xml:space="preserve"', then preserve whitespace.
201
     *
202
     * @param \DOMElement $textNode The text node element (<t>) whose whitespace may be preserved
203
     * @return bool Whether whitespace should be preserved
204
     */
205 72
    protected function shouldPreserveWhitespace($textNode)
206
    {
207 72
        $spaceValue = $textNode->getAttribute(self::XML_ATTRIBUTE_XML_SPACE);
208 72
        return ($spaceValue === self::XML_ATTRIBUTE_VALUE_PRESERVE);
209
    }
210
211
    /**
212
     * Returns the shared string at the given index, using the previously chosen caching strategy.
213
     *
214
     * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
215
     * @return string The shared string at the given index
216
     * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index
217
     */
218 72
    public function getStringAtIndex($sharedStringIndex)
219
    {
220 72
        return $this->cachingStrategy->getStringAtIndex($sharedStringIndex);
221
    }
222
223
    /**
224
     * Destroys the cache, freeing memory and removing any created artifacts
225
     *
226
     * @return void
227
     */
228 114
    public function cleanup()
229
    {
230 114
        if ($this->cachingStrategy) {
231 96
            $this->cachingStrategy->clearCache();
232 96
        }
233 114
    }
234
}
235