bf0beb709ed1ce772b7d793483887cbb807b6260
[yaffs-website] / vendor / symfony / console / Formatter / OutputFormatterStyleStack.php
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
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15
16 /**
17  * @author Jean-François Simon <contact@jfsimon.fr>
18  */
19 class OutputFormatterStyleStack
20 {
21     /**
22      * @var OutputFormatterStyleInterface[]
23      */
24     private $styles;
25
26     private $emptyStyle;
27
28     public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
29     {
30         $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
31         $this->reset();
32     }
33
34     /**
35      * Resets stack (ie. empty internal arrays).
36      */
37     public function reset()
38     {
39         $this->styles = array();
40     }
41
42     /**
43      * Pushes a style in the stack.
44      */
45     public function push(OutputFormatterStyleInterface $style)
46     {
47         $this->styles[] = $style;
48     }
49
50     /**
51      * Pops a style from the stack.
52      *
53      * @return OutputFormatterStyleInterface
54      *
55      * @throws InvalidArgumentException When style tags incorrectly nested
56      */
57     public function pop(OutputFormatterStyleInterface $style = null)
58     {
59         if (empty($this->styles)) {
60             return $this->emptyStyle;
61         }
62
63         if (null === $style) {
64             return array_pop($this->styles);
65         }
66
67         foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
68             if ($style->apply('') === $stackedStyle->apply('')) {
69                 $this->styles = array_slice($this->styles, 0, $index);
70
71                 return $stackedStyle;
72             }
73         }
74
75         throw new InvalidArgumentException('Incorrectly nested style tag found.');
76     }
77
78     /**
79      * Computes current style with stacks top codes.
80      *
81      * @return OutputFormatterStyle
82      */
83     public function getCurrent()
84     {
85         if (empty($this->styles)) {
86             return $this->emptyStyle;
87         }
88
89         return $this->styles[count($this->styles) - 1];
90     }
91
92     /**
93      * @return $this
94      */
95     public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
96     {
97         $this->emptyStyle = $emptyStyle;
98
99         return $this;
100     }
101
102     /**
103      * @return OutputFormatterStyleInterface
104      */
105     public function getEmptyStyle()
106     {
107         return $this->emptyStyle;
108     }
109 }