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

Relationships   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 40
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSharedStringsPath() 0 3 1
A getStylesPath() 0 3 1
A __construct() 0 19 4
A getWorksheetPath() 0 3 1
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