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