Skip to content

Commit

Permalink
Merge pull request #166 from igormukhingmailcom/consoletablewriter
Browse files Browse the repository at this point in the history
ConsoleTableWriter
  • Loading branch information
Baachi committed Mar 19, 2015
2 parents 455c239 + f53f3f8 commit afd71ba
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Documentation
- [DoctrineWriter](#doctrinewriter)
- [PdoWriter](#pdowriter)
- [ExcelWriter](#excelwriter)
- [ConsoleTableWriter](#consoletablewriter)
- [ConsoleProgressWriter](#consoleprogresswriter)
- [CallbackWriter](#callbackwriter)
- [AbstractStreamWriter](#abstractstreamwriter)
Expand Down Expand Up @@ -545,6 +546,34 @@ existing sheet:

```php
$writer = new ExcelWriter($file, 'Old sheet');
```
#### ConsoleTableWriter

This writer displays items as table on console output for debug purposes
when you start the workflow from the command-line.
It requires Symfony’s Console component 2.5 or higher:

```bash
$ composer require symfony/console ~2.5
```

```php
use Ddeboer\DataImport\Reader;
use Ddeboer\DataImport\Writer\ConsoleTableWriter;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Helper\Table;

$reader = new Reader\...;
$output = new ConsoleOutput(...);

$table = new Table($output);

// Make some manipulations, e.g. set table style
$table->setStyle('compact');

$workflow = new Workflow($reader);
$workflow->addWriter(new ConsoleTableWriter($output, $table));

```

#### ConsoleProgressWriter
Expand Down
65 changes: 65 additions & 0 deletions src/Ddeboer/DataImport/Writer/ConsoleTableWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Ddeboer\DataImport\Writer;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

/**
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
class ConsoleTableWriter implements WriterInterface
{
private $output = null;
private $table = null;
private $firstItem = null;

/**
* @param OutputInterface $output
* @param Table $table
*/
public function __construct(OutputInterface $output, Table $table) {
$this->output = $output;
$this->table = $table;
}

/**
* {@inheritdoc}
*/
public function prepare() {

}

/**
* {@inheritdoc}
*/
public function writeItem(array $item) {

// Save first item to get keys to display at header
if (is_null($this->firstItem)) {
$this->firstItem = $item;
}

$this->table->addRow($item);
}

/**
* {@inheritdoc}
*/
public function finish() {
$this->table->setHeaders(array_keys($this->firstItem));
$this->table->render();

$this->firstItem = null;
}

/**
* You can get Table object to apply extra
*
* @return Table
*/
public function getTable()
{
return $this->table;
}
}
52 changes: 52 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/ConsoleTableWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Ddeboer\DataImport\Tests\Writer;

use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Helper\Table;

use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Reader\ArrayReader;
use Ddeboer\DataImport\ItemConverter\MappingItemConverter;
use Ddeboer\DataImport\Writer\ConsoleTableWriter;

/**
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
class ConsoleTableWriterTest extends \PHPUnit_Framework_TestCase
{
public function testRightColumnsHeadersNamesAfterItemConverter()
{
$data = array(
array(
'first' => 'John',
'lastname' => 'Doe'
),
array(
'first' => 'Ivan',
'lastname' => 'Sidorov'
)
);
$reader = new ArrayReader($data);

$converter = new MappingItemConverter();
$converter
->addMapping('first', 'firstname')
;

$output = new BufferedOutput();
$table = new Table($output);
$table
->setStyle('compact')
;

$workflow = new Workflow($reader);
$workflow
->addItemConverter($converter)
->addWriter(new ConsoleTableWriter($output, $table))
->process()
;

$this->assertRegExp('/\s+lastname\s+firstname\s+Doe\s+John\s+Sidorov\s+Ivan\s+/', $output->fetch());
}
}

0 comments on commit afd71ba

Please sign in to comment.