2017 © Pedro Peláez
 

library php-array-table

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

image

dekor/php-array-table

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

  • Wednesday, October 25, 2017
  • by deniskoronets
  • Repository
  • 1 Watchers
  • 2 Stars
  • 4,472 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 32 % Grown

The README.md

PHP Array To Text Table

packagist downloads min php version license tests code coverage badge lines of code, (*1)

PHP-class, which allows to transform php associative arrays to cool ASCII tables., (*2)

BlueYellow Ukraine ❤, (*3)

Quick doc:

https://deniskoronets.github.io/php-array-table/, (*4)

Installation

Simply run composer require:, (*5)

composer require dekor/php-array-table

or add to composer.json:, (*6)

"dekor/php-array-table": "^2.0"

Usage

use dekor\ArrayToTextTable;

$data = [
    [
        'id' => 1,
        'name' => 'Denis Koronets',
        'role' => 'php developer',
    ],
    [
        'id' => 2,
        'name' => 'Maxim Ambroskin',
        'role' => 'java developer',
    ],
    [
        'id' => 3,
        'name' => 'Andrew Sikorsky',
        'role' => 'php developer',
    ]
];

echo (new ArrayToTextTable($data))->render();

Will draw the next output:, (*7)

+----+-----------------+----------------+
| id | name            | role           |
+----+-----------------+----------------+
| 1  | Denis Koronets  | php developer  |
| 2  | Maxim Ambroskin | java developer |
| 3  | Andrew Sikorsky | php developer  |
+----+-----------------+----------------+

Formatters (since v2)

Version 2 introduces a new feature that allows to pre and postprocess column data by applying filters., (*8)

You're able to develop your own formatters by extending BaseColumnFormatter and implementing abstract methods., (*9)

List of formatters out of the box: - AlignFormatter - allows to set text align for inner column (useful for numbers):, (*10)

use dekor\ArrayToTextTable;
use dekor\formatters\AlignFormatter;

$data = [
    [
        'left' => 2,
        'center' => 'Dummy one',
        'right' => 14.33,
    ],
    [
        'left' => 3,
        'center' => 'Another great day for a great inventers!',
        'right' => 1,
    ],
];

$builder = new ArrayToTextTable($data);
$builder->applyFormatter(new AlignFormatter(['center' => 'center', 'right' => 'right']));

echo $builder->render();

outputs:, (*11)

+------+------------------------------------------+-------+
| left | center                                   | right |
+------+------------------------------------------+-------+
| 2    |                Dummy one                 | 14.33 |
| 3    | Another great day for a great inventers! |     1 |
+------+------------------------------------------+-------+
  • SprintfFormatter - allows to format column value before rendering using sprintf function (ex: %01.3f)
use dekor\ArrayToTextTable;
use dekor\formatters\SprintfFormatter;

$data = [
    [
        'left' => 1,
        'right' => 2.89,
    ]
];

$builder = new ArrayToTextTable($data);
$builder->applyFormatter(new SprintfFormatter(['left' => '%01.3f', 'right' => '%03.3f']));

echo $builder->render();

outputs:, (*12)

+-------+-------+
| left  | right |
+-------+-------+
| 1.000 | 2.890 |
+-------+-------+
  • ColorFormatter - allows to highlight text with specific color (only works in terminal):
use dekor\ArrayToTextTable;
use dekor\formatters\ColorFormatter;

$data = [
    ['test' => 1],
    ['test' => -1],
];

$builder = new ArrayToTextTable($data);
$builder->applyFormatter(new ColorFormatter(['test' => fn ($value) => $value > 0 ? 'Red' : 'Green']));

echo $builder->render() . PHP_EOL;

outputs:, (*13)

img.png, (*14)

Allowed colors list (see ColorFormatter::$colors) - Black - Dark Grey - Red - Light Red - Green - Light Green - Brown - Yellow - Blue - Light Blue - Magenta - Light Magenta - Cyan - Light Cyan - Light Grey - White, (*15)

Our sponsors list:

, (*16)

The Versions

25/10 2017

dev-master

9999999-dev

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

php library