Passed
Push — spaghetti ( fcb196...2528e5 )
by simpletoimplement
02:19
created

Relationships::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.9
1
<?php declare(strict_types = 1);
2
3
namespace Spaghetti\XLSXParser;
4
5
use XMLReader;
6
7
use function basename;
8
9
/**
10
 * @internal
11
 */
12
final class Relationships extends AbstractXMLResource
13
{
14
    private array $workSheetPaths = [];
15
    private string $sharedStringPath = '';
16
    private string $stylePath = '';
17
18
    public function __construct(string $path)
19
    {
20
        parent::__construct(path: $path);
21
        $xml = $this->getXMLReader();
22
23
        while ($xml->read()) {
24
            if (XMLReader::ELEMENT === $xml->nodeType && 'Relationship' === $xml->name) {
25
                $target = 'xl/' . $xml->getAttribute(name: 'Target');
26
27
                match (basename(path: (string) $xml->getAttribute(name: 'Type'))) {
28
                    'worksheet' => $this->workSheetPaths[$xml->getAttribute(name: 'Id')] = $target,
29
                    'styles' => $this->stylePath = $target,
30
                    'sharedStrings' => $this->sharedStringPath = $target,
31
                    default => null,
32
                };
33
            }
34
        }
35
36
        $this->closeXMLReader();
37
    }
38
39
    public function getWorksheetPath(string $rId): string
40
    {
41
        return $this->workSheetPaths[$rId];
42
    }
43
44
    public function getSharedStringsPath(): string
45
    {
46
        return $this->sharedStringPath;
47
    }
48
49
    public function getStylesPath(): string
50
    {
51
        return $this->stylePath;
52
    }
53
}
54