Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   6  use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
   7  
   8  class Column
   9  {
  10      const AUTOFILTER_FILTERTYPE_FILTER = 'filters';
  11      const AUTOFILTER_FILTERTYPE_CUSTOMFILTER = 'customFilters';
  12      //    Supports no more than 2 rules, with an And/Or join criteria
  13      //        if more than 1 rule is defined
  14      const AUTOFILTER_FILTERTYPE_DYNAMICFILTER = 'dynamicFilter';
  15      //    Even though the filter rule is constant, the filtered data can vary
  16      //        e.g. filtered by date = TODAY
  17      const AUTOFILTER_FILTERTYPE_TOPTENFILTER = 'top10';
  18  
  19      /**
  20       * Types of autofilter rules.
  21       *
  22       * @var string[]
  23       */
  24      private static $filterTypes = [
  25          //    Currently we're not handling
  26          //        colorFilter
  27          //        extLst
  28          //        iconFilter
  29          self::AUTOFILTER_FILTERTYPE_FILTER,
  30          self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER,
  31          self::AUTOFILTER_FILTERTYPE_DYNAMICFILTER,
  32          self::AUTOFILTER_FILTERTYPE_TOPTENFILTER,
  33      ];
  34  
  35      // Multiple Rule Connections
  36      const AUTOFILTER_COLUMN_JOIN_AND = 'and';
  37      const AUTOFILTER_COLUMN_JOIN_OR = 'or';
  38  
  39      /**
  40       * Join options for autofilter rules.
  41       *
  42       * @var string[]
  43       */
  44      private static $ruleJoins = [
  45          self::AUTOFILTER_COLUMN_JOIN_AND,
  46          self::AUTOFILTER_COLUMN_JOIN_OR,
  47      ];
  48  
  49      /**
  50       * Autofilter.
  51       *
  52       * @var AutoFilter
  53       */
  54      private $parent;
  55  
  56      /**
  57       * Autofilter Column Index.
  58       *
  59       * @var string
  60       */
  61      private $columnIndex = '';
  62  
  63      /**
  64       * Autofilter Column Filter Type.
  65       *
  66       * @var string
  67       */
  68      private $filterType = self::AUTOFILTER_FILTERTYPE_FILTER;
  69  
  70      /**
  71       * Autofilter Multiple Rules And/Or.
  72       *
  73       * @var string
  74       */
  75      private $join = self::AUTOFILTER_COLUMN_JOIN_OR;
  76  
  77      /**
  78       * Autofilter Column Rules.
  79       *
  80       * @var array of Column\Rule
  81       */
  82      private $ruleset = [];
  83  
  84      /**
  85       * Autofilter Column Dynamic Attributes.
  86       *
  87       * @var array of mixed
  88       */
  89      private $attributes = [];
  90  
  91      /**
  92       * Create a new Column.
  93       *
  94       * @param string $pColumn Column (e.g. A)
  95       * @param AutoFilter $pParent Autofilter for this column
  96       */
  97      public function __construct($pColumn, AutoFilter $pParent = null)
  98      {
  99          $this->columnIndex = $pColumn;
 100          $this->parent = $pParent;
 101      }
 102  
 103      /**
 104       * Get AutoFilter column index as string eg: 'A'.
 105       *
 106       * @return string
 107       */
 108      public function getColumnIndex()
 109      {
 110          return $this->columnIndex;
 111      }
 112  
 113      /**
 114       * Set AutoFilter column index as string eg: 'A'.
 115       *
 116       * @param string $pColumn Column (e.g. A)
 117       *
 118       * @throws PhpSpreadsheetException
 119       *
 120       * @return Column
 121       */
 122      public function setColumnIndex($pColumn)
 123      {
 124          // Uppercase coordinate
 125          $pColumn = strtoupper($pColumn);
 126          if ($this->parent !== null) {
 127              $this->parent->testColumnInRange($pColumn);
 128          }
 129  
 130          $this->columnIndex = $pColumn;
 131  
 132          return $this;
 133      }
 134  
 135      /**
 136       * Get this Column's AutoFilter Parent.
 137       *
 138       * @return AutoFilter
 139       */
 140      public function getParent()
 141      {
 142          return $this->parent;
 143      }
 144  
 145      /**
 146       * Set this Column's AutoFilter Parent.
 147       *
 148       * @param AutoFilter $pParent
 149       *
 150       * @return Column
 151       */
 152      public function setParent(AutoFilter $pParent = null)
 153      {
 154          $this->parent = $pParent;
 155  
 156          return $this;
 157      }
 158  
 159      /**
 160       * Get AutoFilter Type.
 161       *
 162       * @return string
 163       */
 164      public function getFilterType()
 165      {
 166          return $this->filterType;
 167      }
 168  
 169      /**
 170       * Set AutoFilter Type.
 171       *
 172       * @param string $pFilterType
 173       *
 174       * @throws PhpSpreadsheetException
 175       *
 176       * @return Column
 177       */
 178      public function setFilterType($pFilterType)
 179      {
 180          if (!in_array($pFilterType, self::$filterTypes)) {
 181              throw new PhpSpreadsheetException('Invalid filter type for column AutoFilter.');
 182          }
 183  
 184          $this->filterType = $pFilterType;
 185  
 186          return $this;
 187      }
 188  
 189      /**
 190       * Get AutoFilter Multiple Rules And/Or Join.
 191       *
 192       * @return string
 193       */
 194      public function getJoin()
 195      {
 196          return $this->join;
 197      }
 198  
 199      /**
 200       * Set AutoFilter Multiple Rules And/Or.
 201       *
 202       * @param string $pJoin And/Or
 203       *
 204       * @throws PhpSpreadsheetException
 205       *
 206       * @return Column
 207       */
 208      public function setJoin($pJoin)
 209      {
 210          // Lowercase And/Or
 211          $pJoin = strtolower($pJoin);
 212          if (!in_array($pJoin, self::$ruleJoins)) {
 213              throw new PhpSpreadsheetException('Invalid rule connection for column AutoFilter.');
 214          }
 215  
 216          $this->join = $pJoin;
 217  
 218          return $this;
 219      }
 220  
 221      /**
 222       * Set AutoFilter Attributes.
 223       *
 224       * @param string[] $attributes
 225       *
 226       * @return Column
 227       */
 228      public function setAttributes(array $attributes)
 229      {
 230          $this->attributes = $attributes;
 231  
 232          return $this;
 233      }
 234  
 235      /**
 236       * Set An AutoFilter Attribute.
 237       *
 238       * @param string $pName Attribute Name
 239       * @param string $pValue Attribute Value
 240       *
 241       * @return Column
 242       */
 243      public function setAttribute($pName, $pValue)
 244      {
 245          $this->attributes[$pName] = $pValue;
 246  
 247          return $this;
 248      }
 249  
 250      /**
 251       * Get AutoFilter Column Attributes.
 252       *
 253       * @return string[]
 254       */
 255      public function getAttributes()
 256      {
 257          return $this->attributes;
 258      }
 259  
 260      /**
 261       * Get specific AutoFilter Column Attribute.
 262       *
 263       * @param string $pName Attribute Name
 264       *
 265       * @return string
 266       */
 267      public function getAttribute($pName)
 268      {
 269          if (isset($this->attributes[$pName])) {
 270              return $this->attributes[$pName];
 271          }
 272  
 273          return null;
 274      }
 275  
 276      /**
 277       * Get all AutoFilter Column Rules.
 278       *
 279       * @return Column\Rule[]
 280       */
 281      public function getRules()
 282      {
 283          return $this->ruleset;
 284      }
 285  
 286      /**
 287       * Get a specified AutoFilter Column Rule.
 288       *
 289       * @param int $pIndex Rule index in the ruleset array
 290       *
 291       * @return Column\Rule
 292       */
 293      public function getRule($pIndex)
 294      {
 295          if (!isset($this->ruleset[$pIndex])) {
 296              $this->ruleset[$pIndex] = new Column\Rule($this);
 297          }
 298  
 299          return $this->ruleset[$pIndex];
 300      }
 301  
 302      /**
 303       * Create a new AutoFilter Column Rule in the ruleset.
 304       *
 305       * @return Column\Rule
 306       */
 307      public function createRule()
 308      {
 309          $this->ruleset[] = new Column\Rule($this);
 310  
 311          return end($this->ruleset);
 312      }
 313  
 314      /**
 315       * Add a new AutoFilter Column Rule to the ruleset.
 316       *
 317       * @param Column\Rule $pRule
 318       *
 319       * @return Column
 320       */
 321      public function addRule(Column\Rule $pRule)
 322      {
 323          $pRule->setParent($this);
 324          $this->ruleset[] = $pRule;
 325  
 326          return $this;
 327      }
 328  
 329      /**
 330       * Delete a specified AutoFilter Column Rule
 331       * If the number of rules is reduced to 1, then we reset And/Or logic to Or.
 332       *
 333       * @param int $pIndex Rule index in the ruleset array
 334       *
 335       * @return Column
 336       */
 337      public function deleteRule($pIndex)
 338      {
 339          if (isset($this->ruleset[$pIndex])) {
 340              unset($this->ruleset[$pIndex]);
 341              //    If we've just deleted down to a single rule, then reset And/Or joining to Or
 342              if (count($this->ruleset) <= 1) {
 343                  $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
 344              }
 345          }
 346  
 347          return $this;
 348      }
 349  
 350      /**
 351       * Delete all AutoFilter Column Rules.
 352       *
 353       * @return Column
 354       */
 355      public function clearRules()
 356      {
 357          $this->ruleset = [];
 358          $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
 359  
 360          return $this;
 361      }
 362  
 363      /**
 364       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 365       */
 366      public function __clone()
 367      {
 368          $vars = get_object_vars($this);
 369          foreach ($vars as $key => $value) {
 370              if ($key === 'parent') {
 371                  // Detach from autofilter parent
 372                  $this->parent = null;
 373              } elseif ($key === 'ruleset') {
 374                  // The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter objects
 375                  $this->ruleset = [];
 376                  foreach ($value as $k => $v) {
 377                      $cloned = clone $v;
 378                      $cloned->setParent($this); // attach the new cloned Rule to this new cloned Autofilter Cloned object
 379                      $this->ruleset[$k] = $cloned;
 380                  }
 381              } elseif (is_object($value)) {
 382                  $this->$key = clone $value;
 383              } else {
 384                  $this->$key = $value;
 385              }
 386          }
 387      }
 388  }