/** * Defined by Zend_ProgressBar_Adapter_Interface * * @param float $current Current progress value * @param float $max Max progress value * @param float $percent Current percent value * @param integer $timeTaken Taken time in seconds * @param integer $timeRemaining Remaining time in seconds * @param string $text Status text * @return void */ public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text) { // See if we must clear the line if ($this->_outputStarted) { $data = str_repeat("", $this->_width); } else { $data = ''; $this->_outputStarted = true; } // Build all elements $renderedElements = array(); foreach ($this->_elements as $element) { switch ($element) { case self::ELEMENT_BAR: $visualWidth = $this->_barWidth - 2; $bar = '['; $indicatorWidth = strlen($this->_barIndicatorChar); $doneWidth = min($visualWidth - $indicatorWidth, round($visualWidth * $percent)); if ($doneWidth > 0) { $bar .= substr(str_repeat($this->_barLeftChar, ceil($doneWidth / strlen($this->_barLeftChar))), 0, $doneWidth); } $bar .= $this->_barIndicatorChar; $leftWidth = $visualWidth - $doneWidth - $indicatorWidth; if ($leftWidth > 0) { $bar .= substr(str_repeat($this->_barRightChar, ceil($leftWidth / strlen($this->_barRightChar))), 0, $leftWidth); } $bar .= ']'; $renderedElements[] = $bar; break; case self::ELEMENT_PERCENT: $renderedElements[] = str_pad(round($percent * 100), 3, ' ', STR_PAD_LEFT) . '%'; break; case self::ELEMENT_ETA: // In the first 5 seconds we don't get accurate results, // this skipping technique is found in many progressbar // implementations. if ($timeTaken < 5) { $renderedElements[] = str_repeat(' ', 12); break; } if ($timeRemaining === null || $timeRemaining > 86400) { $etaFormatted = '??:??:??'; } else { $hours = floor($timeRemaining / 3600); $minutes = floor($timeRemaining % 3600 / 60); $seconds = $timeRemaining % 3600 % 60; $etaFormatted = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); } $renderedElements[] = 'ETA ' . $etaFormatted; break; case self::ELEMENT_TEXT: $renderedElements[] = \Zend\Text\MultiByte::strPad(substr($text, 0, $this->_textWidth), $this->_textWidth, ' ', STR_PAD_RIGHT, $this->_charset); break; } } $data .= implode(' ', $renderedElements); // Output line data $this->_outputData($data); }
public function testRightPad() { $text = Text\MultiByte::strPad('äääöö', 5, 'ö', STR_PAD_RIGHT); $this->assertEquals('äääöö', $text); }
public function testStrPadTriggersDeprecatedError() { $this->setExpectedException('PHPUnit_Framework_Error_Deprecated'); $text = Text\MultiByte::strPad('äääöö', 2, 'ö'); }
/** * Render the column width the given column width * * @param integer $columnWidth The width of the column * @param integer $padding The padding for the column * @throws \Zend\Text\Table\Exception When $columnWidth is lower than 1 * @throws \Zend\Text\Table\Exception When padding is greater than columnWidth * @return string */ public function render($columnWidth, $padding = 0) { if (is_int($columnWidth) === false or $columnWidth < 1) { throw new Exception('$columnWidth must be an integer and greater than 0'); } $columnWidth -= $padding * 2; if ($columnWidth < 1) { throw new Exception('Padding (' . $padding . ') is greater than column width'); } switch ($this->_align) { case self::ALIGN_LEFT: $padMode = STR_PAD_RIGHT; break; case self::ALIGN_CENTER: $padMode = STR_PAD_BOTH; break; case self::ALIGN_RIGHT: $padMode = STR_PAD_LEFT; break; default: // This can never happen, but the CS tells I have to have it ... break; } $outputCharset = Table::getOutputCharset(); $lines = explode("\n", Text\MultiByte::wordWrap($this->_content, $columnWidth, "\n", true, $outputCharset)); $paddedLines = array(); foreach ($lines as $line) { $paddedLines[] = str_repeat(' ', $padding) . Text\MultiByte::strPad($line, $columnWidth, ' ', $padMode, $outputCharset) . str_repeat(' ', $padding); } $result = implode("\n", $paddedLines); return $result; }
/** * @group ZF-12186 */ public function testPadNegativePadLength() { $text = Text\MultiByte::strPad('äääöö', -2, 'ö'); $this->assertEquals('äääöö', $text); }