Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentGenerator.php 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet;
  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 = [20, 42];
  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(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. $lengths = [20, 31, 42];
  72. if ($lastAlphabet !== $excelFunction[0]) {
  73. $lastAlphabet = $excelFunction[0];
  74. $result .= "\n";
  75. $result .= "## {$lastAlphabet}\n";
  76. $result .= "\n";
  77. $result .= self::tableRow($lengths, ['Excel Function', 'Category', 'PhpSpreadsheet Function']) . "\n";
  78. $result .= self::tableRow($lengths, null) . "\n";
  79. }
  80. $category = $categoryConstants[$functionInfo['category']];
  81. $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
  82. $result .= self::tableRow($lengths, [$excelFunction, $category, $phpFunction]) . "\n";
  83. }
  84. return $result;
  85. }
  86. }