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
|
166 |
|
public function count() |
29
|
|
|
{ |
30
|
166 |
|
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
|
166 |
|
public function push( |
46
|
|
|
$type, |
47
|
|
|
$value, |
48
|
|
|
$reference = null, |
49
|
|
|
$storeKey = null, |
50
|
|
|
$onlyIf = null, |
51
|
|
|
$onlyIfNot = null |
52
|
|
|
) { |
53
|
166 |
|
$stackItem = $this->getStackItem($type, $value, $reference, $storeKey, $onlyIf, $onlyIfNot); |
54
|
|
|
|
55
|
166 |
|
$this->stack[$this->count++] = $stackItem; |
56
|
|
|
|
57
|
166 |
|
if ($type == 'Function') { |
58
|
62 |
|
$localeFunction = Calculation::localeFunc($value); |
59
|
62 |
|
if ($localeFunction != $value) { |
60
|
|
|
$this->stack[($this->count - 1)]['localeValue'] = $localeFunction; |
61
|
|
|
} |
62
|
|
|
} |
63
|
166 |
|
} |
64
|
|
|
|
65
|
166 |
|
public function getStackItem( |
66
|
|
|
$type, |
67
|
|
|
$value, |
68
|
|
|
$reference = null, |
69
|
|
|
$storeKey = null, |
70
|
|
|
$onlyIf = null, |
71
|
|
|
$onlyIfNot = null |
72
|
|
|
) { |
73
|
|
|
$stackItem = [ |
74
|
166 |
|
'type' => $type, |
75
|
166 |
|
'value' => $value, |
76
|
166 |
|
'reference' => $reference, |
77
|
|
|
]; |
78
|
|
|
|
79
|
166 |
|
if (isset($storeKey)) { |
80
|
25 |
|
$stackItem['storeKey'] = $storeKey; |
81
|
|
|
} |
82
|
|
|
|
83
|
166 |
|
if (isset($onlyIf)) { |
84
|
25 |
|
$stackItem['onlyIf'] = $onlyIf; |
85
|
|
|
} |
86
|
|
|
|
87
|
166 |
|
if (isset($onlyIfNot)) { |
88
|
22 |
|
$stackItem['onlyIfNot'] = $onlyIfNot; |
89
|
|
|
} |
90
|
|
|
|
91
|
166 |
|
return $stackItem; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Pop the last entry from the stack. |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
166 |
|
public function pop() |
100
|
|
|
{ |
101
|
166 |
|
if ($this->count > 0) { |
102
|
166 |
|
return $this->stack[--$this->count]; |
103
|
|
|
} |
104
|
|
|
|
105
|
166 |
|
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
|
166 |
|
public function last($n = 1) |
116
|
|
|
{ |
117
|
166 |
|
if ($this->count - $n < 0) { |
118
|
144 |
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
164 |
|
return $this->stack[$this->count - $n]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Clear the stack. |
126
|
|
|
*/ |
127
|
|
|
public function clear() |
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
|
|
|
} |
150
|
|
|
|