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://github.com/zendframework/zf2 into cach…
Browse files Browse the repository at this point in the history
…e_internals
  • Loading branch information
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 70 deletions.
34 changes: 34 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Text
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Text\Exception;

use Zend\Text\Exception;

/**
* @category Zend
* @package Zend_Text
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException extends \InvalidArgumentException implements Exception
{}
125 changes: 58 additions & 67 deletions src/MultiByte.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Zend\Text;

/**
* Zend_Text_MultiByte contains multibyte safe string methods
* Contains multibyte safe string methods
*
* @category Zend
* @package Zend_Text
Expand All @@ -43,78 +43,69 @@ 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);
$stringWidth = iconv_strlen($string, $charset);
$breakWidth = iconv_strlen($break, $charset);

while (($stringLength = iconv_strlen($string, $charset)) > 0) {
$breakPos = iconv_strpos($string, $break, 0, $charset);
if (strlen($string) === 0) {
return '';
}

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 {
$subString = iconv_substr($string, 0, $width, $charset);
if ($breakWidth === null) {
throw new Exception\InvalidArgumentException('Break string cannot be empty');
}

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;
}
}
}
if ($width === 0 && $cut) {
throw new Exception\InvalidArgumentException('Cannot force cut when width is zero');
}

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

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

$possibleBreak = $char;
if ($breakWidth !== 1) {
$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;
continue;
}

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

$lastSpace = $current;
continue;
}

if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
$result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
$lastStart = $lastSpace = $current;
continue;
}

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

return implode($break, $result);

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

return $result;
}

/**
Expand All @@ -127,7 +118,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 src/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 test/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', "Cannot force cut when width is zero");
Text\MultiByte::wordWrap('a', 0, "\n", true);
}
}
11 changes: 11 additions & 0 deletions test/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 6c67c50

Please sign in to comment.