Browse Source

table\Table::toText(): support multiline strings

Fabian Peter Hammerle 7 years ago
parent
commit
0b41e08f3f
2 changed files with 63 additions and 16 deletions
  1. 27 16
      table/Table.php
  2. 36 0
      tests/table/TableTest.php

+ 27 - 16
table/Table.php

@@ -131,38 +131,49 @@ class Table
             return str_repeat("\n", $rows_number);
         }
         $cols_max_length = [];
-        $string_table = new self();
+        $string_table = [];
         for($col_index = 0; $col_index < $cols_number; $col_index++) {
             $cols_max_length[$col_index] = 0;
             for($row_index = 0; $row_index < $rows_number; $row_index++) {
                 $cell_value = $this->getCell($row_index, $col_index)->value;
                 if($cell_value === false) {
-                    $cell_value_string = '0';
+                    $cell_value_lines = ['0'];
                 } else {
-                    $cell_value_string = (string)$cell_value;
+                    $cell_value_lines = explode("\n", (string)$cell_value);
+                    assert(sizeof($cell_value_lines) > 0);
                 }
-                $string_table->setCellValue($row_index, $col_index, $cell_value_string);
+                $string_table[$row_index][$col_index] = $cell_value_lines;
                 $cols_max_length[$col_index] = max(
-                    strlen($cell_value_string),
+                    max(array_map(
+                        function($line) { return strlen($line); },
+                        $cell_value_lines
+                        )),
                     $cols_max_length[$col_index]
                     );
             }
         }
         return implode("\n", array_map(
             function($row) use ($cols_number, $cols_max_length) {
-                $line = '';
-                for($col_index = 0; $col_index < $cols_number; $col_index++) {
-                    $line .= str_pad(
-                        $row->getCell($col_index)->value,
-                        $cols_max_length[$col_index]
-                            + ($col_index + 1 == $cols_number ? 0 : 1),
-                        ' ',
-                        STR_PAD_RIGHT
-                        );
+                $lines_number = max(array_map(function($c) { return sizeof($c); }, $row));
+                $lines = [];
+                for($line_index = 0; $line_index < $lines_number; $line_index++) {
+                    $line = '';
+                    for($col_index = 0; $col_index < $cols_number; $col_index++) {
+                        $line .= str_pad(
+                            isset($row[$col_index][$line_index])
+                                ? $row[$col_index][$line_index]
+                                : '',
+                            $cols_max_length[$col_index]
+                                + ($col_index + 1 == $cols_number ? 0 : 1),
+                            ' ',
+                            STR_PAD_RIGHT
+                            );
+                    }
+                    $lines[] = $line;
                 }
-                return $line;
+                return implode("\n", $lines);
                 },
-            $string_table->_rows
+            $string_table
             )) . "\n";
     }
 

+ 36 - 0
tests/table/TableTest.php

@@ -471,6 +471,22 @@ class TableTest extends \PHPUnit_Framework_TestCase
                     '__   *** 3.33    ',
                     ]) . "\n",
                 ],
+            [
+                new Table([
+                    ['r0c0',     "r0\nc1", 'r0c2'  ],
+                    ["r1\n\nc0", 'r1c1',   "\nr1c2"],
+                    ["r2c0\n",   'r2c1',   'r2c2'  ],
+                    ]),
+                implode("\n", [
+                    'r0c0 r0   r0c2',
+                    '     c1       ',
+                    'r1   r1c1     ',
+                    '          r1c2',
+                    'c0            ',
+                    'r2c0 r2c1 r2c2',
+                    '              ',
+                    ]) . "\n",
+                ],
             ];
     }
 
@@ -498,6 +514,26 @@ class TableTest extends \PHPUnit_Framework_TestCase
                     '          d',
                     ]) . "\n",
                 ],
+            [
+                [
+                    ['a' => 'laaang', 'b' => 'kurz'],
+                    ['a' => 'kurz', 'c' => "new\nline"],
+                    ['a' => '1st', 'b' => "\n2nd", 'c' => "\n\n3rd"],
+                    ['b' => "\ncenter\n"],
+                    ],
+                implode("\n", [
+                    'a      b      c   ',
+                    'laaang kurz       ',
+                    'kurz          new ',
+                    '              line',
+                    '1st               ',
+                    '       2nd        ',
+                    '              3rd ',
+                    '                  ',
+                    '       center     ',
+                    '                  ',
+                    ]) . "\n",
+                ],
             ];
     }