zhaojs
2023-05-16 ea24ddd0b978cbd3b0a900711b49b8a9c2db4186
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
 
namespace PhpOffice\PhpSpreadsheet;
 
use PhpOffice\PhpSpreadsheet\Calculation\Category;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use ReflectionClass;
use ReflectionException;
use UnexpectedValueException;
 
class DocumentGenerator
{
    /**
     * @param array[] $phpSpreadsheetFunctions
     *
     * @throws ReflectionException
     *
     * @return string
     */
    public static function generateFunctionListByCategory(array $phpSpreadsheetFunctions): string
    {
        $result = "# Function list by category\n";
        foreach (self::getCategories() as $categoryConstant => $category) {
            $result .= "\n";
            $result .= "## {$categoryConstant}\n";
            $result .= "\n";
            $lengths = [20, 42];
            $result .= self::tableRow($lengths, ['Excel Function', 'PhpSpreadsheet Function']) . "\n";
            $result .= self::tableRow($lengths, null) . "\n";
            foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
                if ($category === $functionInfo['category']) {
                    $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
                    $result .= self::tableRow($lengths, [$excelFunction, $phpFunction]) . "\n";
                }
            }
        }
 
        return $result;
    }
 
    /**
     * @throws ReflectionException
     *
     * @return array
     */
    private static function getCategories(): array
    {
        return (new ReflectionClass(Category::class))->getConstants();
    }
 
    private static function tableRow(array $lengths, array $values = null): string
    {
        $result = '';
        foreach (array_map(null, $lengths, $values ?? []) as $i => [$length, $value]) {
            $pad = $value === null ? '-' : ' ';
            if ($i > 0) {
                $result .= '|' . $pad;
            }
            $result .= str_pad($value ?? '', $length, $pad);
        }
 
        return rtrim($result, ' ');
    }
 
    private static function getPhpSpreadsheetFunctionText($functionCall): string
    {
        if (is_string($functionCall)) {
            return $functionCall;
        }
        if ($functionCall === [Functions::class, 'DUMMY']) {
            return '**Not yet Implemented**';
        }
        if (is_array($functionCall)) {
            return "\\{$functionCall[0]}::{$functionCall[1]}";
        }
 
        throw new UnexpectedValueException(
            '$functionCall is of type ' . gettype($functionCall) . '. string or array expected'
        );
    }
 
    /**
     * @param array[] $phpSpreadsheetFunctions
     *
     * @throws ReflectionException
     *
     * @return string
     */
    public static function generateFunctionListByName(array $phpSpreadsheetFunctions): string
    {
        $categoryConstants = array_flip(self::getCategories());
        $result = "# Function list by name\n";
        $lastAlphabet = null;
        foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
            $lengths = [20, 31, 42];
            if ($lastAlphabet !== $excelFunction[0]) {
                $lastAlphabet = $excelFunction[0];
                $result .= "\n";
                $result .= "## {$lastAlphabet}\n";
                $result .= "\n";
                $result .= self::tableRow($lengths, ['Excel Function', 'Category', 'PhpSpreadsheet Function']) . "\n";
                $result .= self::tableRow($lengths, null) . "\n";
            }
            $category = $categoryConstants[$functionInfo['category']];
            $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
            $result .= self::tableRow($lengths, [$excelFunction, $category, $phpFunction]) . "\n";
        }
 
        return $result;
    }
}