Completed
Push — master ( 1eb01a...048105 )
by Adrien
02:16
created

SharedStringsHelper::shouldExtractTextNodeValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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
     * Please note that SimpleXML does not provide such a functionality but since it is faster
84
     * and more handy to parse few XML nodes, it is used in combination with XMLReader for that purpose.
85
     *
86
     * @return void
87
     * @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml can't be read
88
     */
89 105
    public function extractSharedStrings()
90
    {
91 105
        $xmlReader = new XMLReader();
92 105
        $sharedStringIndex = 0;
93
94 105
        $sharedStringsFilePath = $this->getSharedStringsFilePath();
95 105
        if ($xmlReader->open($sharedStringsFilePath) === false) {
96
            throw new IOException('Could not open "' . self::SHARED_STRINGS_XML_FILE_PATH . '".');
97
        }
98
99
        try {
100 105
            $sharedStringsUniqueCount = $this->getSharedStringsUniqueCount($xmlReader);
101 102
            $this->cachingStrategy = $this->getBestSharedStringsCachingStrategy($sharedStringsUniqueCount);
102
103 102
            $xmlReader->readUntilNodeFound(self::XML_NODE_SI);
104
105 102
            while ($xmlReader->name === self::XML_NODE_SI) {
106 72
                $this->processSharedStringsItem($xmlReader, $sharedStringIndex);
107 72
                $sharedStringIndex++;
108
109
                // jump to the next '<si>' tag
110 72
                $xmlReader->next(self::XML_NODE_SI);
111 72
            }
112
113 102
            $this->cachingStrategy->closeCache();
114
115 105
        } catch (XMLProcessingException $exception) {
116 3
            throw new IOException("The sharedStrings.xml file is invalid and cannot be read. [{$exception->getMessage()}]");
117
        }
118
119 102
        $xmlReader->close();
120 102
    }
121
122
    /**
123
     * @return string The path to the shared strings XML file
124
     */
125 105
    protected function getSharedStringsFilePath()
126
    {
127 105
        return 'zip://' . $this->filePath . '#' . self::SHARED_STRINGS_XML_FILE_PATH;
128
    }
129
130
    /**
131
     * Returns the shared strings unique count, as specified in <sst> tag.
132
     *
133
     * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XMLReader instance
134
     * @return int|null Number of unique shared strings in the sharedStrings.xml file
135
     * @throws \Box\Spout\Common\Exception\IOException If sharedStrings.xml is invalid and can't be read
136
     */
137 105
    protected function getSharedStringsUniqueCount($xmlReader)
138
    {
139 105
        $xmlReader->next(self::XML_NODE_SST);
140
141
        // Iterate over the "sst" elements to get the actual "sst ELEMENT" (skips any DOCTYPE)
142 102
        while ($xmlReader->name === self::XML_NODE_SST && $xmlReader->nodeType !== XMLReader::ELEMENT) {
143 3
            $xmlReader->read();
144 3
        }
145
146 102
        $uniqueCount = $xmlReader->getAttribute(self::XML_ATTRIBUTE_UNIQUE_COUNT);
147
148
        // some software do not add the "uniqueCount" attribute but only use the "count" one
149
        // @see https://github.com/box/spout/issues/254
150 102
        if ($uniqueCount === null) {
151 9
            $uniqueCount = $xmlReader->getAttribute(self::XML_ATTRIBUTE_COUNT);
152 9
        }
153
154 102
        return ($uniqueCount !== null) ? intval($uniqueCount) : null;
155
    }
156
157
    /**
158
     * Returns the best shared strings caching strategy.
159
     *
160
     * @param int|null $sharedStringsUniqueCount Number of unique shared strings (NULL if unknown)
161
     * @return CachingStrategyInterface
162
     */
163 102
    protected function getBestSharedStringsCachingStrategy($sharedStringsUniqueCount)
164
    {
165 102
        return CachingStrategyFactory::getInstance()
166 102
                ->getBestCachingStrategy($sharedStringsUniqueCount, $this->tempFolder);
167
    }
168
169
    /**
170
     * Processes the shared strings item XML node which the given XML reader is positioned on.
171
     *
172
     * @param \Box\Spout\Reader\Wrapper\XMLReader $xmlReader XML Reader positioned on a "<si>" node
173
     * @param int $sharedStringIndex Index of the processed shared strings item
174
     * @return void
175
     */
176 72
    protected function processSharedStringsItem($xmlReader, $sharedStringIndex)
177
    {
178 72
        $sharedStringValue = '';
179
180
        // NOTE: expand() will automatically decode all XML entities of the child nodes
181 72
        $siNode = $xmlReader->expand();
182 72
        $textNodes = $siNode->getElementsByTagName(self::XML_NODE_T);
183
184 72
        foreach ($textNodes as $textNode) {
185 72
            if ($this->shouldExtractTextNodeValue($textNode)) {
186 72
                $textNodeValue = $textNode->nodeValue;
187 72
                $shouldPreserveWhitespace = $this->shouldPreserveWhitespace($textNode);
188
189 72
                $sharedStringValue .= ($shouldPreserveWhitespace) ? $textNodeValue : trim($textNodeValue);
190 72
            }
191 72
        }
192
193 72
        $this->cachingStrategy->addStringForIndex($sharedStringValue, $sharedStringIndex);
194 72
    }
195
196
    /**
197
     * Not all text nodes' values must be extracted.
198
     * Some text nodes are part of a node describing the pronunciation for instance.
199
     * We'll only consider the nodes whose parents are "<si>" or "<r>".
200
     *
201
     * @param \DOMElement $textNode Text node to check
202
     * @return bool Whether the given text node's value must be extracted
203
     */
204 72
    protected function shouldExtractTextNodeValue($textNode)
205
    {
206 72
        $parentTagName = $textNode->parentNode->localName;
207 72
        return ($parentTagName === self::XML_NODE_SI || $parentTagName === self::XML_NODE_R);
208
    }
209
210
    /**
211
     * If the text node has the attribute 'xml:space="preserve"', then preserve whitespace.
212
     *
213
     * @param \DOMElement $textNode The text node element (<t>) whose whitespace may be preserved
214
     * @return bool Whether whitespace should be preserved
215
     */
216 72
    protected function shouldPreserveWhitespace($textNode)
217
    {
218 72
        $spaceValue = $textNode->getAttribute(self::XML_ATTRIBUTE_XML_SPACE);
219 72
        return ($spaceValue === self::XML_ATTRIBUTE_VALUE_PRESERVE);
220
    }
221
222
    /**
223
     * Returns the shared string at the given index, using the previously chosen caching strategy.
224
     *
225
     * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
226
     * @return string The shared string at the given index
227
     * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index
228
     */
229 72
    public function getStringAtIndex($sharedStringIndex)
230
    {
231 72
        return $this->cachingStrategy->getStringAtIndex($sharedStringIndex);
232
    }
233
234
    /**
235
     * Destroys the cache, freeing memory and removing any created artifacts
236
     *
237
     * @return void
238
     */
239 114
    public function cleanup()
240
    {
241 114
        if ($this->cachingStrategy) {
242 96
            $this->cachingStrategy->clearCache();
243 96
        }
244 114
    }
245
}
246