TYPO3  7.6
OutputFormatterStyleStack.php
Go to the documentation of this file.
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 
12 namespace Symfony\Component\Console\Formatter;
13 
18 {
22  private $styles;
23 
27  private $emptyStyle;
28 
35  {
36  $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
37  $this->reset();
38  }
39 
43  public function reset()
44  {
45  $this->styles = array();
46  }
47 
53  public function push(OutputFormatterStyleInterface $style)
54  {
55  $this->styles[] = $style;
56  }
57 
67  public function pop(OutputFormatterStyleInterface $style = null)
68  {
69  if (empty($this->styles)) {
70  return $this->emptyStyle;
71  }
72 
73  if (null === $style) {
74  return array_pop($this->styles);
75  }
76 
77  foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
78  if ($style->apply('') === $stackedStyle->apply('')) {
79  $this->styles = array_slice($this->styles, 0, $index);
80 
81  return $stackedStyle;
82  }
83  }
84 
85  throw new \InvalidArgumentException('Incorrectly nested style tag found.');
86  }
87 
93  public function getCurrent()
94  {
95  if (empty($this->styles)) {
96  return $this->emptyStyle;
97  }
98 
99  return $this->styles[count($this->styles) - 1];
100  }
101 
108  {
109  $this->emptyStyle = $emptyStyle;
110 
111  return $this;
112  }
113 
117  public function getEmptyStyle()
118  {
119  return $this->emptyStyle;
120  }
121 }