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

Commit

Permalink
Merge pull request zendframework/zendframework#4606 from neilime/develop
Browse files Browse the repository at this point in the history
Supports the encoding of the console and encodes the text to display if needed
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ abstract class AbstractAdapter implements AdapterInterface
*/
public function write($text, $color = null, $bgColor = null)
{
if ($color !== null || $bgColor !== null) {

//Encode text to match console encoding
$text = $this->encodeText($text);

if ($color !== null || $bgColor !== null) {
echo $this->colorize($text, $color, $bgColor);
} else {
echo $text;
Expand Down Expand Up @@ -497,4 +501,27 @@ public function readChar($mask = null)
fclose($f);
return $char;
}

/**
* Encode a text to match console encoding
*
* @param string $text
* @return string the encoding text
*/
public function encodeText($text)
{
if ($this->isUtf8()) {
if (StringUtils::isValidUtf8($text)) {
return $text;
}

return utf8_encode($text);
}

if (StringUtils::isValidUtf8($text)) {
return utf8_decode($text);
}

return $text;
}
}
26 changes: 25 additions & 1 deletion test/Adapter/AbstractAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Console\Adapater;
namespace ZendTest\Console\Adapter;

use ZendTest\Console\TestAssets\ConsoleAdapter;

Expand Down Expand Up @@ -120,4 +120,28 @@ public function testReadCharWithMaskInsensitiveCase()
$char = $this->adapter->readChar('ar');
$this->assertEquals($char, 'r');
}

public function testEncodeText()
{
//Utf8 string
$text = '\u00E9\u00E9\u00E9';

//Console UTF8 - Text utf8
$this->adapter->setTestUtf8(true);
$encodedText = $this->adapter->encodeText($text);
$this->assertEquals($text,$encodedText);

//Console UTF8 - Text not utf8
$encodedText = $this->adapter->encodeText(utf8_decode($text));
$this->assertEquals($text,$encodedText);

//Console not UTF8 - Text utf8
$this->adapter->setTestUtf8(false);
$encodedText = $this->adapter->encodeText($text);
$this->assertEquals(utf8_decode($text),$encodedText);

//Console not UTF8 - Text not utf8
$encodedText = $this->adapter->encodeText(utf8_decode($text));
$this->assertEquals(utf8_decode($text),$encodedText);
}
}

0 comments on commit cb2e475

Please sign in to comment.