1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Reader\XLSX;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Common\Exception\IOException;
|
|
|
8 |
use OpenSpout\Reader\Common\XMLProcessor;
|
|
|
9 |
use OpenSpout\Reader\Wrapper\XMLReader;
|
|
|
10 |
|
|
|
11 |
use function ltrim;
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* @internal
|
|
|
15 |
*/
|
|
|
16 |
final class SheetMergeCellsReader
|
|
|
17 |
{
|
|
|
18 |
public const XML_NODE_MERGE_CELL = 'mergeCell';
|
|
|
19 |
public const XML_ATTRIBUTE_REF = 'ref';
|
|
|
20 |
|
|
|
21 |
/** @var list<string> Merged cells list */
|
|
|
22 |
private array $mergeCells = [];
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* @param string $filePath Path of the XLSX file being read
|
|
|
26 |
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
|
|
|
27 |
* @param XMLProcessor $xmlProcessor Helper to process XML files
|
|
|
28 |
*/
|
|
|
29 |
public function __construct(
|
|
|
30 |
string $filePath,
|
|
|
31 |
string $sheetDataXMLFilePath,
|
|
|
32 |
XMLReader $xmlReader,
|
|
|
33 |
XMLProcessor $xmlProcessor
|
|
|
34 |
) {
|
|
|
35 |
$sheetDataXMLFilePath = ltrim($sheetDataXMLFilePath, '/');
|
|
|
36 |
|
|
|
37 |
// Register all callbacks to process different nodes when reading the XML file
|
|
|
38 |
$xmlProcessor->registerCallback(self::XML_NODE_MERGE_CELL, XMLProcessor::NODE_TYPE_START, [$this, 'processMergeCellsStartingNode']);
|
|
|
39 |
$xmlReader->close();
|
|
|
40 |
|
|
|
41 |
if (false === $xmlReader->openFileInZip($filePath, $sheetDataXMLFilePath)) {
|
|
|
42 |
throw new IOException("Could not open \"{$sheetDataXMLFilePath}\".");
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// Now read the entire header of the sheet, until we reach the <sheetData> element
|
|
|
46 |
$xmlProcessor->readUntilStopped();
|
|
|
47 |
$xmlReader->close();
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @return list<string>
|
|
|
52 |
*/
|
|
|
53 |
public function getMergeCells(): array
|
|
|
54 |
{
|
|
|
55 |
return $this->mergeCells;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @param XMLReader $xmlReader XMLReader object, positioned on a "<mergeCells>" starting node
|
|
|
60 |
*
|
|
|
61 |
* @return int A return code that indicates what action should the processor take next
|
|
|
62 |
*/
|
|
|
63 |
private function processMergeCellsStartingNode(XMLReader $xmlReader): int
|
|
|
64 |
{
|
|
|
65 |
$this->mergeCells[] = $xmlReader->getAttribute(self::XML_ATTRIBUTE_REF);
|
|
|
66 |
|
|
|
67 |
return XMLProcessor::PROCESSING_CONTINUE;
|
|
|
68 |
}
|
|
|
69 |
}
|