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  use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
   8  
   9  /**
  10   * @method Errors notError()
  11   * @method Errors isError()
  12   */
  13  class Errors extends WizardAbstract implements WizardInterface
  14  {
  15      protected const OPERATORS = [
  16          'notError' => false,
  17          'isError' => true,
  18      ];
  19  
  20      protected const EXPRESSIONS = [
  21          Wizard::NOT_ERRORS => 'NOT(ISERROR(%s))',
  22          Wizard::ERRORS => 'ISERROR(%s)',
  23      ];
  24  
  25      /**
  26       * @var bool
  27       */
  28      protected $inverse;
  29  
  30      public function __construct(string $cellRange, bool $inverse = false)
  31      {
  32          parent::__construct($cellRange);
  33          $this->inverse = $inverse;
  34      }
  35  
  36      protected function inverse(bool $inverse): void
  37      {
  38          $this->inverse = $inverse;
  39      }
  40  
  41      protected function getExpression(): void
  42      {
  43          $this->expression = sprintf(
  44              self::EXPRESSIONS[$this->inverse ? Wizard::ERRORS : Wizard::NOT_ERRORS],
  45              $this->referenceCell
  46          );
  47      }
  48  
  49      public function getConditional(): Conditional
  50      {
  51          $this->getExpression();
  52  
  53          $conditional = new Conditional();
  54          $conditional->setConditionType(
  55              $this->inverse ? Conditional::CONDITION_CONTAINSERRORS : Conditional::CONDITION_NOTCONTAINSERRORS
  56          );
  57          $conditional->setConditions([$this->expression]);
  58          $conditional->setStyle($this->getStyle());
  59          $conditional->setStopIfTrue($this->getStopIfTrue());
  60  
  61          return $conditional;
  62      }
  63  
  64      public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
  65      {
  66          if (
  67              $conditional->getConditionType() !== Conditional::CONDITION_CONTAINSERRORS &&
  68              $conditional->getConditionType() !== Conditional::CONDITION_NOTCONTAINSERRORS
  69          ) {
  70              throw new Exception('Conditional is not an Errors CF Rule conditional');
  71          }
  72  
  73          $wizard = new self($cellRange);
  74          $wizard->style = $conditional->getStyle();
  75          $wizard->stopIfTrue = $conditional->getStopIfTrue();
  76          $wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_CONTAINSERRORS;
  77  
  78          return $wizard;
  79      }
  80  
  81      /**
  82       * @param string $methodName
  83       * @param mixed[] $arguments
  84       */
  85      public function __call($methodName, $arguments): self
  86      {
  87          if (!array_key_exists($methodName, self::OPERATORS)) {
  88              throw new Exception('Invalid Operation for Errors CF Rule Wizard');
  89          }
  90  
  91          $this->inverse(self::OPERATORS[$methodName]);
  92  
  93          return $this;
  94      }
  95  }