Stack.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\Token;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class Stack
  5. {
  6. /**
  7. * The parser stack for formulae.
  8. *
  9. * @var mixed[]
  10. */
  11. private $stack = [];
  12. /**
  13. * Count of entries in the parser stack.
  14. *
  15. * @var int
  16. */
  17. private $count = 0;
  18. /**
  19. * Return the number of entries on the stack.
  20. *
  21. * @return int
  22. */
  23. public function count()
  24. {
  25. return $this->count;
  26. }
  27. /**
  28. * Push a new entry onto the stack.
  29. *
  30. * @param mixed $type
  31. * @param mixed $value
  32. * @param mixed $reference
  33. */
  34. public function push($type, $value, $reference = null)
  35. {
  36. $this->stack[$this->count++] = [
  37. 'type' => $type,
  38. 'value' => $value,
  39. 'reference' => $reference,
  40. ];
  41. if ($type == 'Function') {
  42. $localeFunction = Calculation::localeFunc($value);
  43. if ($localeFunction != $value) {
  44. $this->stack[($this->count - 1)]['localeValue'] = $localeFunction;
  45. }
  46. }
  47. }
  48. /**
  49. * Pop the last entry from the stack.
  50. *
  51. * @return mixed
  52. */
  53. public function pop()
  54. {
  55. if ($this->count > 0) {
  56. return $this->stack[--$this->count];
  57. }
  58. return null;
  59. }
  60. /**
  61. * Return an entry from the stack without removing it.
  62. *
  63. * @param int $n number indicating how far back in the stack we want to look
  64. *
  65. * @return mixed
  66. */
  67. public function last($n = 1)
  68. {
  69. if ($this->count - $n < 0) {
  70. return null;
  71. }
  72. return $this->stack[$this->count - $n];
  73. }
  74. /**
  75. * Clear the stack.
  76. */
  77. public function clear()
  78. {
  79. $this->stack = [];
  80. $this->count = 0;
  81. }
  82. }