Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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