diff --git a/.gitignore b/.gitignore index 88e99d5..826bff9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ vendor -composer.lock \ No newline at end of file +composer.lock +.idea +nbproject \ No newline at end of file diff --git a/README.md b/README.md index 0cb347c..ec6ba71 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,385 @@ The converter allows you to convert an array into an ASCII formatted string. You can see such ASCII formatted tables when working in command line with MySQL or PostgreSQL. +## Available converters +ToolkitLab\ASCII\Formatter\MysqlFormatter +``` ++---+-------+----------+ +| | A | B | ++---+-------+----------+ +| 1 | Name | Position | +| 2 | John | Writer | +| 3 | Anna | Student | +| 4 | David | Teacher | ++---+-------+----------+ +``` +ToolkitLab\ASCII\Formatter\UnicodeFormatter +``` +╔═══╦═══════╦══════════╗ +║ ║ A ║ B ║ +╠═══╬═══════╬══════════╣ +║ 1 ║ Name ║ Position ║ +║ 2 ║ John ║ Writer ║ +║ 3 ║ Anna ║ Student ║ +║ 4 ║ David ║ Teacher ║ +╚═══╩═══════╩══════════╝ +``` +ToolkitLab\ASCII\Formatter\DotsFormatter +``` +........................ +: : A : B : +:...:.......:..........: +: 1 : Name : Position : +: 2 : John : Writer : +: 3 : Anna : Student : +: 4 : David : Teacher : +:...:.......:..........: +``` +ToolkitLab\ASCII\Formatter\MarkdownFormatter +``` +| | A | B | +|---|-------|----------| +| 1 | Name | Position | +| 2 | John | Writer | +| 3 | Anna | Student | +| 4 | David | Teacher | +``` +ToolkitLab\ASCII\Formatter\TableFormatter +``` + ___ _______ __________ +| | A | B | +|___|_______|__________| +| 1 | Name | Position | +| 2 | John | Writer | +| 3 | Anna | Student | +| 4 | David | Teacher | + ¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯ +``` + +## Usage + +```php +format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` + +this will output the following: + +``` ++-------+----------+ +| Name | Position | +| John | Writer | +| Anna | Student | +| David | Teacher | ++-------+----------+ +``` + +#### Modes +There are different modes available, which add additional formatting to the output: + +##### Use the first row as a header +```php + AbstractFormatter::HEADER_FIRST_ROW_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++-------+----------+ +| Name | Position | ++-------+----------+ +| John | Writer | +| Anna | Student | +| David | Teacher | ++-------+----------+ +``` +##### Use a numbered header +```php + AbstractFormatter::HEADER_NUMERIC_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++-------+----------+ +| 1 | 2 | ++-------+----------+ +| Name | Position | +| John | Writer | +| Anna | Student | +| David | Teacher | ++-------+----------+ +``` +##### Use an alphabetical header +```php + AbstractFormatter::HEADER_ABC_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++-------+----------+ +| A | B | ++-------+----------+ +| Name | Position | +| John | Writer | +| Anna | Student | +| David | Teacher | ++-------+----------+ +``` + +##### Use a numbered sidebar +```php + AbstractFormatter::SIDEBAR_NUMERIC_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++---+-------+----------+ +| 1 | Name | Position | +| 2 | John | Writer | +| 3 | Anna | Student | +| 4 | David | Teacher | ++---+-------+----------+ +``` + +##### Use an alphabetical sidebar +```php + AbstractFormatter::SIDEBAR_ABC_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++---+-------+----------+ +| A | Name | Position | +| B | John | Writer | +| C | Anna | Student | +| D | David | Teacher | ++---+-------+----------+ +``` + +##### Apply spreadsheet mode +```php + AbstractFormatter::SPREADSHEET_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++---+-------+----------+ +| | A | B | ++---+-------+----------+ +| 1 | Name | Position | +| 2 | John | Writer | +| 3 | Anna | Student | +| 4 | David | Teacher | ++---+-------+----------+ +``` + +##### Using multiple modes +```php + AbstractFormatter::SIDEBAR_NUMERIC_MODE | AbstractFormatter::HEADER_NUMERIC_MODE +]); +echo $formatter->format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +]); + +?> +``` +will output: +``` ++---+-------+----------+ +| | 1 | 2 | ++---+-------+----------+ +| 1 | Name | Position | +| 2 | John | Writer | +| 3 | Anna | Student | +| 4 | David | Teacher | ++---+-------+----------+ +``` + +##### Rotate +```php +format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +], [ + 'rotate' => -90 +]); + +?> +``` +will output: +``` ++----------+--------+---------+---------+ +| Position | Writer | Student | Teacher | +| Name | John | Anna | David | ++----------+--------+---------+---------+ +``` +##### Set max cell length +The parameter "max_cell_length" allows you to set the maximum cell length. If the length is exceeded all further text will be replaced by three dots (the ending can be set with the parameter "max_cell_ending"). Default value is 100. +```php +format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +], [ + 'max_cell_length' => 5, +]); + +?> +``` +will output: +``` ++-------+----------+ +| Name | Posit... | +| John | Write... | +| Anna | Stude... | +| David | Teach... | ++-------+----------+ +``` +##### Set ending for exceeded max cell length +The parameter "max_cell_ending" allows you to set the ending if the maximum cell length is exceeded. Default value is "...". +```php +format([ + ["Name", "Position"], + ["John", "Writer"], + ["Anna", "Student"], + ["David", "Teacher"], +], [ + 'max_cell_length' => 5, + 'max_cell_ending' => '[hidden]', +]); + +?> +``` +will output: +``` ++-------+---------------+ +| Name | Posit[hidden] | +| John | Write[hidden] | +| Anna | Stude[hidden] | +| David | Teach[hidden] | ++-------+---------------+ +``` [ico-version]: https://img.shields.io/packagist/v/toolkitlab/ascii-converter.svg?style=flat-square [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square diff --git a/src/AbstractFormatter.php b/src/AbstractFormatter.php index c01023f..27b7643 100644 --- a/src/AbstractFormatter.php +++ b/src/AbstractFormatter.php @@ -70,12 +70,12 @@ public function __construct($params = []) { /** * Converts an array into ASCII-formatted string (table) - * @param mixed $data + * @param array $data * @param array $params * @return string */ - public function format($data, $params = []) { - $table = $data instanceof Table ? $data : new Table($data); + public function format(array $data, $params = []) { + $table = new Table($data); $this->init($table, $params); $this->addTopBorder(); $this->addHeader(); diff --git a/src/FormatterInterface.php b/src/FormatterInterface.php index db1ba03..facf0cc 100644 --- a/src/FormatterInterface.php +++ b/src/FormatterInterface.php @@ -11,5 +11,5 @@ interface FormatterInterface * @param array $params * @return string */ - public function format($data, $params = []); + public function format(array $data, $params = []); } \ No newline at end of file diff --git a/tests/AsciiTableFormatterTest.php b/tests/AsciiTableFormatterTest.php index dfa70b6..4191b27 100644 --- a/tests/AsciiTableFormatterTest.php +++ b/tests/AsciiTableFormatterTest.php @@ -266,7 +266,44 @@ public function testSpreadsheetMode() { STR; $this->assertEquals($result, $expect); } - + + + public function testNumericSidebarAbcHeaderMode() { + $formatter = new TableFormatter([ + 'mode' => AbstractFormatter::SIDEBAR_NUMERIC_MODE | AbstractFormatter::HEADER_ABC_MODE, + ]); + $result = $formatter->format($this->testData); + $expect = <<assertEquals($result, $expect); + } + + public function testNumericSidebarNumericHeaderMode() { + $formatter = new TableFormatter([ + 'mode' => AbstractFormatter::SIDEBAR_NUMERIC_MODE | AbstractFormatter::HEADER_NUMERIC_MODE, + ]); + $result = $formatter->format($this->testData); + $expect = <<assertEquals($result, $expect); + } + /** * @expectedException LogicException */ @@ -286,7 +323,7 @@ public function testInitSidebarException() { ]); $formatter->format($this->testData); } - + public function testMaxLength() { $formatter = new TableFormatter(['max_cell_length' => 4]); $data = [