You are here

abstract class Helper in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Helper/Helper.php \Symfony\Component\Console\Helper\Helper

Helper is the base class for all helper classes.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of Helper

2 files declare their use of Helper
ProgressBarTest.php in vendor/symfony/console/Tests/Helper/ProgressBarTest.php
SymfonyStyle.php in vendor/symfony/console/Style/SymfonyStyle.php

File

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

Namespace

Symfony\Component\Console\Helper
View source
abstract class Helper implements HelperInterface {
  protected $helperSet = null;

  /**
   * Sets the helper set associated with this helper.
   *
   * @param HelperSet $helperSet A HelperSet instance
   */
  public function setHelperSet(HelperSet $helperSet = null) {
    $this->helperSet = $helperSet;
  }

  /**
   * Gets the helper set associated with this helper.
   *
   * @return HelperSet A HelperSet instance
   */
  public function getHelperSet() {
    return $this->helperSet;
  }

  /**
   * Returns the length of a string, using mb_strwidth if it is available.
   *
   * @param string $string The string to check its length
   *
   * @return int The length of the string
   */
  public static function strlen($string) {
    if (!function_exists('mb_strwidth')) {
      return strlen($string);
    }
    if (false === ($encoding = mb_detect_encoding($string))) {
      return strlen($string);
    }
    return mb_strwidth($string, $encoding);
  }
  public static function formatTime($secs) {
    static $timeFormats = array(
      array(
        0,
        '< 1 sec',
      ),
      array(
        2,
        '1 sec',
      ),
      array(
        59,
        'secs',
        1,
      ),
      array(
        60,
        '1 min',
      ),
      array(
        3600,
        'mins',
        60,
      ),
      array(
        5400,
        '1 hr',
      ),
      array(
        86400,
        'hrs',
        3600,
      ),
      array(
        129600,
        '1 day',
      ),
      array(
        604800,
        'days',
        86400,
      ),
    );
    foreach ($timeFormats as $format) {
      if ($secs >= $format[0]) {
        continue;
      }
      if (2 == count($format)) {
        return $format[1];
      }
      return ceil($secs / $format[2]) . ' ' . $format[1];
    }
  }
  public static function formatMemory($memory) {
    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 strlenWithoutDecoration(OutputFormatterInterface $formatter, $string) {
    $isDecorated = $formatter
      ->isDecorated();
    $formatter
      ->setDecorated(false);

    // remove <...> formatting
    $string = $formatter
      ->format($string);

    // remove already formatted characters
    $string = preg_replace("/\33\\[[^m]*m/", '', $string);
    $formatter
      ->setDecorated($isDecorated);
    return self::strlen($string);
  }

}

Members

Namesort descending Modifiers Type Description 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::setHelperSet public function Sets the helper set associated with this helper. Overrides HelperInterface::setHelperSet
Helper::strlen public static function Returns the length of a string, using mb_strwidth if it is available.
Helper::strlenWithoutDecoration public static function
HelperInterface::getName public function Returns the canonical name of this helper. 8