Passed
Pull Request — master (#4240)
by Owen
13:49
created

DataValidations::load()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 33
ccs 21
cts 21
cp 1
rs 9.2408
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 5
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
7
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9
use SimpleXMLElement;
10
11
class DataValidations
12
{
13
    private Worksheet $worksheet;
14
15
    private SimpleXMLElement $worksheetXml;
16 17
17
    public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml)
18 17
    {
19 17
        $this->worksheet = $workSheet;
20
        $this->worksheetXml = $worksheetXml;
21
    }
22 17
23
    public function load(): void
24 17
    {
25
        foreach ($this->worksheetXml->dataValidations->dataValidation as $dataValidation) {
26 17
            // Uppercase coordinate
27 17
            $range = strtoupper((string) $dataValidation['sqref']);
28 17
            $rangeSet = explode(' ', $range);
29 17
            foreach ($rangeSet as $range) {
30
                if (preg_match('/^[A-Z]{1,3}\\d{1,7}/', $range, $matches) === 1) {
31
                    // Ensure left/top row of range exists, thereby
32 17
                    // adjusting high row/column.
33
                    $this->worksheet->getCell($matches[0]);
34
                }
35
            }
36 17
        }
37
        foreach ($this->worksheetXml->dataValidations->dataValidation as $dataValidation) {
38 17
            // Uppercase coordinate
39 17
            $range = strtoupper((string) $dataValidation['sqref']);
40 17
            $docValidation = new DataValidation();
41 17
            $docValidation->setType((string) $dataValidation['type']);
42
            $docValidation->setErrorStyle((string) $dataValidation['errorStyle']);
43
            $docValidation->setOperator((string) $dataValidation['operator']);
44 17
            $docValidation->setAllowBlank(filter_var($dataValidation['allowBlank'], FILTER_VALIDATE_BOOLEAN));
45
            // showDropDown is inverted (works as hideDropDown if true)
46 17
            $docValidation->setShowDropDown(!filter_var($dataValidation['showDropDown'], FILTER_VALIDATE_BOOLEAN));
47 17
            $docValidation->setShowInputMessage(filter_var($dataValidation['showInputMessage'], FILTER_VALIDATE_BOOLEAN));
48 17
            $docValidation->setShowErrorMessage(filter_var($dataValidation['showErrorMessage'], FILTER_VALIDATE_BOOLEAN));
49 17
            $docValidation->setErrorTitle((string) $dataValidation['errorTitle']);
50 17
            $docValidation->setError((string) $dataValidation['error']);
51
            $docValidation->setPromptTitle((string) $dataValidation['promptTitle']);
52 17
            $docValidation->setPrompt((string) $dataValidation['prompt']);
53 17
            $docValidation->setFormula1(Xlsx::replacePrefixes((string) $dataValidation->formula1));
54 17
            $docValidation->setFormula2(Xlsx::replacePrefixes((string) $dataValidation->formula2));
55 17
            $this->worksheet->setDataValidation($range, $docValidation);
56 17
        }
57 17
    }
58
}
59