Skip to content

Commit 83161de

Browse files
author
MarkBaker
committed
Some refactoring to extract the logic for calculating the CF operand tokens and size, to reduce duplicated code
1 parent 9ca9d74 commit 83161de

File tree

2 files changed

+95
-51
lines changed

2 files changed

+95
-51
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6+
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
7+
8+
class ConditionalHelper
9+
{
10+
/**
11+
* Formula parser.
12+
*
13+
* @var Parser
14+
*/
15+
protected $parser;
16+
17+
/**
18+
* @var mixed
19+
*/
20+
protected $condition;
21+
22+
/**
23+
* @var string
24+
*/
25+
protected $cellRange;
26+
27+
/**
28+
* @var null|string
29+
*/
30+
protected $tokens;
31+
32+
/**
33+
* @var int
34+
*/
35+
protected $size;
36+
37+
public function __construct(Parser $parser)
38+
{
39+
$this->parser = $parser;
40+
}
41+
42+
/**
43+
* @param mixed $condition
44+
*/
45+
public function processCondition($condition, string $cellRange): void
46+
{
47+
$this->condition = $condition;
48+
$this->cellRange = $cellRange;
49+
50+
if (is_int($condition) || is_float($condition)) {
51+
$this->size = ($condition <= 65535 ? 3 : 0x0000);
52+
$this->tokens = pack('Cv', 0x1E, $condition);
53+
} else {
54+
try {
55+
$formula = Wizard\WizardAbstract::reverseAdjustCellRef((string) $condition, $cellRange);
56+
$this->parser->parse($formula);
57+
$this->tokens = $this->parser->toReversePolish();
58+
$this->size = strlen($this->tokens);
59+
} catch (PhpSpreadsheetException $e) {
60+
var_dump("PARSER EXCEPTION: {$e->getMessage()}");
61+
$this->tokens = null;
62+
$this->size = 0;
63+
}
64+
}
65+
}
66+
67+
public function tokens(): ?string
68+
{
69+
return $this->tokens;
70+
}
71+
72+
public function size(): int
73+
{
74+
return $this->size;
75+
}
76+
}

src/PhpSpreadsheet/Writer/Xls/Worksheet.php

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PhpOffice\PhpSpreadsheet\Style\Border;
1414
use PhpOffice\PhpSpreadsheet\Style\Color;
1515
use PhpOffice\PhpSpreadsheet\Style\Conditional;
16-
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
1716
use PhpOffice\PhpSpreadsheet\Style\Protection;
1817
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
1918
use PhpOffice\PhpSpreadsheet\Worksheet\SheetView;
@@ -548,6 +547,8 @@ public function close(): void
548547

