Passed
Pull Request — master (#3860)
by Adrien
11:44
created

Properties::nullOrSimple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 564
        // okay to omit namespace because everything will be processed by xpath
25
        $obj = simplexml_load_string(
26
            $this->securityScanner->scan($propertyData),
27 564
            'SimpleXMLElement',
28
            Settings::getLibXmlLoaderOptions()
29
        );
30 564
31 564
        return $obj === false ? null : $obj;
32 564
    }
33 564
34 564
    public function readCoreProperties(string $propertyData): void
35
    {
36 564
        $xmlCore = $this->extractPropertyData($propertyData);
37
38
        if (is_object($xmlCore)) {
39 563
            $xmlCore->registerXPathNamespace('dc', Namespaces::DC_ELEMENTS);
40
            $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
            $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 563
        }
53 563
    }
54 563
55 563
    public function readExtendedProperties(string $propertyData): void
56 563
    {
57
        $xmlCore = $this->extractPropertyData($propertyData);
58
59
        if (is_object($xmlCore)) {
60 562
            if (isset($xmlCore->Company)) {
61
                $this->docProps->setCompany((string) $xmlCore->Company);
62 562
            }
63
            if (isset($xmlCore->Manager)) {
64 562
                $this->docProps->setManager((string) $xmlCore->Manager);
65 562
            }
66 508
            if (isset($xmlCore->HyperlinkBase)) {
67
                $this->docProps->setHyperlinkBase((string) $xmlCore->HyperlinkBase);
68 562
            }
69 246
        }
70
    }
71 562
72 204
    public function readCustomProperties(string $propertyData): void
73
    {
74
        $xmlCore = $this->extractPropertyData($propertyData);
75
76
        if (is_object($xmlCore)) {
77 50
            foreach ($xmlCore as $xmlProperty) {
78
                /** @var SimpleXMLElement $xmlProperty */
79 50
                $cellDataOfficeAttributes = $xmlProperty->attributes();
80
                if (isset($cellDataOfficeAttributes['name'])) {
81 50
                    $propertyName = (string) $cellDataOfficeAttributes['name'];
82 50
                    $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
                    $this->docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
89 48
                }
90 48
            }
91 48
        }
92 48
    }
93 48
94
    private function getArrayItem(null|array|false $array): string
95
    {
96
        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