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

Breadcrumb

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

class Helper

Helper is the base class for all helper classes.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

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

Expanded class hierarchy of Helper

6 files declare their use of Helper
Application.php in vendor/symfony/console/Application.php
ConsoleSectionOutput.php in vendor/symfony/console/Output/ConsoleSectionOutput.php
MarkdownDescriptor.php in vendor/symfony/console/Descriptor/MarkdownDescriptor.php
ReStructuredTextDescriptor.php in vendor/symfony/console/Descriptor/ReStructuredTextDescriptor.php
SymfonyStyle.php in vendor/symfony/console/Style/SymfonyStyle.php

... See full list

2 string references to 'Helper'
ManyToOne::ensureMyTable in core/modules/views/src/Plugin/views/filter/ManyToOne.php
Ensures that the main table for this handler is in the query.
ManyToOne::operators in core/modules/views/src/Plugin/views/filter/ManyToOne.php
Returns an array of operator information, keyed by operator ID.

File

vendor/symfony/console/Helper/Helper.php, line 22

Namespace

Symfony\Component\Console\Helper
View source
abstract class Helper implements HelperInterface {
    protected ?HelperSet $helperSet = null;
    public function setHelperSet(?HelperSet $helperSet) : void {
        $this->helperSet = $helperSet;
    }
    public function getHelperSet() : ?HelperSet {
        return $this->helperSet;
    }
    
    /**
     * Returns the width of a string, using mb_strwidth if it is available.
     * The width is how many characters positions the string will use.
     */
    public static function width(?string $string) : int {
        $string ??= '';
        if (preg_match('//u', $string)) {
            return (new UnicodeString($string))->width(false);
        }
        if (false === ($encoding = mb_detect_encoding($string, null, true))) {
            return \strlen($string);
        }
        return mb_strwidth($string, $encoding);
    }
    
    /**
     * 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.
     */
    public static function length(?string $string) : int {
        $string ??= '';
        if (preg_match('//u', $string)) {
            return (new UnicodeString($string))->length();
        }
        if (false === ($encoding = mb_detect_encoding($string, null, true))) {
            return \strlen($string);
        }
        return mb_strlen($string, $encoding);
    }
    
    /**
     * Returns the subset of a string, using mb_substr if it is available.
     */
    public static function substr(?string $string, int $from, ?int $length = null) : string {
        $string ??= '';
        if (false === ($encoding = mb_detect_encoding($string, null, true))) {
            return substr($string, $from, $length);
        }
        return mb_substr($string, $from, $length, $encoding);
    }
    public static function formatTime(int|float $secs, int $precision = 1) : string {
        $secs = (int) floor($secs);
        if (0 === $secs) {
            return '< 1 sec';
        }
        static $timeFormats = [
            [
                1,
                '1 sec',
                'secs',
            ],
            [
                60,
                '1 min',
                'mins',
            ],
            [
                3600,
                '1 hr',
                'hrs',
            ],
            [
                86400,
                '1 day',
                'days',
            ],
        ];
        $times = [];
        foreach ($timeFormats as $index => $format) {
            $seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
            if (isset($times[$index - $precision])) {
                unset($times[$index - $precision]);
            }
            if (0 === $seconds) {
                continue;
            }
            $unitCount = $seconds / $format[0];
            $times[$index] = 1 === $unitCount ? $format[1] : $unitCount . ' ' . $format[2];
            if ($secs === $seconds) {
                break;
            }
            $secs -= $seconds;
        }
        return implode(', ', array_reverse($times));
    }
    public static function formatMemory(int $memory) : string {
        if ($memory >= 1024 * 1024 * 1024) {
            return \sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024);
        }
        if ($memory >= 1024 * 1024) {
            return \sprintf('%.1f MiB', $memory / 1024 / 1024);
        }
        if ($memory >= 1024) {
            return \sprintf('%d KiB', $memory / 1024);
        }
        return \sprintf('%d B', $memory);
    }
    public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) : string {
        $isDecorated = $formatter->isDecorated();
        $formatter->setDecorated(false);
        // remove <...> formatting
        $string = $formatter->format($string ?? '');
        // remove already formatted characters
        $string = preg_replace("/\x1b\\[[^m]*m/", '', $string ?? '');
        // remove terminal hyperlinks
        $string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
        $formatter->setDecorated($isDecorated);
        return $string;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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.
HelperInterface::getName public function Returns the canonical name of this helper. 5

API Navigation

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