This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Helper/FormatterHelper.php

98 lines
2.6 KiB
PHP
Raw Normal View History

2010-01-04 14:42:28 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-01-04 14:42:28 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-01-04 14:42:28 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2010-01-04 14:42:28 +00:00
*/
namespace Symfony\Component\Console\Helper;
2010-01-04 14:42:28 +00:00
/**
* The Formatter class provides helpers to format messages.
*
* @author Fabien Potencier <fabien@symfony.com>
2010-01-04 14:42:28 +00:00
*/
class FormatterHelper extends Helper
2010-01-04 14:42:28 +00:00
{
/**
* Formats a message within a section.
*
* @param string $section The section name
* @param string $message The message
* @param string $style The style to apply to the section
*/
public function formatSection($section, $message, $style = 'info')
2010-01-04 14:42:28 +00:00
{
return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
2010-01-04 14:42:28 +00:00
}
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
* @param string $style The style to apply to the whole block
* @param Boolean $large Whether to return a large block
*
* @return string The formatter message
*/
public function formatBlock($messages, $style, $large = false)
2010-01-04 14:42:28 +00:00
{
$messages = (array) $messages;
2010-01-04 14:42:28 +00:00
$len = 0;
$lines = array();
foreach ($messages as $message) {
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
$len = max($this->strlen($message) + ($large ? 4 : 2), $len);
}
$messages = $large ? array(str_repeat(' ', $len)) : array();
foreach ($lines as $line) {
$messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
}
if ($large) {
$messages[] = str_repeat(' ', $len);
}
foreach ($messages as &$message) {
$message = sprintf('<%s>%s</%s>', $style, $message, $style);
}
return implode("\n", $messages);
2010-01-04 14:42:28 +00:00
}
2011-02-07 00:34:24 +00:00
/**
* Returns the length of a string, uses mb_strlen if it is available.
*
* @param string $string The string to check its length
*
* @return integer The length of the string
*/
private function strlen($string)
2010-01-04 14:42:28 +00:00
{
if (!function_exists('mb_strlen')) {
return strlen($string);
}
if (false === $encoding = mb_detect_encoding($string)) {
return strlen($string);
}
return mb_strlen($string, $encoding);
2010-01-04 14:42:28 +00:00
}
/**
* Returns the helper's canonical name
2011-02-07 00:34:24 +00:00
*
* @return string The canonical name of the helper
*/
public function getName()
2010-01-04 14:42:28 +00:00
{
return 'formatter';
2010-01-04 14:42:28 +00:00
}
2010-01-11 13:46:11 +00:00
}