Passed
Push — master ( fb74dc...f37b11 )
by
unknown
18:17 queued 06:47
created

Properties::getArrayItem()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
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 SimpleXMLElement;
8
9
class Properties
10
{
11
    private XmlScanner $securityScanner;
12
13
    private DocumentProperties $docProps;
14
15 640
    public function __construct(XmlScanner $securityScanner, DocumentProperties $docProps)
16
    {
17 640
        $this->securityScanner = $securityScanner;
18 640
        $this->docProps = $docProps;
19
    }
20
21 625
    private function extractPropertyData(string $propertyData): ?SimpleXMLElement
22
    {
23
        // okay to omit namespace because everything will be processed by xpath
24 625
        $obj = simplexml_load_string(
25 625
            $this->securityScanner->scan($propertyData)
26 625
        );
27
28 625
        return $obj === false ? null : $obj;
29
    }
30
31 624
    public function readCoreProperties(string $propertyData): void
32
    {
33 624
        $xmlCore = $this->extractPropertyData($propertyData);
34
35 624
        if (is_object($xmlCore)) {
36 624
            $xmlCore->registerXPathNamespace('dc', Namespaces::DC_ELEMENTS);
37 624
            $xmlCore->registerXPathNamespace('dcterms', Namespaces::DC_TERMS);
38 624
            $xmlCore->registerXPathNamespace('cp', Namespaces::CORE_PROPERTIES2);
39
40 624
            $this->docProps->setCreator($this->getArrayItem($xmlCore->xpath('dc:creator')));
41 624
            $this->docProps->setLastModifiedBy($this->getArrayItem($xmlCore->xpath('cp:lastModifiedBy')));
42 624
            $this->docProps->setCreated($this->getArrayItem($xmlCore->xpath('dcterms:created'))); //! respect xsi:type
43 624
            $this->docProps->setModified($this->getArrayItem($xmlCore->xpath('dcterms:modified'))); //! respect xsi:type
44 624
            $this->docProps->setTitle($this->getArrayItem($xmlCore->xpath('dc:title')));
45 624
            $this->docProps->setDescription($this->getArrayItem($xmlCore->xpath('dc:description')));
46 624
            $this->docProps->setSubject($this->getArrayItem($xmlCore->xpath('dc:subject')));
47 624
            $this->docProps->setKeywords($this->getArrayItem($xmlCore->xpath('cp:keywords')));
48 624
            $this->docProps->setCategory($this->getArrayItem($xmlCore->xpath('cp:category')));
49
        }
50
    }
51
52 623
    public function readExtendedProperties(string $propertyData): void
53
    {
54 623
        $xmlCore = $this->extractPropertyData($propertyData);
55
56 623
        if (is_object($xmlCore)) {
57 623
            if (isset($xmlCore->Company)) {
58 568
                $this->docProps->setCompany((string) $xmlCore->Company);
59
            }
60 623
            if (isset($xmlCore->Manager)) {
61 280
                $this->docProps->setManager((string) $xmlCore->Manager);
62
            }
63 623
            if (isset($xmlCore->HyperlinkBase)) {
64 238
                $this->docProps->setHyperlinkBase((string) $xmlCore->HyperlinkBase);
65
            }
66
        }
67
    }
68
69 52
    public function readCustomProperties(string $propertyData): void
70
    {
71 52
        $xmlCore = $this->extractPropertyData($propertyData);
72
73 52
        if (is_object($xmlCore)) {
74 52
            foreach ($xmlCore as $xmlProperty) {
75
                /** @var SimpleXMLElement $xmlProperty */
76 49
                $cellDataOfficeAttributes = $xmlProperty->attributes();
77 49
                if (isset($cellDataOfficeAttributes['name'])) {
78 49
                    $propertyName = (string) $cellDataOfficeAttributes['name'];
79 49
                    $cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
80
81 49
                    $attributeType = $cellDataOfficeChildren->getName();
82 49
                    $attributeValue = (string) $cellDataOfficeChildren->{$attributeType};
83 49
                    $attributeValue = DocumentProperties::convertProperty($attributeValue, $attributeType);
84 49
                    $attributeType = DocumentProperties::convertPropertyType($attributeType);
85 49
                    $this->docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
86
                }
87
            }
88
        }
89
    }
90
91 624
    private function getArrayItem(null|array|false $array): string
92
    {
93 624
        return is_array($array) ? (string) ($array[0] ?? '') : '';
94
    }
95
}
96