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\Calculation\Token;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Engine\BranchPruner;
   7  
   8  class Stack
   9  {
  10      /**
  11       * @var BranchPruner
  12       */
  13      private $branchPruner;
  14  
  15      /**
  16       * The parser stack for formulae.
  17       *
  18       * @var mixed[]
  19       */
  20      private $stack = [];
  21  
  22      /**
  23       * Count of entries in the parser stack.
  24       *
  25       * @var int
  26       */
  27      private $count = 0;
  28  
  29      public function __construct(BranchPruner $branchPruner)
  30      {
  31          $this->branchPruner = $branchPruner;
  32      }
  33  
  34      /**
  35       * Return the number of entries on the stack.
  36       */
  37      public function count(): int
  38      {
  39          return $this->count;
  40      }
  41  
  42      /**
  43       * Push a new entry onto the stack.
  44       *
  45       * @param mixed $value
  46       */
  47      public function push(string $type, $value, ?string $reference = null): void
  48      {
  49          $stackItem = $this->getStackItem($type, $value, $reference);
  50          $this->stack[$this->count++] = $stackItem;
  51  
  52          if ($type == 'Function') {
  53              $localeFunction = Calculation::localeFunc($value);
  54              if ($localeFunction != $value) {
  55                  $this->stack[($this->count - 1)]['localeValue'] = $localeFunction;
  56              }
  57          }
  58      }
  59  
  60      public function pushStackItem(array $stackItem): void
  61      {
  62          $this->stack[$this->count++] = $stackItem;
  63      }
  64  
  65      /**
  66       * @param mixed $value
  67       */
  68      public function getStackItem(string $type, $value, ?string $reference = null): array
  69      {
  70          $stackItem = [
  71              'type' => $type,
  72              'value' => $value,
  73              'reference' => $reference,
  74          ];
  75  
  76          // will store the result under this alias
  77          $storeKey = $this->branchPruner->currentCondition();
  78          if (isset($storeKey) || $reference === 'NULL') {
  79              $stackItem['storeKey'] = $storeKey;
  80          }
  81  
  82          // will only run computation if the matching store key is true
  83          $onlyIf = $this->branchPruner->currentOnlyIf();
  84          if (isset($onlyIf) || $reference === 'NULL') {
  85              $stackItem['onlyIf'] = $onlyIf;
  86          }
  87  
  88          // will only run computation if the matching store key is false
  89          $onlyIfNot = $this->branchPruner->currentOnlyIfNot();
  90          if (isset($onlyIfNot) || $reference === 'NULL') {
  91              $stackItem['onlyIfNot'] = $onlyIfNot;
  92          }
  93  
  94          return $stackItem;
  95      }
  96  
  97      /**
  98       * Pop the last entry from the stack.
  99       */
 100      public function pop(): ?array
 101      {
 102          if ($this->count > 0) {
 103              return $this->stack[--$this->count];
 104          }
 105  
 106          return null;
 107      }
 108  
 109      /**
 110       * Return an entry from the stack without removing it.
 111       */
 112      public function last(int $n = 1): ?array
 113      {
 114          if ($this->count - $n < 0) {
 115              return null;
 116          }
 117  
 118          return $this->stack[$this->count - $n];
 119      }
 120  
 121      /**
 122       * Clear the stack.
 123       */
 124      public function clear(): void
 125      {
 126          $this->stack = [];
 127          $this->count = 0;
 128      }
 129  }