1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Exception;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @method Blanks notBlank()
|
|
|
11 |
* @method Blanks notEmpty()
|
|
|
12 |
* @method Blanks isBlank()
|
|
|
13 |
* @method Blanks isEmpty()
|
|
|
14 |
*/
|
|
|
15 |
class Blanks extends WizardAbstract implements WizardInterface
|
|
|
16 |
{
|
|
|
17 |
protected const OPERATORS = [
|
|
|
18 |
'notBlank' => false,
|
|
|
19 |
'isBlank' => true,
|
|
|
20 |
'notEmpty' => false,
|
|
|
21 |
'empty' => true,
|
|
|
22 |
];
|
|
|
23 |
|
|
|
24 |
protected const EXPRESSIONS = [
|
|
|
25 |
Wizard::NOT_BLANKS => 'LEN(TRIM(%s))>0',
|
|
|
26 |
Wizard::BLANKS => 'LEN(TRIM(%s))=0',
|
|
|
27 |
];
|
|
|
28 |
|
|
|
29 |
protected bool $inverse;
|
|
|
30 |
|
|
|
31 |
public function __construct(string $cellRange, bool $inverse = false)
|
|
|
32 |
{
|
|
|
33 |
parent::__construct($cellRange);
|
|
|
34 |
$this->inverse = $inverse;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
protected function inverse(bool $inverse): void
|
|
|
38 |
{
|
|
|
39 |
$this->inverse = $inverse;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
protected function getExpression(): void
|
|
|
43 |
{
|
|
|
44 |
$this->expression = sprintf(
|
|
|
45 |
self::EXPRESSIONS[$this->inverse ? Wizard::BLANKS : Wizard::NOT_BLANKS],
|
|
|
46 |
$this->referenceCell
|
|
|
47 |
);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function getConditional(): Conditional
|
|
|
51 |
{
|
|
|
52 |
$this->getExpression();
|
|
|
53 |
|
|
|
54 |
$conditional = new Conditional();
|
|
|
55 |
$conditional->setConditionType(
|
|
|
56 |
$this->inverse ? Conditional::CONDITION_CONTAINSBLANKS : Conditional::CONDITION_NOTCONTAINSBLANKS
|
|
|
57 |
);
|
|
|
58 |
$conditional->setConditions([$this->expression]);
|
|
|
59 |
$conditional->setStyle($this->getStyle());
|
|
|
60 |
$conditional->setStopIfTrue($this->getStopIfTrue());
|
|
|
61 |
|
|
|
62 |
return $conditional;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
|
|
|
66 |
{
|
|
|
67 |
if (
|
|
|
68 |
$conditional->getConditionType() !== Conditional::CONDITION_CONTAINSBLANKS
|
|
|
69 |
&& $conditional->getConditionType() !== Conditional::CONDITION_NOTCONTAINSBLANKS
|
|
|
70 |
) {
|
|
|
71 |
throw new Exception('Conditional is not a Blanks CF Rule conditional');
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$wizard = new self($cellRange);
|
|
|
75 |
$wizard->style = $conditional->getStyle();
|
|
|
76 |
$wizard->stopIfTrue = $conditional->getStopIfTrue();
|
|
|
77 |
$wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_CONTAINSBLANKS;
|
|
|
78 |
|
|
|
79 |
return $wizard;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @param mixed[] $arguments
|
|
|
84 |
*/
|
|
|
85 |
public function __call(string $methodName, array $arguments): self
|
|
|
86 |
{
|
|
|
87 |
if (!array_key_exists($methodName, self::OPERATORS)) {
|
|
|
88 |
throw new Exception('Invalid Operation for Blanks CF Rule Wizard');
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$this->inverse(self::OPERATORS[$methodName]);
|
|
|
92 |
|
|
|
93 |
return $this;
|
|
|
94 |
}
|
|
|
95 |
}
|