1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Kernel\Cli\Writers; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Kernel\Cli\Writers\Interfaces\ContextualClassInterface; |
||
19 | use O2System\Kernel\Cli\Writers\Traits\ContextualColorClassSetterTrait; |
||
20 | use O2System\Kernel\Cli\Writers\Traits\IndentSetterTrait; |
||
21 | use O2System\Kernel\Cli\Writers\Traits\NewLinesSetterTrait; |
||
22 | use O2System\Kernel\Cli\Writers\Traits\StringSetterTrait; |
||
23 | |||
24 | /** |
||
25 | * Class Label |
||
26 | * |
||
27 | * @package O2System\Kernel\Cli\Writers |
||
28 | */ |
||
29 | class Label implements ContextualClassInterface |
||
30 | { |
||
31 | use ContextualColorClassSetterTrait; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
32 | use IndentSetterTrait; |
||
33 | use NewLinesSetterTrait; |
||
34 | use StringSetterTrait; |
||
35 | |||
36 | /** |
||
37 | * Label::__construct |
||
38 | * |
||
39 | * @param string $string |
||
40 | * @param string $contextualClass |
||
41 | */ |
||
42 | public function __construct($string = null, $contextualClass = 'default') |
||
43 | { |
||
44 | $this->setIndent(1); |
||
45 | $this->setString($string); |
||
46 | $this->setContextualClass($contextualClass); |
||
47 | } |
||
48 | |||
49 | // ------------------------------------------------------------------------ |
||
50 | |||
51 | /** |
||
52 | * Label::__toString |
||
53 | * |
||
54 | * Implementation __toString magic method so that when the class is converted to a string |
||
55 | * automatically performs the rendering process. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function __toString() |
||
60 | { |
||
61 | return $this->render(); |
||
62 | } |
||
63 | |||
64 | // ------------------------------------------------------------------------ |
||
65 | |||
66 | /** |
||
67 | * Label::render |
||
68 | * |
||
69 | * Rendering labeled string. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function render() |
||
74 | { |
||
75 | // if the string of label is empty then nothing to be processed. |
||
76 | if (empty($this->string)) { |
||
77 | return ''; |
||
78 | } |
||
79 | |||
80 | $string = str_repeat(' ', 2) . $this->string . str_repeat(' ', 2); |
||
81 | |||
82 | switch ($this->contextualClass) { |
||
83 | default: |
||
84 | $output = $string; |
||
85 | break; |
||
86 | case 'primary': |
||
87 | $output = str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[44m" . str_repeat( |
||
88 | ' ', |
||
89 | strlen($string) |
||
90 | ) . "\033[0m" . "\r\n"; |
||
91 | $output .= str_repeat( |
||
92 | ' ', |
||
93 | $this->indent |
||
94 | ) . "\033[1;37m" . "\033[44m" . $string . "\033[0m" . "\r\n"; |
||
95 | $output .= str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[44m" . str_repeat( |
||
96 | ' ', |
||
97 | strlen($string) |
||
98 | ) . "\033[0m"; |
||
99 | break; |
||
100 | case 'success': |
||
101 | $output = str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[42m" . str_repeat( |
||
102 | ' ', |
||
103 | strlen($string) |
||
104 | ) . "\033[0m" . "\r\n"; |
||
105 | $output .= str_repeat( |
||
106 | ' ', |
||
107 | $this->indent |
||
108 | ) . "\033[1;37m" . "\033[42m" . $string . "\033[0m" . "\r\n"; |
||
109 | $output .= str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[42m" . str_repeat( |
||
110 | ' ', |
||
111 | strlen($string) |
||
112 | ) . "\033[0m"; |
||
113 | break; |
||
114 | case 'info': |
||
115 | $output = str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[46m" . str_repeat( |
||
116 | ' ', |
||
117 | strlen($string) |
||
118 | ) . "\033[0m" . "\r\n"; |
||
119 | $output .= str_repeat( |
||
120 | ' ', |
||
121 | $this->indent |
||
122 | ) . "\033[1;37m" . "\033[46m" . $string . "\033[0m" . "\r\n"; |
||
123 | $output .= str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[46m" . str_repeat( |
||
124 | ' ', |
||
125 | strlen($string) |
||
126 | ) . "\033[0m"; |
||
127 | break; |
||
128 | case 'warning': |
||
129 | $output = str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[43m" . str_repeat( |
||
130 | ' ', |
||
131 | strlen($string) |
||
132 | ) . "\033[0m" . "\r\n"; |
||
133 | $output .= str_repeat( |
||
134 | ' ', |
||
135 | $this->indent |
||
136 | ) . "\033[1;37m" . "\033[43m" . $string . "\033[0m" . "\r\n"; |
||
137 | $output .= str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[43m" . str_repeat( |
||
138 | ' ', |
||
139 | strlen($string) |
||
140 | ) . "\033[0m"; |
||
141 | break; |
||
142 | case 'danger': |
||
143 | $output = str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[41m" . |
||
144 | str_repeat(' ', strlen($string)) . "\033[0m" . "\r\n"; |
||
145 | $output .= str_repeat( |
||
146 | ' ', |
||
147 | $this->indent |
||
148 | ) . "\033[1;37m" . "\033[41m" . $string . "\033[0m" . "\r\n"; |
||
149 | $output .= str_repeat(' ', $this->indent) . "\033[1;37m" . "\033[41m" . |
||
150 | str_repeat(' ', strlen($string)) . "\033[0m"; |
||
151 | break; |
||
152 | } |
||
153 | |||
154 | return str_repeat(PHP_EOL, $this->newLinesBefore) . $output . str_repeat(PHP_EOL, $this->newLinesAfter); |
||
155 | } |
||
156 | } |