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