Skip to content

Commit dc255fb

Browse files
author
MarkBaker
committed
Merge branch 'master' into CalculationEngine-Array-Formulae-Initial-Work
# Conflicts: # phpstan-baseline.neon
2 parents 2acb3fe + 1bd5369 commit dc255fb

File tree

12 files changed

+162
-103
lines changed

12 files changed

+162
-103
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2929
This is determined by the Calculation Engine locale setting.
3030

3131
(i.e. `"Vrai"` wil be converted to a boolean `true` if the Locale is set to `fr`.)
32+
- Allow `psr/simple-cache` 2.x
3233

3334
### Deprecated
3435

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"markbaker/matrix": "^3.0",
7676
"psr/http-client": "^1.0",
7777
"psr/http-factory": "^1.0",
78-
"psr/simple-cache": "^1.0"
78+
"psr/simple-cache": "^1.0 || ^2.0"
7979
},
8080
"require-dev": {
8181
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,16 +2290,6 @@ parameters:
22902290
count: 2
22912291
path: src/PhpSpreadsheet/Reader/Xls.php
22922292

2293-
-
2294-
message: "#^Parameter \\#1 \\$errorStyle of method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataValidation\\:\\:setErrorStyle\\(\\) expects string, int\\|string given\\.$#"
2295-
count: 1
2296-
path: src/PhpSpreadsheet/Reader/Xls.php
2297-
2298-
-
2299-
message: "#^Parameter \\#1 \\$operator of method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataValidation\\:\\:setOperator\\(\\) expects string, int\\|string given\\.$#"
2300-
count: 1
2301-
path: src/PhpSpreadsheet/Reader/Xls.php
2302-
23032293
-
23042294
message: "#^Parameter \\#1 \\$showSummaryBelow of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\Worksheet\\:\\:setShowSummaryBelow\\(\\) expects bool, int given\\.$#"
23052295
count: 1
@@ -2310,16 +2300,6 @@ parameters:
23102300
count: 1
23112301
path: src/PhpSpreadsheet/Reader/Xls.php
23122302

2313-
-
2314-
message: "#^Parameter \\#1 \\$type of method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataValidation\\:\\:setType\\(\\) expects string, int\\|string given\\.$#"
2315-
count: 1
2316-
path: src/PhpSpreadsheet/Reader/Xls.php
2317-
2318-
-
2319-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, array\\<string\\>\\|string given\\.$#"
2320-
count: 1
2321-
path: src/PhpSpreadsheet/Reader/Xls.php
2322-
23232303
-
23242304
message: "#^Parameter \\#2 \\$row of method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\IReadFilter\\:\\:readCell\\(\\) expects int, string given\\.$#"
23252305
count: 1

src/PhpSpreadsheet/Calculation/Information/ErrorValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static function isError($value = '')
4747
return false;
4848
}
4949

50-
return in_array($value, ExcelError::$errorCodes);
50+
return in_array($value, ExcelError::$errorCodes) || $value === ExcelError::CALC();
5151
}
5252

5353
/**

src/PhpSpreadsheet/Calculation/Information/ExcelError.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ExcelError
2222
'num' => '#NUM!',
2323
'na' => '#N/A',
2424
'gettingdata' => '#GETTING_DATA',
25+
'spill' => '#SPILL!',
2526
];
2627

2728
/**
@@ -45,6 +46,10 @@ public static function type($value = '')
4546
++$i;
4647
}
4748

49+
if ($value === self::CALC()) {
50+
return 14;
51+
}
52+
4853
return self::NA();
4954
}
5055

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+
}

tests/data/Calculation/Information/ERROR_TYPE.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@
5353
7,
5454
'#N/A',
5555
],
56+
[
57+
9,
58+
'#SPILL!',
59+
],
60+
[
61+
14,
62+
'#CALC!',
63+
],
5664
];

0 commit comments

Comments
 (0)