Skip to content

Commit 7e9edf5

Browse files
authored
Merge pull request #2698 from PHPOffice/Xls-Reader-DataValidations-Refactoring
Move DataValidation switch statements into a dedicated helper class
2 parents 0e7ee37 + c73bb61 commit 7e9edf5

File tree

5 files changed

+138
-95
lines changed

5 files changed

+138
-95
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,16 +2300,6 @@ parameters:
23002300
count: 2
23012301
path: src/PhpSpreadsheet/Reader/Xls.php
23022302

2303-
-
2304-
message: "#^Parameter \\#1 \\$errorStyle of method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataValidation\\:\\:setErrorStyle\\(\\) expects string, int\\|string given\\.$#"
2305-
count: 1
2306-
path: src/PhpSpreadsheet/Reader/Xls.php
2307-
2308-
-
2309-
message: "#^Parameter \\#1 \\$operator of method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataValidation\\:\\:setOperator\\(\\) expects string, int\\|string given\\.$#"
2310-
count: 1
2311-
path: src/PhpSpreadsheet/Reader/Xls.php
2312-
23132303
-
23142304
message: "#^Parameter \\#1 \\$showSummaryBelow of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\Worksheet\\:\\:setShowSummaryBelow\\(\\) expects bool, int given\\.$#"
23152305
count: 1
@@ -2320,11 +2310,6 @@ parameters:
23202310
count: 1
23212311
path: src/PhpSpreadsheet/Reader/Xls.php
23222312

2323-
-
2324-
message: "#^Parameter \\#1 \\$type of method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataValidation\\:\\:setType\\(\\) expects string, int\\|string given\\.$#"
2325-
count: 1
2326-
path: src/PhpSpreadsheet/Reader/Xls.php
2327-
23282313
-
23292314
message: "#^Parameter \\#2 \\$row of method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\IReadFilter\\:\\:readCell\\(\\) expects int, string given\\.$#"
23302315
count: 1
@@ -5484,3 +5469,4 @@ parameters:
54845469
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\Xlsx\\\\Xlfn\\:\\:addXlfn\\(\\) should return string but returns string\\|null\\.$#"
54855470
count: 1
54865471
path: src/PhpSpreadsheet/Writer/Xlsx/Xlfn.php
5472+

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4776,57 +4776,11 @@ private function readDataValidation(): void
47764776

47774777
// bit: 0-3; mask: 0x0000000F; type
47784778
$type = (0x0000000F & $options) >> 0;
4779-
switch ($type) {
4780-
case 0x00:
4781-
$type = DataValidation::TYPE_NONE;
4782-
4783-
break;
4784-
case 0x01:
4785-
$type = DataValidation::TYPE_WHOLE;
4786-
4787-
break;
4788-
case 0x02:
4789-
$type = DataValidation::TYPE_DECIMAL;
4790-
4791-
break;
4792-
case 0x03:
4793-
$type = DataValidation::TYPE_LIST;
4794-
4795-
break;
4796-
case 0x04:
4797-
$type = DataValidation::TYPE_DATE;
4798-
4799-
break;
4800-
case 0x05:
4801-
$type = DataValidation::TYPE_TIME;
4802-
4803-
break;
4804-
case 0x06:
4805-
$type = DataValidation::TYPE_TEXTLENGTH;
4806-
4807-
break;
4808-
case 0x07:
4809-
$type = DataValidation::TYPE_CUSTOM;
4810-
4811-
break;
4812-
}
4779+
$type = Xls\DataValidationHelper::type($type);
48134780

48144781
// bit: 4-6; mask: 0x00000070; error type
48154782
$errorStyle = (0x00000070 & $options) >> 4;
4816-
switch ($errorStyle) {
4817-
case 0x00:
4818-
$errorStyle = DataValidation::STYLE_STOP;
4819-
4820-
break;
4821-
case 0x01:
4822-
$errorStyle = DataValidation::STYLE_WARNING;
4823-
4824-
break;
4825-
case 0x02:
4826-
$errorStyle = DataValidation::STYLE_INFORMATION;
4827-
4828-
break;
4829-
}
4783+
$errorStyle = Xls\DataValidationHelper::errorStyle($errorStyle);
48304784

48314785
// bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list)
48324786
// I have only seen cases where this is 1
@@ -4846,39 +4800,10 @@ private function readDataValidation(): void
48464800

