Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FormatterHelper.php

class FormatterHelper

The Formatter class provides helpers to format messages.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\Console\Helper\Helper implements \Symfony\Component\Console\Helper\HelperInterface
    • class \Symfony\Component\Console\Helper\FormatterHelper extends \Symfony\Component\Console\Helper\Helper

Expanded class hierarchy of FormatterHelper

2 files declare their use of FormatterHelper
Application.php in vendor/symfony/console/Application.php
InitCommand.php in vendor/composer/composer/src/Composer/Command/InitCommand.php

File

vendor/symfony/console/Helper/FormatterHelper.php, line 21

Namespace

Symfony\Component\Console\Helper
View source
class FormatterHelper extends Helper {
    
    /**
     * Formats a message within a section.
     */
    public function formatSection(string $section, string $message, string $style = 'info') : string {
        return \sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
    }
    
    /**
     * Formats a message as a block of text.
     */
    public function formatBlock(string|array $messages, string $style, bool $large = false) : string {
        if (!\is_array($messages)) {
            $messages = [
                $messages,
            ];
        }
        $len = 0;
        $lines = [];
        foreach ($messages as $message) {
            $message = OutputFormatter::escape($message);
            $lines[] = \sprintf($large ? '  %s  ' : ' %s ', $message);
            $len = max(self::width($message) + ($large ? 4 : 2), $len);
        }
        $messages = $large ? [
            str_repeat(' ', $len),
        ] : [];
        for ($i = 0; isset($lines[$i]); ++$i) {
            $messages[] = $lines[$i] . str_repeat(' ', $len - self::width($lines[$i]));
        }
        if ($large) {
            $messages[] = str_repeat(' ', $len);
        }
        for ($i = 0; isset($messages[$i]); ++$i) {
            $messages[$i] = \sprintf('<%s>%s</%s>', $style, $messages[$i], $style);
        }
        return implode("\n", $messages);
    }
    
    /**
     * Truncates a message to the given length.
     */
    public function truncate(string $message, int $length, string $suffix = '...') : string {
        $computedLength = $length - self::width($suffix);
        if ($computedLength > self::width($message)) {
            return $message;
        }
        return self::substr($message, 0, $length) . $suffix;
    }
    public function getName() : string {
        return 'formatter';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FormatterHelper::formatBlock public function Formats a message as a block of text.
FormatterHelper::formatSection public function Formats a message within a section.
FormatterHelper::getName public function Returns the canonical name of this helper. Overrides HelperInterface::getName
FormatterHelper::truncate public function Truncates a message to the given length.
Helper::$helperSet protected property
Helper::formatMemory public static function
Helper::formatTime public static function
Helper::getHelperSet public function Gets the helper set associated with this helper. Overrides HelperInterface::getHelperSet
Helper::length public static function Returns the length of a string, using mb_strlen if it is available.
The length is related to how many bytes the string will use.
Helper::removeDecoration public static function
Helper::setHelperSet public function Sets the helper set associated with this helper. Overrides HelperInterface::setHelperSet
Helper::substr public static function Returns the subset of a string, using mb_substr if it is available.
Helper::width public static function Returns the width of a string, using mb_strwidth if it is available.
The width is how many characters positions the string will use.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal