Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / symfony / console / Symfony / Component / Console / Formatter / OutputFormatterStyle.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 /**
15  * Formatter style class for defining styles.
16  *
17  * @author Konstantin Kudryashov <ever.zet@gmail.com>
18  *
19  * @api
20  */
21 class OutputFormatterStyle implements OutputFormatterStyleInterface
22 {
23     private static $availableForegroundColors = array(
24         'black'     => 30,
25         'red'       => 31,
26         'green'     => 32,
27         'yellow'    => 33,
28         'blue'      => 34,
29         'magenta'   => 35,
30         'cyan'      => 36,
31         'white'     => 37
32     );
33     private static $availableBackgroundColors = array(
34         'black'     => 40,
35         'red'       => 41,
36         'green'     => 42,
37         'yellow'    => 43,
38         'blue'      => 44,
39         'magenta'   => 45,
40         'cyan'      => 46,
41         'white'     => 47
42     );
43     private static $availableOptions = array(
44         'bold'          => 1,
45         'underscore'    => 4,
46         'blink'         => 5,
47         'reverse'       => 7,
48         'conceal'       => 8
49     );
50
51     private $foreground;
52     private $background;
53     private $options = array();
54
55     /**
56      * Initializes output formatter style.
57      *
58      * @param string $foreground The style foreground color name
59      * @param string $background The style background color name
60      * @param array  $options    The style options
61      *
62      * @api
63      */
64     public function __construct($foreground = null, $background = null, array $options = array())
65     {
66         if (null !== $foreground) {
67             $this->setForeground($foreground);
68         }
69         if (null !== $background) {
70             $this->setBackground($background);
71         }
72         if (count($options)) {
73             $this->setOptions($options);
74         }
75     }
76
77     /**
78      * Sets style foreground color.
79      *
80      * @param string $color The color name
81      *
82      * @throws \InvalidArgumentException When the color name isn't defined
83      *
84      * @api
85      */
86     public function setForeground($color = null)
87     {
88         if (null === $color) {
89             $this->foreground = null;
90
91             return;
92         }
93
94         if (!isset(static::$availableForegroundColors[$color])) {
95             throw new \InvalidArgumentException(sprintf(
96                 'Invalid foreground color specified: "%s". Expected one of (%s)',
97                 $color,
98                 implode(', ', array_keys(static::$availableForegroundColors))
99             ));
100         }
101
102         $this->foreground = static::$availableForegroundColors[$color];
103     }
104
105     /**
106      * Sets style background color.
107      *
108      * @param string $color The color name
109      *
110      * @throws \InvalidArgumentException When the color name isn't defined
111      *
112      * @api
113      */
114     public function setBackground($color = null)
115     {
116         if (null === $color) {
117             $this->background = null;
118
119             return;
120         }
121
122         if (!isset(static::$availableBackgroundColors[$color])) {
123             throw new \InvalidArgumentException(sprintf(
124                 'Invalid background color specified: "%s". Expected one of (%s)',
125                 $color,
126                 implode(', ', array_keys(static::$availableBackgroundColors))
127             ));
128         }
129
130         $this->background = static::$availableBackgroundColors[$color];
131     }
132
133     /**
134      * Sets some specific style option.
135      *
136      * @param string $option The option name
137      *
138      * @throws \InvalidArgumentException When the option name isn't defined
139      *
140      * @api
141      */
142     public function setOption($option)
143     {
144         if (!isset(static::$availableOptions[$option])) {
145             throw new \InvalidArgumentException(sprintf(
146                 'Invalid option specified: "%s". Expected one of (%s)',
147                 $option,
148                 implode(', ', array_keys(static::$availableOptions))
149             ));
150         }
151
152         if (false === array_search(static::$availableOptions[$option], $this->options)) {
153             $this->options[] = static::$availableOptions[$option];
154         }
155     }
156
157     /**
158      * Unsets some specific style option.
159      *
160      * @param string $option The option name
161      *
162      * @throws \InvalidArgumentException When the option name isn't defined
163      *
164      */
165     public function unsetOption($option)
166     {
167         if (!isset(static::$availableOptions[$option])) {
168             throw new \InvalidArgumentException(sprintf(
169                 'Invalid option specified: "%s". Expected one of (%s)',
170                 $option,
171                 implode(', ', array_keys(static::$availableOptions))
172             ));
173         }
174
175         $pos = array_search(static::$availableOptions[$option], $this->options);
176         if (false !== $pos) {
177             unset($this->options[$pos]);
178         }
179     }
180
181     /**
182      * Sets multiple style options at once.
183      *
184      * @param array $options
185      */
186     public function setOptions(array $options)
187     {
188         $this->options = array();
189
190         foreach ($options as $option) {
191             $this->setOption($option);
192         }
193     }
194
195     /**
196      * Applies the style to a given text.
197      *
198      * @param string $text The text to style
199      *
200      * @return string
201      */
202     public function apply($text)
203     {
204         $codes = array();
205
206         if (null !== $this->foreground) {
207             $codes[] = $this->foreground;
208         }
209         if (null !== $this->background) {
210             $codes[] = $this->background;
211         }
212         if (count($this->options)) {
213             $codes = array_merge($codes, $this->options);
214         }
215
216         if (0 === count($codes)) {
217             return $text;
218         }
219
220         return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
221     }
222 }