Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Category;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use ReflectionClass;
   8  use UnexpectedValueException;
   9  
  10  class DocumentGenerator
  11  {
  12      /**
  13       * @param array[] $phpSpreadsheetFunctions
  14       */
  15      public static function generateFunctionListByCategory(array $phpSpreadsheetFunctions): string
  16      {
  17          $result = "# Function list by category\n";
  18          foreach (self::getCategories() as $categoryConstant => $category) {
  19              $result .= "\n";
  20              $result .= "## {$categoryConstant}\n";
  21              $result .= "\n";
  22              $lengths = [20, 42];
  23              $result .= self::tableRow($lengths, ['Excel Function', 'PhpSpreadsheet Function']) . "\n";
  24              $result .= self::tableRow($lengths, null) . "\n";
  25              foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
  26                  if ($category === $functionInfo['category']) {
  27                      $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
  28                      $result .= self::tableRow($lengths, [$excelFunction, $phpFunction]) . "\n";
  29                  }
  30              }
  31          }
  32  
  33          return $result;
  34      }
  35  
  36      private static function getCategories(): array
  37      {
  38          return (new ReflectionClass(Category::class))->getConstants();
  39      }
  40  
  41      private static function tableRow(array $lengths, ?array $values = null): string
  42      {
  43          $result = '';
  44          foreach (array_map(null, $lengths, $values ?? []) as $i => [$length, $value]) {
  45              $pad = $value === null ? '-' : ' ';
  46              if ($i > 0) {
  47                  $result .= '|' . $pad;
  48              }
  49              $result .= str_pad($value ?? '', $length, $pad);
  50          }
  51  
  52          return rtrim($result, ' ');
  53      }
  54  
  55      private static function getPhpSpreadsheetFunctionText($functionCall): string
  56      {
  57          if (is_string($functionCall)) {
  58              return $functionCall;
  59          }
  60          if ($functionCall === [Functions::class, 'DUMMY']) {
  61              return '**Not yet Implemented**';
  62          }
  63          if (is_array($functionCall)) {
  64              return "\\{$functionCall[0]}::{$functionCall[1]}";
  65          }
  66  
  67          throw new UnexpectedValueException(
  68              '$functionCall is of type ' . gettype($functionCall) . '. string or array expected'
  69          );
  70      }
  71  
  72      /**
  73       * @param array[] $phpSpreadsheetFunctions
  74       */
  75      public static function generateFunctionListByName(array $phpSpreadsheetFunctions): string
  76      {
  77          $categoryConstants = array_flip(self::getCategories());
  78          $result = "# Function list by name\n";
  79          $lastAlphabet = null;
  80          foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
  81              $lengths = [20, 31, 42];
  82              if ($lastAlphabet !== $excelFunction[0]) {
  83                  $lastAlphabet = $excelFunction[0];
  84                  $result .= "\n";
  85                  $result .= "## {$lastAlphabet}\n";
  86                  $result .= "\n";
  87                  $result .= self::tableRow($lengths, ['Excel Function', 'Category', 'PhpSpreadsheet Function']) . "\n";
  88                  $result .= self::tableRow($lengths, null) . "\n";
  89              }
  90              $category = $categoryConstants[$functionInfo['category']];
  91              $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
  92              $result .= self::tableRow($lengths, [$excelFunction, $category, $phpFunction]) . "\n";
  93          }
  94  
  95          return $result;
  96      }
  97  }