1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Console\Helper;
13
14use Symfony\Component\Console\Formatter\OutputFormatter;
15
16/**
17 * The Formatter class provides helpers to format messages.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 */
21class FormatterHelper extends Helper
22{
23    /**
24     * Formats a message within a section.
25     *
26     * @param string $section The section name
27     * @param string $message The message
28     * @param string $style   The style to apply to the section
29     *
30     * @return string The format section
31     */
32    public function formatSection($section, $message, $style = 'info')
33    {
34        return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
35    }
36
37    /**
38     * Formats a message as a block of text.
39     *
40     * @param string|array $messages The message to write in the block
41     * @param string       $style    The style to apply to the whole block
42     * @param bool         $large    Whether to return a large block
43     *
44     * @return string The formatter message
45     */
46    public function formatBlock($messages, $style, $large = false)
47    {
48        $messages = (array) $messages;
49
50        $len = 0;
51        $lines = array();
52        foreach ($messages as $message) {
53            $message = OutputFormatter::escape($message);
54            $lines[] = sprintf($large ? '  %s  ' : ' %s ', $message);
55            $len = max($this->strlen($message) + ($large ? 4 : 2), $len);
56        }
57
58        $messages = $large ? array(str_repeat(' ', $len)) : array();
59        foreach ($lines as $line) {
60            $messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
61        }
62        if ($large) {
63            $messages[] = str_repeat(' ', $len);
64        }
65
66        foreach ($messages as &$message) {
67            $message = sprintf('<%s>%s</%s>', $style, $message, $style);
68        }
69
70        return implode("\n", $messages);
71    }
72
73    /**
74     * {@inheritdoc}
75     */
76    public function getName()
77    {
78        return 'formatter';
79    }
80}
81