Browse Source

added table\Table::toText()

Fabian Peter Hammerle 7 years ago
parent
commit
2698a207f9
2 changed files with 170 additions and 1 deletions
  1. 49 1
      table/Table.php
  2. 121 0
      tests/table/TableTest.php

+ 49 - 1
table/Table.php

@@ -109,7 +109,7 @@ class Table
         $columns_number = $this->columnsCount;
         $empty_row_csv = (new Row)->toCSV($delimiter, $columns_number);
         $rows_csv = [];
-        $rows_number = sizeof($this->_rows) > 0 ? max(array_keys($this->_rows)) + 1 : 0;
+        $rows_number = $this->rowsCount;
         for($row_index = 0; $row_index < $rows_number; $row_index++) {
             $rows_csv[] = isset($this->_rows[$row_index])
                 ? $this->_rows[$row_index]->toCSV($delimiter, $columns_number)
@@ -118,6 +118,54 @@ class Table
         return implode('', $rows_csv);
     }
 
+    /**
+     * @return string
+     */
+    public function toText()
+    {
+        $rows_number = $this->rowsCount;
+        $cols_number = $this->columnsCount;
+        if($rows_number == 0) {
+            return '';
+        } elseif($cols_number == 0) {
+            return str_repeat("\n", $rows_number);
+        }
+        $cols_max_length = [];
+        $string_table = new self();
+        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';
+                } else {
+                    $cell_value_string = (string)$cell_value;
+                }
+                $string_table->setCellValue($row_index, $col_index, $cell_value_string);
+                $cols_max_length[$col_index] = max(
+                    strlen($cell_value_string),
+                    $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
+                        );
+                }
+                return $line;
+                },
+            $string_table->_rows
+            )) . "\n";
+    }
+
     /**
      * @param array<mixed> $rows associative array
      * @return Table

+ 121 - 0
tests/table/TableTest.php

@@ -388,4 +388,125 @@ class TableTest extends \PHPUnit_Framework_TestCase
         $this->assertNull($t->getCell(2, 1)->value);
         $this->assertSame('r2c3', $t->getCell(2, 3)->value);
     }
+
+    public function toTextProvider()
+    {
+        return [
+            [
+                new Table([]),
+                '',
+                ],
+            [
+                new Table([
+                    [],
+                    ]),
+                "\n",
+                ],
+            [
+                new Table([
+                    [],
+                    [],
+                    ]),
+                "\n\n",
+                ],
+            [
+                new Table([
+                    ['22'],
+                    ]),
+                "22\n",
+                ],
+            [
+                new Table([
+                    [null],
+                    ]),
+                "\n",
+                ],
+            [
+                new Table([
+                    ['1', '', '22', '333'],
+                    ]),
+                "1  22 333\n",
+                ],
+            [
+                new Table([
+                    [],
+                    ['2', '', '22', '333'],
+                    ['22'],
+                    ]),
+                implode("\n", [
+                    '          ',
+                    '2   22 333',
+                    '22        ',
+                    ]) . "\n",
+                ],
+            [
+                new Table([
+                    ['22', '1', '2',  '3'  ],
+                    ['2',  '',  '22', '333'],
+                    ]),
+                implode("\n", [
+                    '22 1 2  3  ',
+                    '2    22 333',
+                    ]) . "\n",
+                ],
+            [
+                new Table([
+                    ['33',  '',  '', '22', ''],
+                    ['3',   '1', '', '2',  ''],
+                    ['333', '',  '', '2',  ''],
+                    ]),
+                implode("\n", [
+                    '33     22 ',
+                    '3   1  2  ',
+                    '333    2  ',
+                    ]) . "\n",
+                ],
+            [
+                new Table([
+                    [true, false, null,  '',   1    ],
+                    ['__', null,  '***', 3.33, '   '],
+                    ]),
+                implode("\n", [
+                    '1  0          1  ',
+                    '__   *** 3.33    ',
+                    ]) . "\n",
+                ],
+            ];
+    }
+
+    /**
+     * @dataProvider toTextProvider
+     */
+    public function testToText($table, $expected_text)
+    {
+        $this->assertSame($expected_text, $table->toText());
+    }
+
+    public function associativeArrayToTextProvider()
+    {
+        return [
+            [
+                [
+                    ['A' => 'a', 'B' => 'bb'],
+                    ['B' => 'bbb', 'CCC' => 'c'],
+                    ['D' => 'd'],
+                    ],
+                implode("\n", [
+                    'A B   CCC D',
+                    'a bb       ',
+                    '  bbb c    ',
+                    '          d',
+                    ]) . "\n",
+                ],
+            ];
+    }
+
+    /**
+     * @dataProvider associativeArrayToTextProvider
+     */
+    public function testAssociativeArrayToText($rows, $expected_text)
+    {
+        $t = Table::fromAssociativeArray($rows);
+        $this->assertSame($expected_text, $t->toText());
+    }
 }