Passed
Pull Request — master (#3860)
by Adrien
19:40 queued 09:20
created

Properties::extractPropertyData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Document\Properties as DocumentProperties;
6
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
7
use PhpOffice\PhpSpreadsheet\Settings;
8
use SimpleXMLElement;
9
10
class Properties
11
{
12
    private XmlScanner $securityScanner;
13
14
    private DocumentProperties $docProps;
15
16 573
    public function __construct(XmlScanner $securityScanner, DocumentProperties $docProps)
17
    {
18 573
        $this->securityScanner = $securityScanner;
19 573
        $this->docProps = $docProps;
20
    }
21
22 564
    private function extractPropertyData(string $propertyData): ?SimpleXMLElement
23
    {
24
        // okay to omit namespace because everything will be processed by xpath
25 564
        $obj = simplexml_load_string(
26 564
            $this->securityScanner->scan($propertyData),
27 564
            'SimpleXMLElement',
28 564
            Settings::getLibXmlLoaderOptions()
29 564
        );
30
31 564
        return $obj === false ? null : $obj;
32
    }
33
34 563
    public function readCoreProperties(string $propertyData): void
35
    {
36 563
        $xmlCore = $this->extractPropertyData($propertyData);
37
38 563
        if (is_object($xmlCore)) {
39 563
            $xmlCore->registerXPathNamespace('dc', Namespaces::DC_ELEMENTS);
40 563
            $xmlCore->registerXPathNamespace('dcterms', Namespaces::DC_TERMS);
41 563
            $xmlCore->registerXPathNamespace('cp', Namespaces::CORE_PROPERTIES2);
42
43 563
            $this->docProps->setCreator($this->getArrayItem($xmlCore->xpath('dc:creator')));
44 563
            $this->docProps->setLastModifiedBy($this->getArrayItem($xmlCore->xpath('cp:lastModifiedBy')));
45 563
            $this->docProps->setCreated($this->getArrayItem($xmlCore->xpath('dcterms:created'))); //! respect xsi:type
46 563
            $this->docProps->setModified($this->getArrayItem($xmlCore->xpath('dcterms:modified'))); //! respect xsi:type
47 563
            $this->docProps->setTitle($this->getArrayItem($xmlCore->xpath('dc:title')));
48 563
            $this->docProps->setDescription($this->getArrayItem($xmlCore->xpath('dc:description')));
49 563
            $this->docProps->setSubject($this->getArrayItem($xmlCore->xpath('dc:subject')));
50 563
            $this->docProps->setKeywords($this->getArrayItem($xmlCore->xpath('cp:keywords')));
51 563
            $this->docProps->setCategory($this->getArrayItem($xmlCore->xpath('cp:category')));
52
        }
53
    }
54
55 562
    public function readExtendedProperties(string $propertyData): void
56
    {
57 562
        $xmlCore = $this->extractPropertyData($propertyData);
58
59 562
        if (is_object($xmlCore)) {
60 562
            if (isset($xmlCore->Company)) {
61 508
                $this->docProps->setCompany((string) $xmlCore->Company);
62
            }
63 562
            if (isset($xmlCore->Manager)) {
64 246
                $this->docProps->setManager((string) $xmlCore->Manager);
65
            }
66 562
            if (isset($xmlCore->HyperlinkBase)) {
67 204
                $this->docProps->setHyperlinkBase((string) $xmlCore->HyperlinkBase);
68
            }
69
        }
70
    }
71
72 50
    public function readCustomProperties(string $propertyData): void
73
    {
74 50
        $xmlCore = $this->extractPropertyData($propertyData);
75
76 50
        if (is_object($xmlCore)) {
77 50
            foreach ($xmlCore as $xmlProperty) {
78
                /** @var SimpleXMLElement $xmlProperty */
79 48
                $cellDataOfficeAttributes = $xmlProperty->attributes();
80 48
                if (isset($cellDataOfficeAttributes['name'])) {
81 48
                    $propertyName = (string) $cellDataOfficeAttributes['name'];
82 48
                    $cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
83
84 48
                    $attributeType = $cellDataOfficeChildren->getName();
85 48
                    $attributeValue = (string) $cellDataOfficeChildren->{$attributeType};
86 48
                    $attributeValue = DocumentProperties::convertProperty($attributeValue, $attributeType);
87 48
                    $attributeType = DocumentProperties::convertPropertyType($attributeType);
88 48
                    $this->docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
89
                }
90
            }
91
        }
92
    }
93
94 563
    private function getArrayItem(null|array|false $array): string
95
    {
96 563
        return is_array($array) ? (string) ($array[0] ?? '') : '';
1 ignored issue
show
introduced by
The condition is_array($array) is always true.
Loading history...
97
    }
98
}
99