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

Commit

Permalink
Merge branch 'master' of git://git.zendframework.com/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahar Evron committed Jul 25, 2010
5 parents 6bf667a + be37fde + aa8cf19 + c9dfc6a + 0e2cddf commit f1a4a26
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 51 deletions.
70 changes: 41 additions & 29 deletions src/MultiByte.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,58 @@ class MultiByte
*/
public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8')
{
$result = array();
$result = array();
$breakWidth = iconv_strlen($break, $charset);

while (($stringLength = iconv_strlen($string, $charset)) > 0) {
$subString = iconv_substr($string, 0, $width, $charset);
$breakPos = iconv_strpos($string, $break, 0, $charset);

if ($subString === $string) {
$cutLength = null;
if ($breakPos !== false && $breakPos < $width) {
if ($breakPos === $stringLength - $breakWidth) {
$subString = $string;
$cutLength = null;
} else {
$subString = iconv_substr($string, 0, $breakPos, $charset);
$cutLength = $breakPos + $breakWidth;
}
} else {
$nextChar = iconv_substr($string, $width, 1, $charset);
$subString = iconv_substr($string, 0, $width, $charset);

if ($nextChar === ' ' || $nextChar === $break) {
$afterNextChar = iconv_substr($string, $width + 1, 1, $charset);

if ($afterNextChar === false) {
$subString .= $nextChar;
}

$cutLength = iconv_strlen($subString, $charset) + 1;
if ($subString === $string) {
$cutLength = null;
} else {
$spacePos = iconv_strrpos($subString, ' ', $charset);
$nextChar = iconv_substr($string, $width, 1, $charset);

if ($breakWidth === 1) {
$nextBreak = $nextChar;
} else {
$nextBreak = iconv_substr($string, $breakWidth, 1, $charset);
}

if ($spacePos !== false) {
$subString = iconv_substr($subString, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else if ($cut === false) {
$spacePos = iconv_strpos($string, ' ', 0, $charset);
if ($nextChar === ' ' || $nextBreak === $break) {
$afterNextChar = iconv_substr($string, $width + 1, 1, $charset);

if ($spacePos !== false) {
$subString = iconv_substr($string, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else {
$subString = $string;
$cutLength = null;
if ($afterNextChar === false) {
$subString .= $nextChar;
}

$cutLength = iconv_strlen($subString, $charset) + 1;
} else {
$breakPos = iconv_strpos($subString, $break, 0, $charset);
$spacePos = iconv_strrpos($subString, ' ', $charset);

if ($breakPos !== false) {
$subString = iconv_substr($subString, 0, $breakPos, $charset);
$cutLength = $breakPos + 1;
if ($spacePos !== false) {
$subString = iconv_substr($subString, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else if ($cut === false) {
$spacePos = iconv_strpos($string, ' ', 0, $charset);

if ($spacePos !== false) {
$subString = iconv_substr($string, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else {
$subString = $string;
$cutLength = null;
}
} else {
$subString = iconv_substr($subString, 0, $width, $charset);
$cutLength = $width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* @namespace
*/
namespace Zend\Text\Table\Decorator;
namespace Zend\Text\Table;

/**
* Interface for Zend_Text_Table decorators
Expand All @@ -32,7 +32,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface DecoratorInterface
interface Decorator
{
/**
* Get a single character for the top left corner
Expand Down
6 changes: 4 additions & 2 deletions src/Table/Decorator/Ascii.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
*/
namespace Zend\Text\Table\Decorator;

use Zend\Text\Table\Decorator;

/**
* ASCII Decorator for Zend_Text_Table
*
* @uses \Zend\Text\Table\Decorator\DecoratorInterface
* @uses \Zend\Text\Table\Decorator
* @category Zend
* @package Zend_Text_Table
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Ascii implements DecoratorInterface
class Ascii implements Decorator
{
/**
* Defined by Zend_Text_Table_Decorator_Interface
Expand Down
6 changes: 4 additions & 2 deletions src/Table/Decorator/Unicode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
*/
namespace Zend\Text\Table\Decorator;

use Zend\Text\Table\Decorator;

/**
* Unicode Decorator for Zend_Text_Table
*
* @uses \Zend\Text\Table\Decorator\DecoratorInterface
* @uses \Zend\Text\Table\Decorator
* @category Zend
* @package Zend_Text_Table
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Unicode implements DecoratorInterface
class Unicode implements Decorator
{
/**
* Defined by Zend_Text_Table_Decorator_Interface
Expand Down
6 changes: 2 additions & 4 deletions src/Table/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,12 @@ public function getColumnWidths()
* Render the row
*
* @param array $columnWidths Width of all columns
* @param \Zend\Text\Table\Decorator\DecoratorInterface $decorator Decorator for the row borders
* @param \Zend\Text\Table\Decorator $decorator Decorator for the row borders
* @param integer $padding Padding for the columns
* @throws \Zend\Text\Table\Exception When there are too many columns
* @return string
*/
public function render(array $columnWidths,
Decorator\DecoratorInterface $decorator,
$padding = 0)
public function render(array $columnWidths, Decorator $decorator, $padding = 0)
{
// Prepare an array to store all column widths
$this->_columnWidths = array();
Expand Down
18 changes: 10 additions & 8 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
* @namespace
*/
namespace Zend\Text\Table;
use Zend\Config;

use Zend\Config,
Zend\Loader\PluginLoader;

/**
* Zend_Text_Table enables developers to create tables out of characters
*
* @uses \Zend\Loader\PluginLoader\PluginLoader
* @uses \Zend\Loader\PluginLoader
* @uses \Zend\Text\Table\Exception
* @uses \Zend\Text\Table\Column
* @uses \Zend\Text\Table\Row
Expand All @@ -50,7 +52,7 @@ class Table
/**
* Decorator used for the table borders
*
* @var \Zend\Text\Table\Decorator\DecoratorInterface
* @var \Zend\Text\Table\Decorator
*/
protected $_decorator = null;

Expand Down Expand Up @@ -92,7 +94,7 @@ class Table
/**
* Plugin loader for decorators
*
* @var string
* @var Zend\Loader\ShortNameLocater
*/
protected $_pluginLoader = null;

Expand Down Expand Up @@ -227,12 +229,12 @@ public function setAutoSeparate($autoSeparate)
/**
* Set decorator
*
* @param \Zend\Text\Table\Decorator\DecoratorInterface|string $decorator Decorator to use
* @param \Zend\Text\Table\Decorator|string $decorator Decorator to use
* @return \Zend\Text\Table\Table
*/
public function setDecorator($decorator)
{
if ($decorator instanceof Decorator\DecoratorInterface) {
if ($decorator instanceof Decorator) {
$this->_decorator = $decorator;
} else {
$classname = $this->getPluginLoader()->load($decorator);
Expand All @@ -257,14 +259,14 @@ public function setPadding($padding)
/**
* Get the plugin loader for decorators
*
* @return \Zend\Loader\PluginLoader\PluginLoader
* @return \Zend\Loader\ShortNameLocater
*/
public function getPluginLoader()
{
if ($this->_pluginLoader === null) {
$prefix = 'Zend\Text\Table\Decorator\\';
$pathPrefix = 'Zend/Text/Table/Decorator/';
$this->_pluginLoader = new \Zend\Loader\PluginLoader\PluginLoader(array($prefix => $pathPrefix));
$this->_pluginLoader = new PluginLoader(array($prefix => $pathPrefix));
}

return $this->_pluginLoader;
Expand Down
8 changes: 4 additions & 4 deletions test/FigletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function testIncorrectEncoding()
public function testNonExistentFont()
{
try {
$figlet = new Figlet\Figlet(array('font' => dirname(__FILE__) . '/Figlet/NonExistentFont.flf'));
$figlet = new Figlet\Figlet(array('font' => __DIR__ . '/Figlet/NonExistentFont.flf'));
$this->fail('An expected Zend_Text_Figlet_Exception has not been raised');
} catch (Figlet\Exception $expected) {
$this->assertContains('Font file not found', $expected->getMessage());
Expand All @@ -143,7 +143,7 @@ public function testNonExistentFont()
public function testInvalidFont()
{
try {
$figlet = new Figlet\Figlet(array('font' => dirname(__FILE__) . '/Figlet/InvalidFont.flf'));
$figlet = new Figlet\Figlet(array('font' => __DIR__ . '/Figlet/InvalidFont.flf'));
$this->fail('An expected Zend_Text_Figlet_Exception has not been raised');
} catch (Figlet\Exception $expected) {
$this->assertContains('Not a FIGlet 2 font file', $expected->getMessage());
Expand All @@ -152,7 +152,7 @@ public function testInvalidFont()

public function testGzippedFont()
{
$figlet = new Figlet\Figlet(array('font' => dirname(__FILE__) . '/Figlet/GzippedFont.gz'));
$figlet = new Figlet\Figlet(array('font' => __DIR__ . '/Figlet/GzippedFont.gz'));
$this->_equalAgainstFile($figlet->render('Dummy'), 'StandardAlignLeft.figlet');
}

Expand Down Expand Up @@ -266,7 +266,7 @@ public function testEmptyString()

protected function _equalAgainstFile($output, $file)
{
$compareString = file_get_contents(dirname(__FILE__) . '/Figlet/' . $file);
$compareString = file_get_contents(__DIR__ . '/Figlet/' . $file);

$this->assertEquals($compareString, $output);
}
Expand Down
15 changes: 15 additions & 0 deletions test/MultiByteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ public function testWordWrapCutMultiLineShortWords()
$this->assertEquals("Ä very\nlong\nwööööööö\nööööörd.", $line);
}

public function testWordWrapCutMultiLineWithPreviousNewlines()
{
$line = Zend_Text_MultiByte::wordWrap("Ä very\nlong wöööööööööööörd.", 8, "\n", false);
$this->assertEquals("Ä very\nlong\nwöööööööööööörd.", $line);
}

/**
* Long-Break tests
*/
public function testWordWrapLongBreak()
{
$line = Zend_Text_MultiByte::wordWrap("Ä very<br>long wöö<br>öööööööö<br>öörd.", 8, '<br>', false);
$this->assertEquals("Ä very<br>long<br>wöö<br>öööööööö<br>öörd.", $line);
}

/**
* Alternative cut tests
*/
Expand Down

0 comments on commit f1a4a26

Please sign in to comment.