Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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      /**
 119       * Create a new Conditional.
 120       */
 121      public function __construct()
 122      {
 123          // Initialise values
 124          $this->style = new Style(false, true);
 125      }
 126  
 127      /**
 128       * Get Condition type.
 129       *
 130       * @return string
 131       */
 132      public function getConditionType()
 133      {
 134          return $this->conditionType;
 135      }
 136  
 137      /**
 138       * Set Condition type.
 139       *
 140       * @param string $type Condition type, see self::CONDITION_*
 141       *
 142       * @return $this
 143       */
 144      public function setConditionType($type)
 145      {
 146          $this->conditionType = $type;
 147  
 148          return $this;
 149      }
 150  
 151      /**
 152       * Get Operator type.
 153       *
 154       * @return string
 155       */
 156      public function getOperatorType()
 157      {
 158          return $this->operatorType;
 159      }
 160  
 161      /**
 162       * Set Operator type.
 163       *
 164       * @param string $type Conditional operator type, see self::OPERATOR_*
 165       *
 166       * @return $this
 167       */
 168      public function setOperatorType($type)
 169      {
 170          $this->operatorType = $type;
 171  
 172          return $this;
 173      }
 174  
 175      /**
 176       * Get text.
 177       *
 178       * @return string
 179       */
 180      public function getText()
 181      {
 182          return $this->text;
 183      }
 184  
 185      /**
 186       * Set text.
 187       *
 188       * @param string $text
 189       *
 190       * @return $this
 191       */
 192      public function setText($text)
 193      {
 194          $this->text = $text;
 195  
 196          return $this;
 197      }
 198  
 199      /**
 200       * Get StopIfTrue.
 201       *
 202       * @return bool
 203       */
 204      public function getStopIfTrue()
 205      {
 206          return $this->stopIfTrue;
 207      }
 208  
 209      /**
 210       * Set StopIfTrue.
 211       *
 212       * @param bool $stopIfTrue
 213       *
 214       * @return $this
 215       */
 216      public function setStopIfTrue($stopIfTrue)
 217      {
 218          $this->stopIfTrue = $stopIfTrue;
 219  
 220          return $this;
 221      }
 222  
 223      /**
 224       * Get Conditions.
 225       *
 226       * @return (bool|float|int|string)[]
 227       */
 228      public function getConditions()
 229      {
 230          return $this->condition;
 231      }
 232  
 233      /**
 234       * Set Conditions.
 235       *
 236       * @param bool|float|int|string|(bool|float|int|string)[] $conditions Condition
 237       *
 238       * @return $this
 239       */
 240      public function setConditions($conditions)
 241      {
 242          if (!is_array($conditions)) {
 243              $conditions = [$conditions];
 244          }
 245          $this->condition = $conditions;
 246  
 247          return $this;
 248      }
 249  
 250      /**
 251       * Add Condition.
 252       *
 253       * @param bool|float|int|string $condition Condition
 254       *
 255       * @return $this
 256       */
 257      public function addCondition($condition)
 258      {
 259          $this->condition[] = $condition;
 260  
 261          return $this;
 262      }
 263  
 264      /**
 265       * Get Style.
 266       *
 267       * @return Style
 268       */
 269      public function getStyle()
 270      {
 271          return $this->style;
 272      }
 273  
 274      /**
 275       * Set Style.
 276       *
 277       * @return $this
 278       */
 279      public function setStyle(Style $style)
 280      {
 281          $this->style = $style;
 282  
 283          return $this;
 284      }
 285  
 286      /**
 287       * get DataBar.
 288       *
 289       * @return null|ConditionalDataBar
 290       */
 291      public function getDataBar()
 292      {
 293          return $this->dataBar;
 294      }
 295  
 296      /**
 297       * set DataBar.
 298       *
 299       * @return $this
 300       */
 301      public function setDataBar(ConditionalDataBar $dataBar)
 302      {
 303          $this->dataBar = $dataBar;
 304  
 305          return $this;
 306      }
 307  
 308      /**
 309       * Get hash code.
 310       *
 311       * @return string Hash code
 312       */
 313      public function getHashCode()
 314      {
 315          return md5(
 316              $this->conditionType .
 317              $this->operatorType .
 318              implode(';', $this->condition) .
 319              $this->style->getHashCode() .
 320              __CLASS__
 321          );
 322      }
 323  
 324      /**
 325       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 326       */
 327      public function __clone()
 328      {
 329          $vars = get_object_vars($this);
 330          foreach ($vars as $key => $value) {
 331              if (is_object($value)) {
 332                  $this->$key = clone $value;
 333              } else {
 334                  $this->$key = $value;
 335              }
 336          }
 337      }
 338  
 339      /**
 340       * Verify if param is valid condition type.
 341       */
 342      public static function isValidConditionType(string $type): bool
 343      {
 344          return in_array($type, self::CONDITION_TYPES);
 345      }
 346  }