Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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