Skip to content

Commit

Permalink
Merge branch 'hotfix/text' of https://github.com/sasezaki/zf2 into ho…
Browse files Browse the repository at this point in the history
…tfix/zf-11792
  • Loading branch information
weierophinney committed Mar 23, 2012
2 parents 3340158 + 2c17b50 commit 9034798
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 71 deletions.
10 changes: 10 additions & 0 deletions library/Zend/Text/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Zend\Text\Exception;

use Zend\Text\Exception;

class InvalidArgumentException
extends \InvalidArgumentException
implements Exception
{}
115 changes: 47 additions & 68 deletions library/Zend/Text/MultiByte.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @namespace
*/
namespace Zend\Text;
use Zend\Text\Exception;

/**
* Zend_Text_MultiByte contains multibyte safe string methods
Expand All @@ -43,78 +44,56 @@ class MultiByte
* @param string $charset
* @return string
*/
public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8')
public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8')
{
$result = array();
$breakWidth = iconv_strlen($break, $charset);

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

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

if (strlen($string) === 0) {
return '';
} elseif ($breakWidth === null) {
throw new Exception\InvalidArgumentException('Break string cannot be empty');
} elseif ($width === 0 && $cut) {
throw new Exception\InvalidArgumentException('Can\'t force cut when width is zero');
}

$result = '';
$lastStart = $lastSpace = 0;

for ($current = 0; $current < $stringWidth; $current++) {
$char = iconv_substr($string, $current, 1, $charset);

if ($breakWidth === 1) {
$possibleBreak = $char;
} else {
$subString = iconv_substr($string, 0, $width, $charset);

if ($subString === $string) {
$cutLength = null;
} else {
$nextChar = iconv_substr($string, $width, 1, $charset);

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

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

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

$cutLength = iconv_strlen($subString, $charset) + 1;
} else {
$spacePos = iconv_strrpos($subString, ' ', $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 ($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;
}
}
}
$possibleBreak = iconv_substr($string, $current, $breakWidth, $charset);
}

$result[] = $subString;

if ($cutLength !== null) {
$string = iconv_substr($string, $cutLength, ($stringLength - $cutLength), $charset);
} else {
break;

if ($possibleBreak === $break) {
$result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset);
$current += $breakWidth - 1;
$lastStart = $lastSpace = $current + 1;
} elseif ($char === ' ') {
if ($current - $lastStart >= $width) {
$result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
$lastStart = $current + 1;
}

$lastSpace = $current;
} elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
$result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
$lastStart = $lastSpace = $current;
} elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) {
$result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break;
$lastStart = $lastSpace = $lastSpace + 1;
}
}

return implode($break, $result);

if ($lastStart !== $current) {
$result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset);
}

return $result;
}

/**
Expand All @@ -127,7 +106,7 @@ public static function wordWrap($string, $width = 75, $break = "\n", $cut = fals
* @param string $charset
* @return string
*/
public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'UTF-8')
public static function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT, $charset = 'utf-8')
{
$return = '';
$lengthOfPadding = $padLength - iconv_strlen($input, $charset);
Expand Down
4 changes: 2 additions & 2 deletions library/Zend/Text/Table/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ public function render(array $columnWidths, Decorator $decorator, $padding = 0)
for ($line = 0; $line < $maxHeight; $line++) {
$result .= $decorator->getVertical();

foreach ($renderedColumns as $renderedColumn) {
foreach ($renderedColumns as $index => $renderedColumn) {
if (isset($renderedColumn[$line]) === true) {
$result .= $renderedColumn[$line];
} else {
$result .= str_repeat(' ', strlen($renderedColumn[0]));
$result .= str_repeat(' ', $this->_columnWidths[$index]);
}

$result .= $decorator->getVertical();
Expand Down
26 changes: 25 additions & 1 deletion tests/Zend/Text/MultiByteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testWordWrapCutMultiLineWithPreviousNewlines()
public function testWordWrapLongBreak()
{
$line = Text\MultiByte::wordWrap("Ä very<br>long wöö<br>öööööööö<br>öörd.", 8, '<br>', false);
$this->assertEquals("Ä very<br>long<br>wöö<br>öööööööö<br>öörd.", $line);
$this->assertEquals("Ä very<br>long wöö<br>öööööööö<br>öörd.", $line);
}

/**
Expand All @@ -86,12 +86,30 @@ public function testWordWrapCutEndingSingleSpace()
$this->assertEquals('äüö äöü ', $line);
}

public function testWordWrapCutEndingSingleSpaceWithNonSpaceDivider()
{
$line = Text\MultiByte::wordWrap('äöüäöü ', 3, '-', true);
$this->assertEquals('äöü-äöü-', $line);
}

public function testWordWrapCutEndingTwoSpaces()
{
$line = Text\MultiByte::wordWrap('äüöäöü ', 3, ' ', true);
$this->assertEquals('äüö äöü ', $line);
}

public function testWordWrapNoCutEndingSingleSpace()
{
$line = Text\Multibyte::wordWrap('12345 ', 5, '-', false);
$this->assertEquals('12345-', $line);
}

public function testWordWrapNoCutEndingTwoSpaces()
{
$line = Text\MultiByte::wordWrap('12345 ', 5, '-', false);
$this->assertEquals('12345- ', $line);
}

public function testWordWrapCutEndingThreeSpaces()
{
$line = Text\MultiByte::wordWrap('äüöäöü ', 3, ' ', true);
Expand Down Expand Up @@ -229,4 +247,10 @@ public function testRightPad()
$text = Text\MultiByte::strPad('äääöö', 5, 'ö', STR_PAD_RIGHT);
$this->assertEquals('äääöö', $text);
}

public function testWordWrapInvalidArgument()
{
$this->setExpectedException('Zend\Text\Exception\InvalidArgumentException', "Can't force cut when width is zero");
Text\MultiByte::wordWrap('a', 0, "\n", true);
}
}
11 changes: 11 additions & 0 deletions tests/Zend/Text/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ public function testRowMultiLine()
$this->assertEquals($row->render(array(10, 10), $decorator), "│foo │foobar │\n│bar │ │\n");
}

public function testUnicodeRowMultiLine()
{
$decorator = new Decorator\Unicode();

$row = new Table\Row();
$row->appendColumn(new Table\Column("föö\nbär"));
$row->appendColumn(new Table\Column("fööbär"));

$this->assertEquals($row->render(array(3, 10), $decorator), "│föö│fööbär │\n│bär│ │\n");
}

public function testTableConstructInvalidColumnWidthsItem()
{
$this->setExpectedException('Zend\Text\Table\Exception\InvalidArgumentException', 'invalid column width');
Expand Down

0 comments on commit 9034798

Please sign in to comment.