48474801
// bit: 20-23; mask: 0x00F00000; condition operator
48484802
$operator = (0x00F00000 & $options) >> 20;
4849-
switch ($operator) {
4850-
case 0x00:
4851-
$operator = DataValidation::OPERATOR_BETWEEN;
4852-
4853-
break;
4854-
case 0x01:
4855-
$operator = DataValidation::OPERATOR_NOTBETWEEN;
4856-
4857-
break;
4858-
case 0x02:
4859-
$operator = DataValidation::OPERATOR_EQUAL;
4860-
4861-
break;
4862-
case 0x03:
4863-
$operator = DataValidation::OPERATOR_NOTEQUAL;
4803+
$operator = Xls\DataValidationHelper::operator($operator);
48644804

4865-
break;
4866-
case 0x04:
4867-
$operator = DataValidation::OPERATOR_GREATERTHAN;
4868-
4869-
break;
4870-
case 0x05:
4871-
$operator = DataValidation::OPERATOR_LESSTHAN;
4872-
4873-
break;
4874-
case 0x06:
4875-
$operator = DataValidation::OPERATOR_GREATERTHANOREQUAL;
4876-
4877-
break;
4878-
case 0x07:
4879-
$operator = DataValidation::OPERATOR_LESSTHANOREQUAL;
4880-
4881-
break;
4805+
if ($type === null || $errorStyle === null || $operator === null) {
4806+
return;
48824807
}
48834808

48844809
// offset: 4; size: var; title of the prompt box
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Reader\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
6+
7+
class DataValidationHelper
8+
{
9+
/**
10+
* @var array<int, string>
11+
*/
12+
private static $types = [
13+
0x00 => DataValidation::TYPE_NONE,
14+
0x01 => DataValidation::TYPE_WHOLE,
15+
0x02 => DataValidation::TYPE_DECIMAL,
16+
0x03 => DataValidation::TYPE_LIST,
17+
0x04 => DataValidation::TYPE_DATE,
18+
0x05 => DataValidation::TYPE_TIME,
19+
0x06 => DataValidation::TYPE_TEXTLENGTH,
20+
0x07 => DataValidation::TYPE_CUSTOM,
21+
];
22+
23+
/**
24+
* @var array<int, string>
25+
*/
26+
private static $errorStyles = [
27+
0x00 => DataValidation::STYLE_STOP,
28+
0x01 => DataValidation::STYLE_WARNING,
29+
0x02 => DataValidation::STYLE_INFORMATION,
30+
];
31+
32+
/**
33+
* @var array<int, string>
34+
*/
35+
private static $operators = [
36+
0x00 => DataValidation::OPERATOR_BETWEEN,
37+
0x01 => DataValidation::OPERATOR_NOTBETWEEN,
38+
0x02 => DataValidation::OPERATOR_EQUAL,
39+
0x03 => DataValidation::OPERATOR_NOTEQUAL,
40+
0x04 => DataValidation::OPERATOR_GREATERTHAN,
41+
0x05 => DataValidation::OPERATOR_LESSTHAN,
42+
0x06 => DataValidation::OPERATOR_GREATERTHANOREQUAL,
43+
0x07 => DataValidation::OPERATOR_LESSTHANOREQUAL,
44+
];
45+
46+
public static function type(int $type): ?string
47+
{
48+
if (isset(self::$types[$type])) {
49+
return self::$types[$type];
50+
}
51+
52+
return null;
53+
}
54+
55+
public static function errorStyle(int $errorStyle): ?string
56+
{
57+
if (isset(self::$errorStyles[$errorStyle])) {
58+
return self::$errorStyles[$errorStyle];
59+
}
60+
61+
return null;
62+
}
63+
64+
public static function operator(int $operator): ?string
65+
{
66+
if (isset(self::$operators[$operator])) {
67+
return self::$operators[$operator];
68+
}
69+
70+
return null;
71+
}
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
6+
use PhpOffice\PhpSpreadsheet\Reader\Xls;
7+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class DataValidationTest extends TestCase
11+
{
12+
/**
13+
* @var Worksheet
14+
*/
15+
protected $sheet;
16+
17+
protected function setUp(): void
18+
{
19+
$filename = 'tests/data/Reader/XLS/DataValidation.xls';
20+
$reader = new Xls();
21+
$spreadsheet = $reader->load($filename);
22+
$this->sheet = $spreadsheet->getActiveSheet();
23+
}
24+
25+
/**
26+
* @dataProvider dataValidationProvider
27+
*/
28+
public function testDataValidation(string $expectedRange, array $expectedRule): void
29+
{
30+
$hasDataValidation = $this->sheet->dataValidationExists($expectedRange);
31+
self::assertTrue($hasDataValidation);
32+
33+
$dataValidation = $this->sheet->getDataValidation($expectedRange);
34+
self::assertSame($expectedRule['type'], $dataValidation->getType());
35+
self::assertSame($expectedRule['operator'], $dataValidation->getOperator());
36+
self::assertSame($expectedRule['formula'], $dataValidation->getFormula1());
37+
}
38+
39+
public function dataValidationProvider(): array
40+
{
41+
return [
42+
[
43+
'B2',
44+
[
45+
'type' => DataValidation::TYPE_WHOLE,
46+
'operator' => DataValidation::OPERATOR_GREATERTHANOREQUAL,
47+
'formula' => '18',
48+
],
49+
],
50+
[
51+
'B3',
52+
[
53+
'type' => DataValidation::TYPE_LIST,
54+
'operator' => DataValidation::OPERATOR_BETWEEN,
55+
'formula' => '"Blocked,Pending,Approved"',
56+
],
57+
],
58+
];
59+
}
60+
}
25.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)