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;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentHelper;
6
use PhpOffice\PhpSpreadsheet\Calculation\Engine\ArrayArgumentProcessor;
7
 
8
trait ArrayEnabled
9
{
10
    private static bool $initializationNeeded = true;
11
 
12
    private static ArrayArgumentHelper $arrayArgumentHelper;
13
 
14
    /**
15
     * @param array|false $arguments Can be changed to array for Php8.1+
16
     */
17
    private static function initialiseHelper($arguments): void
18
    {
19
        if (self::$initializationNeeded === true) {
20
            self::$arrayArgumentHelper = new ArrayArgumentHelper();
21
            self::$initializationNeeded = false;
22
        }
23
        self::$arrayArgumentHelper->initialise(($arguments === false) ? [] : $arguments);
24
    }
25
 
26
    /**
27
     * Handles array argument processing when the function accepts a single argument that can be an array argument.
28
     * Example use for:
29
     *         DAYOFMONTH() or FACT().
30
     */
31
    protected static function evaluateSingleArgumentArray(callable $method, array $values): array
32
    {
33
        $result = [];
34
        foreach ($values as $value) {
35
            $result[] = $method($value);
36
        }
37
 
38
        return $result;
39
    }
40
 
41
    /**
42
     * Handles array argument processing when the function accepts multiple arguments,
43
     *     and any of them can be an array argument.
44
     * Example use for:
45
     *         ROUND() or DATE().
46
     */
47
    protected static function evaluateArrayArguments(callable $method, mixed ...$arguments): array
48
    {
49
        self::initialiseHelper($arguments);
50
        $arguments = self::$arrayArgumentHelper->arguments();
51
 
52
        return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
53
    }
54
 
55
    /**
56
     * Handles array argument processing when the function accepts multiple arguments,
57
     *     but only the first few (up to limit) can be an array arguments.
58
     * Example use for:
59
     *         NETWORKDAYS() or CONCATENATE(), where the last argument is a matrix (or a series of values) that need
60
     *                                         to be treated as a such rather than as an array arguments.
61
     */
62
    protected static function evaluateArrayArgumentsSubset(callable $method, int $limit, mixed ...$arguments): array
63
    {
64
        self::initialiseHelper(array_slice($arguments, 0, $limit));
65
        $trailingArguments = array_slice($arguments, $limit);
66
        $arguments = self::$arrayArgumentHelper->arguments();
67
        $arguments = array_merge($arguments, $trailingArguments);
68
 
69
        return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
70
    }
71
 
72
    private static function testFalse(mixed $value): bool
73
    {
74
        return $value === false;
75
    }
76
 
77
    /**
78
     * Handles array argument processing when the function accepts multiple arguments,
79
     *     but only the last few (from start) can be an array arguments.
80
     * Example use for:
81
     *         Z.TEST() or INDEX(), where the first argument 1 is a matrix that needs to be treated as a dataset
82
     *                   rather than as an array argument.
83
     */
84
    protected static function evaluateArrayArgumentsSubsetFrom(callable $method, int $start, mixed ...$arguments): array
85
    {
86
        $arrayArgumentsSubset = array_combine(
87
            range($start, count($arguments) - $start),
88
            array_slice($arguments, $start)
89
        );
90
        if (self::testFalse($arrayArgumentsSubset)) {
91
            return ['#VALUE!'];
92
        }
93
 
94
        self::initialiseHelper($arrayArgumentsSubset);
95
        $leadingArguments = array_slice($arguments, 0, $start);
96
        $arguments = self::$arrayArgumentHelper->arguments();
97
        $arguments = array_merge($leadingArguments, $arguments);
98
 
99
        return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
100
    }
101
 
102
    /**
103
     * Handles array argument processing when the function accepts multiple arguments,
104
     *     and any of them can be an array argument except for the one specified by ignore.
105
     * Example use for:
106
     *         HLOOKUP() and VLOOKUP(), where argument 1 is a matrix that needs to be treated as a database
107
     *                                  rather than as an array argument.
108
     */
109
    protected static function evaluateArrayArgumentsIgnore(callable $method, int $ignore, mixed ...$arguments): array
110
    {
111
        $leadingArguments = array_slice($arguments, 0, $ignore);
112
        $ignoreArgument = array_slice($arguments, $ignore, 1);
113
        $trailingArguments = array_slice($arguments, $ignore + 1);
114
 
115
        self::initialiseHelper(array_merge($leadingArguments, [[null]], $trailingArguments));
116
        $arguments = self::$arrayArgumentHelper->arguments();
117
 
118
        array_splice($arguments, $ignore, 1, $ignoreArgument);
119
 
120
        return ArrayArgumentProcessor::processArguments(self::$arrayArgumentHelper, $method, ...$arguments);
121
    }
122
}