549548
private function writeConditionalFormatting(): void
550549
{
550+
$conditionalFormulaHelper = new ConditionalHelper($this->parser);
551+
551552
$arrConditionalStyles = $this->phpSheet->getConditionalStylesCollection();
552553
if (!empty($arrConditionalStyles)) {
553554
$arrConditional = [];
@@ -570,7 +571,7 @@ private function writeConditionalFormatting(): void
570571
$arrConditional[$conditional->getHashCode()] = true;
571572

572573
// Write CFRULE record
573-
$this->writeCFRule($conditional, $cellCoordinate);
574+
$this->writeCFRule($conditionalFormulaHelper, $conditional, $cellCoordinate);
574575
}
575576
}
576577
}
@@ -2780,8 +2781,11 @@ private function writePageLayoutView(): void
27802781
/**
27812782
* Write CFRule Record.
27822783
*/
2783-
private function writeCFRule(Conditional $conditional, string $cellRange): void
2784-
{
2784+
private function writeCFRule(
2785+
ConditionalHelper $conditionalFormulaHelper,
2786+
Conditional $conditional,
2787+
string $cellRange
2788+
): void {
27852789
$record = 0x01B1; // Record identifier
27862790
$type = null; // Type of the CF
27872791
$operatorType = null; // Comparison operator
@@ -2839,53 +2843,17 @@ private function writeCFRule(Conditional $conditional, string $cellRange): void
28392843
$operand1 = null;
28402844
$operand2 = null;
28412845

2842-
if ($numConditions == 1) {
2843-
if (is_int($arrConditions[0]) || is_float($arrConditions[0])) {
2844-
$szValue1 = ($arrConditions[0] <= 65535 ? 3 : 0x0000);
2845-
$operand1 = pack('Cv', 0x1E, $arrConditions[0]);
2846-
} else {
2847-
try {
2848-
$formula1 = Wizard\WizardAbstract::reverseAdjustCellRef((string) $arrConditions[0], $cellRange);
2849-
$this->parser->parse($formula1);
2850-
$formula1 = $this->parser->toReversePolish();
2851-
$szValue1 = strlen($formula1);
2852-
} catch (PhpSpreadsheetException $e) {
2853-
var_dump("PARSER EXCEPTION: {$e->getMessage()}");
2854-
$formula1 = null;
2855-
}
2856-
$operand1 = $formula1;
2857-
}
2858-
} elseif ($numConditions == 2 && ($conditional->getOperatorType() == Conditional::OPERATOR_BETWEEN)) {
2859-
if (is_int($arrConditions[0]) || is_float($arrConditions[0])) {
2860-
$szValue1 = ($arrConditions[0] <= 65535 ? 3 : 0x0000);
2861-
$operand1 = pack('Cv', 0x1E, $arrConditions[0]);
2862-
} else {
2863-
try {
2864-
$formula1 = Wizard\WizardAbstract::reverseAdjustCellRef((string) $arrConditions[0], $cellRange);
2865-
$this->parser->parse($formula1);
2866-
$formula1 = $this->parser->toReversePolish();
2867-
$szValue1 = strlen($formula1);
2868-
} catch (PhpSpreadsheetException $e) {
2869-
var_dump("PARSER EXCEPTION: {$e->getMessage()}");
2870-
$formula1 = null;
2871-
}
2872-
$operand1 = $formula1;
2873-
}
2874-
if (is_int($arrConditions[1]) || is_float($arrConditions[1])) {
2875-
$szValue2 = ($arrConditions[1] <= 65535 ? 3 : 0x0000);
2876-
$operand2 = pack('Cv', 0x1E, $arrConditions[1]);
2877-
} else {
2878-
try {
2879-
$formula2 = Wizard\WizardAbstract::reverseAdjustCellRef((string) $arrConditions[1], $cellRange);
2880-
$this->parser->parse($formula2);
2881-
$formula2 = $this->parser->toReversePolish();
2882-
$szValue2 = strlen($formula2);
2883-
} catch (PhpSpreadsheetException $e) {
2884-
var_dump("PARSER EXCEPTION: {$e->getMessage()}");
2885-
$formula2 = null;
2886-
}
2887-
$operand2 = $formula2;
2888-
}
2846+
if ($numConditions === 1) {
2847+
$conditionalFormulaHelper->processCondition($arrConditions[0], $cellRange);
2848+
$szValue1 = $conditionalFormulaHelper->size();
2849+
$operand1 = $conditionalFormulaHelper->tokens();
2850+
} elseif ($numConditions === 2 && ($conditional->getOperatorType() === Conditional::OPERATOR_BETWEEN)) {
2851+
$conditionalFormulaHelper->processCondition($arrConditions[0], $cellRange);
2852+
$szValue1 = $conditionalFormulaHelper->size();
2853+
$operand1 = $conditionalFormulaHelper->tokens();
2854+
$conditionalFormulaHelper->processCondition($arrConditions[1], $cellRange);
2855+
$szValue2 = $conditionalFormulaHelper->size();
2856+
$operand2 = $conditionalFormulaHelper->tokens();
28892857
}
28902858

28912859
// $flags : Option flags

0 commit comments

Comments
 (0)