-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from igormukhingmailcom/consoletablewriter
ConsoleTableWriter
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
tests/Ddeboer/DataImport/Tests/Writer/ConsoleTableWriterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |