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