Relationships::__construct()   A
last analyzed

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 const WORKSHEET = 'worksheet';
15
    private const STYLES = 'styles';
16
    private const SHARED_STRINGS = 'sharedStrings';
17
    private const RELATIONSHIP = 'Relationship';
18
    private const TARGET = 'Target';
19
    private const TYPE = 'Type';
20
    private const ID = 'Id';
21
22
    private array $workSheetPaths = [];
23
    private string $sharedStringPath = '';
24
    private string $stylePath = '';
25
26
    public function __construct(string $path)
27
    {
28
        parent::__construct(path: $path);
29
        $xml = $this->getXMLReader();
30
31
        while ($xml->read()) {
32
            if (XMLReader::ELEMENT === $xml->nodeType && self::RELATIONSHIP === $xml->name) {
33
                $target = 'xl/' . $xml->getAttribute(name: self::TARGET);
34
35
                match (basename(path: (string) $xml->getAttribute(name: self::TYPE))) {
36
                    self::WORKSHEET => $this->workSheetPaths[$xml->getAttribute(name: self::ID)] = $target,
37
                    self::STYLES => $this->stylePath = $target,
38
                    self::SHARED_STRINGS => $this->sharedStringPath = $target,
39
                    default => null,
40
                };
41
            }
42
        }
43
44
        $this->closeXMLReader();
45
    }
46
47
    public function getWorksheetPath(string $rId): string
48
    {
49
        return $this->workSheetPaths[$rId];
50
    }
51
52
    public function getSharedStringsPath(): string
53
    {
54
        return $this->sharedStringPath;
55
    }
56
57
    public function getStylesPath(): string
58
    {
59
        return $this->stylePath;
60
    }
61
}
62