Completed
Push — master ( b50376...8f8eb6 )
by Povilas
02:13
created

Figlet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * This is the part of Povils open-source library.
5
 *
6
 * @author Povilas Susinskas
7
 */
8
9
namespace Povils\Figlet;
10
11
/**
12
 * Class Figlet
13
 *
14
 * @package Povils\Figlet
15
 */
16
class Figlet implements FigletInterface
17
{
18
    /**
19
     * Defines first ASCII character code (blank/space).
20
     */
21
    const FIRST_ASCII_CHARACTER = 32;
22
23
    /**
24
     * @var ColorManager
25
     */
26
    private $colorManager;
27
28
    /**
29
     * @var FontManager
30
     */
31
    private $fontManager;
32
33
    /**
34
     * @var Font
35
     */
36
    private $font;
37
38
    /**
39
     * @var string
40
     */
41
    private $backgroundColor;
42
43
    /**
44
     * @var string
45
     */
46
    private $fontColor;
47
48
    /**
49
     * @var string
50
     */
51
    private $fontName;
52
53
    /**
54
     * @var string
55
     */
56
    private $fontDir;
57
58
    /**
59
     * @var int
60
     */
61
    private $stretching;
62
63
    /**
64
     * This array will hold used Figlet characters.
65
     *
66
     * @var array
67
     */
68
    private $characters = [];
69
70
    /**
71
     * Figlet constructor.
72
     */
73
    public function __construct()
74
    {
75
        $this->fontDir = __DIR__ . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR;
76
        $this->fontName = 'big';
77
        $this->stretching = 0;
78
    }
79
80
    /**
81
     * Outputs Figlet text.
82
     *
83
     * @param string $text
84
     *
85
     * @return FigletInterface
86
     */
87
    public function write($text)
88
    {
89
        echo $this->render($text);
90
91
        return $this;
92
    }
93
94
    /**
95
     * Renders Figlet text.
96
     *
97
     * @param string $text
98
     *
99
     * @return string
100
     * @throws \Exception
101
     */
102
    public function render($text)
103
    {
104
        $this->font = $this->getFontManager()->loadFont($this->fontName, $this->fontDir);
105
106
        $figletText = $this->generateFigletText($text);
107
108
        if ($this->fontColor || $this->backgroundColor) {
109
            $figletText = $this->colorize($figletText);
110
        }
111
112
        return $figletText;
113
    }
114
115
    /**
116
     * @param string $color
117
     *
118
     * @return FigletInterface
119
     */
120
    public function setBackgroundColor($color)
121
    {
122
        $this->backgroundColor = $color;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param string $color
129
     *
130
     * @return FigletInterface
131
     */
132
    public function setFontColor($color)
133
    {
134
        $this->fontColor = $color;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param string $fontName
141
     *
142
     * @return FigletInterface
143
     */
144
    public function setFont($fontName)
145
    {
146
        $this->fontName = $fontName;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $fontDir
153
     *
154
     * @return FigletInterface
155
     */
156
    public function setFontDir($fontDir)
157
    {
158
        $this->fontDir = $fontDir;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @param int $stretching
165
     *
166
     * @return FigletInterface
167
     */
168
    public function setFontStretching($stretching)
169
    {
170
        $this->stretching = $stretching;
171
172
        return $this;
173
    }
174
175
    /**
176
     * Unset some arrays and objects.
177
     */
178
    public function clear()
179
    {
180
        unset($this->characters);
181
        unset($this->font);
182
    }
183
184
    /**
185
     * Generates Figlet text.
186
     *
187
     * @param string $text
188
     *
189
     * @return string
190
     */
191
    private function generateFigletText($text)
192
    {
193
        $figletCharacters = $this->getFigletCharacters($text);
194
195
        return $this->combineFigletCharacters($figletCharacters);
196
    }
197
198
    /**
199
     * @param string $text
200
     *
201
     * @return array
202
     */
203
    private function getFigletCharacters($text)
204
    {
205
        $figletCharacters = [];
206
        foreach (str_split($text) as $character) {
207
            $figletCharacters[] = $this->getFigletCharacter($character);
208
        }
209
210
        return $figletCharacters;
211
    }
212
213
    /**
214
     * @param string $character
215
     *
216
     * @return array
217
     */
218
    private function getFigletCharacter($character)
219
    {
220
        if (isset($this->characters[$this->fontName][$character])) {
221
            return $this->characters[$this->fontName][$character];
222
        }
223
224
        $figletCharacter = [];
225
226
        $lines = $this->getFigletCharacterLines($character);
227
228
        foreach ($lines as $line) {
229
            $figletCharacter[] = str_replace(
230
                ['@', $this->font->getHardBlank()],
231
                ['', ' '],
232
                $line
233
            );
234
        }
235
236
        $this->characters[$this->fontName][$character] = $figletCharacter;
237
238
        return $figletCharacter;
239
    }
240
241
    /**
242
     * @param string $character
243
     *
244
     * @return array
245
     */
246
    private function getFigletCharacterLines($character)
247
    {
248
        $letterStartPosition = $this->getLetterStartPosition($character);
249
250
        $lines = array_slice($this->font->getFileCollection(), $letterStartPosition, $this->font->getHeight());
251
252
        return $lines;
253
    }
254
255
    /**
256
     * Combines Figlet characters to one.
257
     *
258
     * @param array $figletCharacters
259
     *
260
     * @return string
261
     */
262
    private function combineFigletCharacters($figletCharacters)
263
    {
264
        $figletText = '';
265
266
        $height = $this->font->getHeight();
267
        for ($line = 0; $line < $height; $line++) {
268
            $singleLine = '';
269
            foreach ($figletCharacters as $charactersLines) {
270
                $singleLine .= $charactersLines[$line] . $this->addStretching();
271
            }
272
            $singleLine = $this->removeNewlines($singleLine);
273
            $figletText .= $singleLine . "\n";
274
        }
275
276
        return $figletText;
277
    }
278
279
    /**
280
     * Colorize text.
281
     *
282
     * @param string $figletText
283
     *
284
     * @return string
285
     */
286
    private function colorize($figletText)
287
    {
288
        $figletText = $this
289
            ->getColorManager()
290
            ->colorize(
291
                $figletText,
292
                $this->fontColor,
293
                $this->backgroundColor
294
            );
295
296
        return $figletText;
297
    }
298
299
    /**
300
     * @return ColorManager
301
     */
302
    private function getColorManager()
303
    {
304
        if (null === $this->colorManager) {
305
            return $this->colorManager = new ColorManager();
306
        }
307
308
        return $this->colorManager;
309
    }
310
311
    /**
312
     * @return FontManager
313
     */
314
    private function getFontManager()
315
    {
316
        if (null === $this->fontManager) {
317
            return $this->fontManager = new FontManager();
318
        }
319
320
        return $this->fontManager;
321
    }
322
323
    /**
324
     * Remove newlines characters.
325
     *
326
     * @param string $singleLine
327
     *
328
     * @return string
329
     */
330
    private function removeNewlines($singleLine)
331
    {
332
        $singleLine = preg_replace('/[\\r\\n]*/', '', $singleLine);
333
334
        return $singleLine;
335
    }
336
337
    /**
338
     * Adds space(s) in the end to Figlet character.
339
     *
340
     * @return string
341
     */
342
    private function addStretching()
343
    {
344
        if (is_int($this->stretching) && 0 < $this->stretching) {
345
            $stretchingSpace = ' ';
346
        } else {
347
            $stretchingSpace = '';
348
            $this->stretching = 0;
349
        }
350
351
        return str_repeat($stretchingSpace, $this->stretching);
352
    }
353
354
    /**
355
     * @param string $character
356
     *
357
     * @return int
358
     */
359
    private function getLetterStartPosition($character)
360
    {
361
        return ((ord($character) - self::FIRST_ASCII_CHARACTER) * $this->font->getHeight()) + 1 + $this->font->getCommentLines();
362
    }
363
}
364