You are here

class OutputFormatterStyleStack in Zircon Profile 8

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

@author Jean-François Simon <contact@jfsimon.fr>

Hierarchy

Expanded class hierarchy of OutputFormatterStyleStack

1 file declares its use of OutputFormatterStyleStack
OutputFormatterStyleStackTest.php in vendor/symfony/console/Tests/Formatter/OutputFormatterStyleStackTest.php

File

vendor/symfony/console/Formatter/OutputFormatterStyleStack.php, line 17

Namespace

Symfony\Component\Console\Formatter
View source
class OutputFormatterStyleStack {

  /**
   * @var OutputFormatterStyleInterface[]
   */
  private $styles;

  /**
   * @var OutputFormatterStyleInterface
   */
  private $emptyStyle;

  /**
   * Constructor.
   *
   * @param OutputFormatterStyleInterface|null $emptyStyle
   */
  public function __construct(OutputFormatterStyleInterface $emptyStyle = null) {
    $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
    $this
      ->reset();
  }

  /**
   * Resets stack (ie. empty internal arrays).
   */
  public function reset() {
    $this->styles = array();
  }

  /**
   * Pushes a style in the stack.
   *
   * @param OutputFormatterStyleInterface $style
   */
  public function push(OutputFormatterStyleInterface $style) {
    $this->styles[] = $style;
  }

  /**
   * Pops a style from the stack.
   *
   * @param OutputFormatterStyleInterface|null $style
   *
   * @return OutputFormatterStyleInterface
   *
   * @throws \InvalidArgumentException When style tags incorrectly nested
   */
  public function pop(OutputFormatterStyleInterface $style = null) {
    if (empty($this->styles)) {
      return $this->emptyStyle;
    }
    if (null === $style) {
      return array_pop($this->styles);
    }
    foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
      if ($style
        ->apply('') === $stackedStyle
        ->apply('')) {
        $this->styles = array_slice($this->styles, 0, $index);
        return $stackedStyle;
      }
    }
    throw new \InvalidArgumentException('Incorrectly nested style tag found.');
  }

  /**
   * Computes current style with stacks top codes.
   *
   * @return OutputFormatterStyle
   */
  public function getCurrent() {
    if (empty($this->styles)) {
      return $this->emptyStyle;
    }
    return $this->styles[count($this->styles) - 1];
  }

  /**
   * @param OutputFormatterStyleInterface $emptyStyle
   *
   * @return OutputFormatterStyleStack
   */
  public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle) {
    $this->emptyStyle = $emptyStyle;
    return $this;
  }

  /**
   * @return OutputFormatterStyleInterface
   */
  public function getEmptyStyle() {
    return $this->emptyStyle;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OutputFormatterStyleStack::$emptyStyle private property
OutputFormatterStyleStack::$styles private property
OutputFormatterStyleStack::getCurrent public function Computes current style with stacks top codes.
OutputFormatterStyleStack::getEmptyStyle public function
OutputFormatterStyleStack::pop public function Pops a style from the stack.
OutputFormatterStyleStack::push public function Pushes a style in the stack.
OutputFormatterStyleStack::reset public function Resets stack (ie. empty internal arrays).
OutputFormatterStyleStack::setEmptyStyle public function
OutputFormatterStyleStack::__construct public function Constructor.