| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\IComparable; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalColorScale; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalIconSet; |
| 9 |
|
| 10 |
class Conditional implements IComparable |
| 11 |
{ |
| 12 |
// Condition types |
| 13 |
const CONDITION_NONE = 'none'; |
| 14 |
const CONDITION_BEGINSWITH = 'beginsWith'; |
| 15 |
const CONDITION_CELLIS = 'cellIs'; |
| 16 |
const CONDITION_COLORSCALE = 'colorScale'; |
| 17 |
const CONDITION_CONTAINSBLANKS = 'containsBlanks'; |
| 18 |
const CONDITION_CONTAINSERRORS = 'containsErrors'; |
| 19 |
const CONDITION_CONTAINSTEXT = 'containsText'; |
| 20 |
const CONDITION_DATABAR = 'dataBar'; |
| 21 |
const CONDITION_ENDSWITH = 'endsWith'; |
| 22 |
const CONDITION_EXPRESSION = 'expression'; |
| 23 |
const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks'; |
| 24 |
const CONDITION_NOTCONTAINSERRORS = 'notContainsErrors'; |
| 25 |
const CONDITION_NOTCONTAINSTEXT = 'notContainsText'; |
| 26 |
const CONDITION_TIMEPERIOD = 'timePeriod'; |
| 27 |
const CONDITION_DUPLICATES = 'duplicateValues'; |
| 28 |
const CONDITION_UNIQUE = 'uniqueValues'; |
| 29 |
const CONDITION_ICONSET = 'iconSet'; |
| 30 |
|
| 31 |
private const CONDITION_TYPES = [ |
| 32 |
self::CONDITION_BEGINSWITH, |
| 33 |
self::CONDITION_CELLIS, |
| 34 |
self::CONDITION_COLORSCALE, |
| 35 |
self::CONDITION_CONTAINSBLANKS, |
| 36 |
self::CONDITION_CONTAINSERRORS, |
| 37 |
self::CONDITION_CONTAINSTEXT, |
| 38 |
self::CONDITION_DATABAR, |
| 39 |
self::CONDITION_DUPLICATES, |
| 40 |
self::CONDITION_ENDSWITH, |
| 41 |
self::CONDITION_EXPRESSION, |
| 42 |
self::CONDITION_NONE, |
| 43 |
self::CONDITION_NOTCONTAINSBLANKS, |
| 44 |
self::CONDITION_NOTCONTAINSERRORS, |
| 45 |
self::CONDITION_NOTCONTAINSTEXT, |
| 46 |
self::CONDITION_TIMEPERIOD, |
| 47 |
self::CONDITION_UNIQUE, |
| 48 |
self::CONDITION_ICONSET, |
| 49 |
]; |
| 50 |
|
| 51 |
// Operator types |
| 52 |
const OPERATOR_NONE = ''; |
| 53 |
const OPERATOR_BEGINSWITH = 'beginsWith'; |
| 54 |
const OPERATOR_ENDSWITH = 'endsWith'; |
| 55 |
const OPERATOR_EQUAL = 'equal'; |
| 56 |
const OPERATOR_GREATERTHAN = 'greaterThan'; |
| 57 |
const OPERATOR_GREATERTHANOREQUAL = 'greaterThanOrEqual'; |
| 58 |
const OPERATOR_LESSTHAN = 'lessThan'; |
| 59 |
const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual'; |
| 60 |
const OPERATOR_NOTEQUAL = 'notEqual'; |
| 61 |
const OPERATOR_CONTAINSTEXT = 'containsText'; |
| 62 |
const OPERATOR_NOTCONTAINS = 'notContains'; |
| 63 |
const OPERATOR_BETWEEN = 'between'; |
| 64 |
const OPERATOR_NOTBETWEEN = 'notBetween'; |
| 65 |
|
| 66 |
const TIMEPERIOD_TODAY = 'today'; |
| 67 |
const TIMEPERIOD_YESTERDAY = 'yesterday'; |
| 68 |
const TIMEPERIOD_TOMORROW = 'tomorrow'; |
| 69 |
const TIMEPERIOD_LAST_7_DAYS = 'last7Days'; |
| 70 |
const TIMEPERIOD_LAST_WEEK = 'lastWeek'; |
| 71 |
const TIMEPERIOD_THIS_WEEK = 'thisWeek'; |
| 72 |
const TIMEPERIOD_NEXT_WEEK = 'nextWeek'; |
| 73 |
const TIMEPERIOD_LAST_MONTH = 'lastMonth'; |
| 74 |
const TIMEPERIOD_THIS_MONTH = 'thisMonth'; |
| 75 |
const TIMEPERIOD_NEXT_MONTH = 'nextMonth'; |
| 76 |
|
| 77 |
/** |
| 78 |
* Condition type. |
| 79 |
*/ |
| 80 |
private string $conditionType = self::CONDITION_NONE; |
| 81 |
|
| 82 |
/** |
| 83 |
* Operator type. |
| 84 |
*/ |
| 85 |
private string $operatorType = self::OPERATOR_NONE; |
| 86 |
|
| 87 |
/** |
| 88 |
* Text. |
| 89 |
*/ |
| 90 |
private string $text = ''; |
| 91 |
|
| 92 |
/** |
| 93 |
* Stop on this condition, if it matches. |
| 94 |
*/ |
| 95 |
private bool $stopIfTrue = false; |
| 96 |
|
| 97 |
/** |
| 98 |
* Condition. |
| 99 |
* |
| 100 |
* @var (bool|float|int|string)[] |
| 101 |
*/ |
| 102 |
private array $condition = []; |
| 103 |
|
| 104 |
private ?ConditionalDataBar $dataBar = null; |
| 105 |
|
| 106 |
private ?ConditionalColorScale $colorScale = null; |
| 107 |
|
| 108 |
private ?ConditionalIconSet $iconSet = null; |
| 109 |
|
| 110 |
private Style $style; |
| 111 |
|
| 112 |
private bool $noFormatSet = false; |
| 113 |
|
| 114 |
private int $priority = 0; |
| 115 |
|
| 116 |
/** |
| 117 |
* Create a new Conditional. |
| 118 |
*/ |
| 119 |
public function __construct() |
| 120 |
{ |
| 121 |
// Initialise values |
| 122 |
$this->style = new Style(false, true); |
| 123 |
} |
| 124 |
|
| 125 |
public function getPriority(): int |
| 126 |
{ |
| 127 |
return $this->priority; |
| 128 |
} |
| 129 |
|
| 130 |
public function setPriority(int $priority): self |
| 131 |
{ |
| 132 |
$this->priority = $priority; |
| 133 |
|
| 134 |
return $this; |
| 135 |
} |
| 136 |
|
| 137 |
public function getNoFormatSet(): bool |
| 138 |
{ |
| 139 |
return $this->noFormatSet; |
| 140 |
} |
| 141 |
|
| 142 |
public function setNoFormatSet(bool $noFormatSet): self |
| 143 |
{ |
| 144 |
$this->noFormatSet = $noFormatSet; |
| 145 |
|
| 146 |
return $this; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get Condition type. |
| 151 |
*/ |
| 152 |
public function getConditionType(): string |
| 153 |
{ |
| 154 |
return $this->conditionType; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Set Condition type. |
| 159 |
* |
| 160 |
* @param string $type Condition type, see self::CONDITION_* |
| 161 |
* |
| 162 |
* @return $this |
| 163 |
*/ |
| 164 |
public function setConditionType(string $type) |
| 165 |
{ |
| 166 |
$this->conditionType = $type; |
| 167 |
|
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get Operator type. |
| 173 |
*/ |
| 174 |
public function getOperatorType(): string |
| 175 |
{ |
| 176 |
return $this->operatorType; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Set Operator type. |
| 181 |
* |
| 182 |
* @param string $type Conditional operator type, see self::OPERATOR_* |
| 183 |
* |
| 184 |
* @return $this |
| 185 |
*/ |
| 186 |
public function setOperatorType(string $type) |
| 187 |
{ |
| 188 |
$this->operatorType = $type; |
| 189 |
|
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get text. |
| 195 |
*/ |
| 196 |
public function getText(): string |
| 197 |
{ |
| 198 |
return $this->text; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Set text. |
| 203 |
* |
| 204 |
* @return $this |
| 205 |
*/ |
| 206 |
public function setText(string $text) |
| 207 |
{ |
| 208 |
$this->text = $text; |
| 209 |
|
| 210 |
return $this; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get StopIfTrue. |
| 215 |
*/ |
| 216 |
public function getStopIfTrue(): bool |
| 217 |
{ |
| 218 |
return $this->stopIfTrue; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Set StopIfTrue. |
| 223 |
* |
| 224 |
* @return $this |
| 225 |
*/ |
| 226 |
public function setStopIfTrue(bool $stopIfTrue) |
| 227 |
{ |
| 228 |
$this->stopIfTrue = $stopIfTrue; |
| 229 |
|
| 230 |
return $this; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get Conditions. |
| 235 |
* |
| 236 |
* @return (bool|float|int|string)[] |
| 237 |
*/ |
| 238 |
public function getConditions(): array |
| 239 |
{ |
| 240 |
return $this->condition; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Set Conditions. |
| 245 |
* |
| 246 |
* @param bool|(bool|float|int|string)[]|float|int|string $conditions Condition |
| 247 |
* |
| 248 |
* @return $this |
| 249 |
*/ |
| 250 |
public function setConditions($conditions) |
| 251 |
{ |
| 252 |
if (!is_array($conditions)) { |
| 253 |
$conditions = [$conditions]; |
| 254 |
} |
| 255 |
$this->condition = $conditions; |
| 256 |
|
| 257 |
return $this; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Add Condition. |
| 262 |
* |
| 263 |
* @param bool|float|int|string $condition Condition |
| 264 |
* |
| 265 |
* @return $this |
| 266 |
*/ |
| 267 |
public function addCondition($condition) |
| 268 |
{ |
| 269 |
$this->condition[] = $condition; |
| 270 |
|
| 271 |
return $this; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Get Style. |
| 276 |
* @param mixed $cellData |
| 277 |
*/ |
| 278 |
public function getStyle($cellData = null): Style |
| 279 |
{ |
| 280 |
if ($this->conditionType === self::CONDITION_COLORSCALE && $cellData !== null && $this->colorScale !== null && is_numeric($cellData)) { |
| 281 |
$style = new Style(false, true); |
| 282 |
$style->getFill()->setFillType(Fill::FILL_SOLID); |
| 283 |
$style->getFill()->getStartColor()->setARGB($this->colorScale->getColorForValue((float) $cellData)); |
| 284 |
|
| 285 |
return $style; |
| 286 |
} |
| 287 |
|
| 288 |
return $this->style; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Set Style. |
| 293 |
* |
| 294 |
* @return $this |
| 295 |
*/ |
| 296 |
public function setStyle(Style $style) |
| 297 |
{ |
| 298 |
$this->style = $style; |
| 299 |
|
| 300 |
return $this; |
| 301 |
} |
| 302 |
|
| 303 |
public function getDataBar(): ?ConditionalDataBar |
| 304 |
{ |
| 305 |
return $this->dataBar; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @return static |
| 310 |
*/ |
| 311 |
public function setDataBar(ConditionalDataBar $dataBar) |
| 312 |
{ |
| 313 |
$this->dataBar = $dataBar; |
| 314 |
|
| 315 |
return $this; |
| 316 |
} |
| 317 |
|
| 318 |
public function getColorScale(): ?ConditionalColorScale |
| 319 |
{ |
| 320 |
return $this->colorScale; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* @return static |
| 325 |
*/ |
| 326 |
public function setColorScale(ConditionalColorScale $colorScale) |
| 327 |
{ |
| 328 |
$this->colorScale = $colorScale; |
| 329 |
|
| 330 |
return $this; |
| 331 |
} |
| 332 |
|
| 333 |
public function getIconSet(): ?ConditionalIconSet |
| 334 |
{ |
| 335 |
return $this->iconSet; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* @return static |
| 340 |
*/ |
| 341 |
public function setIconSet(ConditionalIconSet $iconSet) |
| 342 |
{ |
| 343 |
$this->iconSet = $iconSet; |
| 344 |
|
| 345 |
return $this; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Get hash code. |
| 350 |
* |
| 351 |
* @return string Hash code |
| 352 |
*/ |
| 353 |
public function getHashCode(): string |
| 354 |
{ |
| 355 |
return md5( |
| 356 |
$this->conditionType |
| 357 |
. $this->operatorType |
| 358 |
. implode(';', $this->condition) |
| 359 |
. $this->style->getHashCode() |
| 360 |
. __CLASS__ |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
| 366 |
*/ |
| 367 |
public function __clone() |
| 368 |
{ |
| 369 |
$vars = get_object_vars($this); |
| 370 |
foreach ($vars as $key => $value) { |
| 371 |
if (is_object($value)) { |
| 372 |
$this->$key = clone $value; |
| 373 |
} else { |
| 374 |
$this->$key = $value; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Verify if param is valid condition type. |
| 381 |
*/ |
| 382 |
public static function isValidConditionType(string $type): bool |
| 383 |
{ |
| 384 |
return in_array($type, self::CONDITION_TYPES); |
| 385 |
} |
| 386 |
} |
| 387 |
|