Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Add support for wireless (blank) Text\Table - properly aligned column…
Browse files Browse the repository at this point in the history
…s and rows without any visible dividers.
  • Loading branch information
Thinkscape committed Jul 19, 2012
1 parent 89b66ed commit cb5a749
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 53 deletions.
132 changes: 132 additions & 0 deletions src/Table/Decorator/Blank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Text
*/

namespace Zend\Text\Table\Decorator;

use Zend\Text\Table\Decorator\DecoratorInterface as Decorator;

/**
* ASCII Decorator for Zend\Text\Table
*
* @category Zend
* @package Zend_Text_Table
*/
class Blank implements Decorator
{
/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getTopLeft()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getTopRight()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getBottomLeft()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getBottomRight()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getVertical()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getHorizontal()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getCross()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getVerticalRight()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getVerticalLeft()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getHorizontalDown()
{
return '';
}

/**
* Defined by Zend\Text\Table\Decorator\DecoratorInterface
*
* @return string
*/
public function getHorizontalUp()
{
return '';
}
}
108 changes: 55 additions & 53 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public function render()
$numColumns = count($columnWidths);

// Check what we have to draw
if ($rowNum === 0) {
if ($rowNum === 0 && $this->_decorator->getHorizontal() !== '') {
// If this is the first row, draw the table top
$result .= $this->_decorator->getTopLeft();

Expand Down Expand Up @@ -399,62 +399,64 @@ public function render()
$currentUpperWidth = 0;
$currentLowerWidth = 0;

// Loop through all column widths
foreach ($this->_columnWidths as $columnNum => $columnWidth) {
// Add the horizontal line
$result .= str_repeat($this->_decorator->getHorizontal(),
$columnWidth);
// Add horizontal lines, but only if there is any horizontal character
if($this->_decorator->getHorizontal() !== ''){
// Loop through all column widths
foreach ($this->_columnWidths as $columnNum => $columnWidth) {
// Add the horizontal line
$result .= str_repeat($this->_decorator->getHorizontal(),
$columnWidth);

// If this is the last line, break out
if (($columnNum + 1) === $totalNumColumns) {
break;
}

// Else check, which connector style has to be used
$connector = 0x0;
$currentUpperWidth += $columnWidth;
$currentLowerWidth += $columnWidth;

if ($lastColumnWidths[$currentUpperColumn] === $currentUpperWidth) {
$connector |= 0x1;
$currentUpperColumn += 1;
$currentUpperWidth = 0;
} else {
$currentUpperWidth += 1;
}

if ($columnWidths[$currentLowerColumn] === $currentLowerWidth) {
$connector |= 0x2;
$currentLowerColumn += 1;
$currentLowerWidth = 0;
} else {
$currentLowerWidth += 1;
}

switch ($connector) {
case 0x0:
$result .= $this->_decorator->getHorizontal();
break;

case 0x1:
$result .= $this->_decorator->getHorizontalUp();
break;

case 0x2:
$result .= $this->_decorator->getHorizontalDown();
break;

case 0x3:
$result .= $this->_decorator->getCross();
break;

default:
// This can never happen, but the CS tells I have to have it ...
// If this is the last line, break out
if (($columnNum + 1) === $totalNumColumns) {
break;
}

// Else check, which connector style has to be used
$connector = 0x0;
$currentUpperWidth += $columnWidth;
$currentLowerWidth += $columnWidth;

if ($lastColumnWidths[$currentUpperColumn] === $currentUpperWidth) {
$connector |= 0x1;
$currentUpperColumn += 1;
$currentUpperWidth = 0;
} else {
$currentUpperWidth += 1;
}

if ($columnWidths[$currentLowerColumn] === $currentLowerWidth) {
$connector |= 0x2;
$currentLowerColumn += 1;
$currentLowerWidth = 0;
} else {
$currentLowerWidth += 1;
}

switch ($connector) {
case 0x0:
$result .= $this->_decorator->getHorizontal();
break;

case 0x1:
$result .= $this->_decorator->getHorizontalUp();
break;

case 0x2:
$result .= $this->_decorator->getHorizontalDown();
break;

case 0x3:
$result .= $this->_decorator->getCross();
break;

default:
// This can never happen, but the CS tells I have to have it ...
break;
}
}
$result .= $this->_decorator->getVerticalLeft() . "\n";
}

$result .= $this->_decorator->getVerticalLeft() . "\n";
}
}

Expand Down

0 comments on commit cb5a749

Please sign in to comment.