DocumentGenerator.php 3.5 KB

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