/** * 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; }
/** * Render a FIGlet text * * @param string $text Text to convert to a figlet text * @param string $encoding Encoding of the input string * @throws Exception\InvalidArgumentException When $text is not a string * @throws Exception\UnexpectedValueException When $text it not properly encoded * @return string */ public function render($text, $encoding = 'UTF-8') { if (!is_string($text)) { throw new Exception\InvalidArgumentException('$text must be a string'); } // Get the string wrapper supporting UTF-8 character encoding and the input encoding $strWrapper = StringUtils::getWrapper($encoding, 'UTF-8'); // Convert $text to UTF-8 and check encoding $text = $strWrapper->convert($text); if (!StringUtils::isValidUtf8($text)) { throw new Exception\UnexpectedValueException('$text is not encoded with ' . $encoding); } $strWrapper = StringUtils::getWrapper('UTF-8'); $this->output = ''; $this->outputLine = array(); $this->_clearLine(); $this->outlineLengthLimit = $this->outputWidth - 1; $this->inCharLineLengthLimit = $this->outputWidth * 4 + 100; $wordBreakMode = 0; $lastCharWasEol = false; $textLength = $strWrapper->strlen($text); for ($charNum = 0; $charNum < $textLength; $charNum++) { // Handle paragraphs $char = $strWrapper->substr($text, $charNum, 1); if ($char === "\n" && $this->handleParagraphs && !$lastCharWasEol) { $nextChar = $strWrapper->substr($text, $charNum + 1, 1); if (!$nextChar) { $nextChar = null; } $char = ctype_space($nextChar) ? "\n" : ' '; } $lastCharWasEol = ctype_space($char) && $char !== "\t" && $char !== ' '; if (ctype_space($char)) { $char = $char === "\t" || $char === ' ' ? ' ' : "\n"; } // Skip unprintable characters $ordChar = $this->_uniOrd($char); if ($ordChar > 0 && $ordChar < 32 && $char !== "\n" || $ordChar === 127) { continue; } // Build the character // Note: The following code is complex and thoroughly tested. // Be careful when modifying! do { $charNotAdded = false; if ($wordBreakMode === -1) { if ($char === ' ') { break; } elseif ($char === "\n") { $wordBreakMode = 0; break; } $wordBreakMode = 0; } if ($char === "\n") { $this->_appendLine(); $wordBreakMode = false; } elseif ($this->_addChar($char)) { if ($char !== ' ') { $wordBreakMode = $wordBreakMode >= 2 ? 3 : 1; } else { $wordBreakMode = $wordBreakMode > 0 ? 2 : 0; } } elseif ($this->outlineLength === 0) { for ($i = 0; $i < $this->charHeight; $i++) { if ($this->rightToLeft === 1 && $this->outputWidth > 1) { $offset = strlen($this->currentChar[$i]) - $this->outlineLengthLimit; $this->_putString(substr($this->currentChar[$i], $offset)); } else { $this->_putString($this->currentChar[$i]); } } $wordBreakMode = -1; } elseif ($char === ' ') { if ($wordBreakMode === 2) { $this->_splitLine(); } else { $this->_appendLine(); } $wordBreakMode = -1; } else { if ($wordBreakMode >= 2) { $this->_splitLine(); } else { $this->_appendLine(); } $wordBreakMode = $wordBreakMode === 3 ? 1 : 0; $charNotAdded = true; } } while ($charNotAdded); } if ($this->outlineLength !== 0) { $this->_appendLine(); } return $this->output; }
/** * @dataProvider getUtf8StringValidity * @param string $str * @param boolean $valid */ public function testIsValidUtf8($str, $valid) { $this->assertSame($valid, StringUtils::isValidUtf8($str)); }
/** * @param string $subject * * @return string */ protected static function ensureUtf8Encoded($subject) { if (!StringUtils::isValidUtf8($subject)) { $subject = utf8_encode($subject); } return $subject; }