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.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception;
   6  use PhpOffice\PhpSpreadsheet\Style\Conditional;
   7  
   8  /**
   9   * @method Errors duplicates()
  10   * @method Errors unique()
  11   */
  12  class Duplicates extends WizardAbstract implements WizardInterface
  13  {
  14      protected const OPERATORS = [
  15          'duplicates' => false,
  16          'unique' => true,
  17      ];
  18  
  19      /**
  20       * @var bool
  21       */
  22      protected $inverse;
  23  
  24      public function __construct(string $cellRange, bool $inverse = false)
  25      {
  26          parent::__construct($cellRange);
  27          $this->inverse = $inverse;
  28      }
  29  
  30      protected function inverse(bool $inverse): void
  31      {
  32          $this->inverse = $inverse;
  33      }
  34  
  35      public function getConditional(): Conditional
  36      {
  37          $conditional = new Conditional();
  38          $conditional->setConditionType(
  39              $this->inverse ? Conditional::CONDITION_UNIQUE : Conditional::CONDITION_DUPLICATES
  40          );
  41          $conditional->setStyle($this->getStyle());
  42          $conditional->setStopIfTrue($this->getStopIfTrue());
  43  
  44          return $conditional;
  45      }
  46  
  47      public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
  48      {
  49          if (
  50              $conditional->getConditionType() !== Conditional::CONDITION_DUPLICATES &&
  51              $conditional->getConditionType() !== Conditional::CONDITION_UNIQUE
  52          ) {
  53              throw new Exception('Conditional is not a Duplicates CF Rule conditional');
  54          }
  55  
  56          $wizard = new self($cellRange);
  57          $wizard->style = $conditional->getStyle();
  58          $wizard->stopIfTrue = $conditional->getStopIfTrue();
  59          $wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_UNIQUE;
  60  
  61          return $wizard;
  62      }
  63  
  64      /**
  65       * @param string $methodName
  66       * @param mixed[] $arguments
  67       */
  68      public function __call($methodName, $arguments): self
  69      {
  70          if (!array_key_exists($methodName, self::OPERATORS)) {
  71              throw new Exception('Invalid Operation for Errors CF Rule Wizard');
  72          }
  73  
  74          $this->inverse(self::OPERATORS[$methodName]);
  75  
  76          return $this;
  77      }
  78  }