comparison vendor/symfony/console/Formatter/OutputFormatterStyleStack.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 /**
27 * @var OutputFormatterStyleInterface
28 */
29 private $emptyStyle;
30
31 /**
32 * Constructor.
33 *
34 * @param OutputFormatterStyleInterface|null $emptyStyle
35 */
36 public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
37 {
38 $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
39 $this->reset();
40 }
41
42 /**
43 * Resets stack (ie. empty internal arrays).
44 */
45 public function reset()
46 {
47 $this->styles = array();
48 }
49
50 /**
51 * Pushes a style in the stack.
52 *
53 * @param OutputFormatterStyleInterface $style
54 */
55 public function push(OutputFormatterStyleInterface $style)
56 {
57 $this->styles[] = $style;
58 }
59
60 /**
61 * Pops a style from the stack.
62 *
63 * @param OutputFormatterStyleInterface|null $style
64 *
65 * @return OutputFormatterStyleInterface
66 *
67 * @throws InvalidArgumentException When style tags incorrectly nested
68 */
69 public function pop(OutputFormatterStyleInterface $style = null)
70 {
71 if (empty($this->styles)) {
72 return $this->emptyStyle;
73 }
74
75 if (null === $style) {
76 return array_pop($this->styles);
77 }
78
79 foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
80 if ($style->apply('') === $stackedStyle->apply('')) {
81 $this->styles = array_slice($this->styles, 0, $index);
82
83 return $stackedStyle;
84 }
85 }
86
87 throw new InvalidArgumentException('Incorrectly nested style tag found.');
88 }
89
90 /**
91 * Computes current style with stacks top codes.
92 *
93 * @return OutputFormatterStyle
94 */
95 public function getCurrent()
96 {
97 if (empty($this->styles)) {
98 return $this->emptyStyle;
99 }
100
101 return $this->styles[count($this->styles) - 1];
102 }
103
104 /**
105 * @param OutputFormatterStyleInterface $emptyStyle
106 *
107 * @return $this
108 */
109 public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
110 {
111 $this->emptyStyle = $emptyStyle;
112
113 return $this;
114 }
115
116 /**
117 * @return OutputFormatterStyleInterface
118 */
119 public function getEmptyStyle()
120 {
121 return $this->emptyStyle;
122 }
123 }