Completed
Pull Request — master (#100)
by
unknown
04:15
created

MenuStyle::getMargin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\CliMenu;
4
5
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
6
use PhpSchool\CliMenu\Terminal\TerminalFactory;
7
use PhpSchool\CliMenu\Util\ColourUtil;
8
use PhpSchool\Terminal\Terminal;
9
10
//TODO: B/W fallback
11
12
/**
13
 * @author Michael Woodward <[email protected]>
14
 */
15
class MenuStyle
16
{
17
    /**
18
     * @var Terminal
19
     */
20
    protected $terminal;
21
22
    /**
23
     * @var int|string
24
     */
25
    protected $fg;
26
27
    /**
28
     * @var int|string
29
     */
30
    protected $bg;
31
32
    /**
33
     * @var int
34
     */
35
    protected $width;
36
37
    /**
38
     * @var int
39
     */
40
    protected $padding;
41
42
    /**
43
     * @var int
44
     */
45
    protected $margin;
46
47
    /**
48
     * @var int
49
     */
50
    protected $contentWidth;
51
52
    /**
53
     * @var string
54
     */
55
    private $selectedMarker;
56
57
    /**
58
     * @var string
59
     */
60
    private $unselectedMarker;
61
62
    /**
63
     * @var string
64
     */
65
    private $itemExtra;
66
67
    /**
68
     * @var bool
69
     */
70
    private $displaysExtra;
71
72
    /**
73
     * @var string
74
     */
75
    private $titleSeparator;
76
77
    /**
78
     * @var string
79
     */
80
    private $coloursSetCode;
81
82
    /**
83
     * @var string
84
     */
85
    private $invertedColoursSetCode = "\033[7m";
86
87
    /**
88
     * @var string
89
     */
90
    private $invertedColoursUnsetCode = "\033[27m";
91
92
    /**
93
     * @var string
94
     */
95
    private $coloursResetCode = "\033[0m";
96
97
    /**
98
     * @var int
99
     */
100
    private $borderTopWidth;
101
102
    /**
103
     * @var int
104
     */
105
    private $borderRightWidth;
106
107
    /**
108
     * @var int
109
     */
110
    private $borderBottomWidth;
111
112
    /**
113
     * @var int
114
     */
115
    private $borderLeftWidth;
116
117
    /**
118
     * @var int|string
119
     */
120
    private $borderColour = 'white';
121
122
    /**
123
     * @var array
124
     */
125
    private $borderTopRows = [];
126
127
    /**
128
     * @var array
129
     */
130
    private $borderBottomRows = [];
131
132
    /**
133
     * @var bool
134
     */
135
    private $marginAuto = false;
136
137
    /**
138
     * Default Values
139
     *
140
     * @var array
141
     */
142
    private static $defaultStyleValues = [
143
        'fg' => 'white',
144
        'bg' => 'blue',
145
        'width' => 100,
146
        'padding' => 2,
147
        'margin' => 2,
148
        'selectedMarker' => '●',
149
        'unselectedMarker' => '○',
150
        'itemExtra' => '✔',
151
        'displaysExtra' => false,
152
        'titleSeparator' => '=',
153
        'borderTopWidth' => 0,
154
        'borderRightWidth' => 0,
155
        'borderBottomWidth' => 0,
156
        'borderLeftWidth' => 0,
157
        'borderColour' => 'white',
158
        'marginAuto' => false,
159
    ];
160
161
    public static function getDefaultStyleValues() : array
162
    {
163
        return static::$defaultStyleValues;
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
164
    }
165
166
    /**
167
     * @var array
168
     */
169
    private static $availableForegroundColors = array(
170
        'black'   => 30,
171
        'red'     => 31,
172
        'green'   => 32,
173
        'yellow'  => 33,
174
        'blue'    => 34,
175
        'magenta' => 35,
176
        'cyan'    => 36,
177
        'white'   => 37,
178
        'default' => 39,
179
    );
180
181
    /**
182
     * @var array
183
     */
184
    private static $availableBackgroundColors = array(
185
        'black'   => 40,
186
        'red'     => 41,
187
        'green'   => 42,
188
        'yellow'  => 43,
189
        'blue'    => 44,
190
        'magenta' => 45,
191
        'cyan'    => 46,
192
        'white'   => 47,
193
        'default' => 49,
194
    );
195
196
    /**
197
     * @var array
198
     */
199
    private static $availableOptions = array(
200
        'bold'       => array('set' => 1, 'unset' => 22),
201
        'dim'        => array('set' => 2, 'unset' => 22),
202
        'underscore' => array('set' => 4, 'unset' => 24),
203
        'blink'      => array('set' => 5, 'unset' => 25),
204
        'reverse'    => array('set' => 7, 'unset' => 27),
205
        'conceal'    => array('set' => 8, 'unset' => 28)
206
    );
207
208
    /**
209
     * Initialise style
210
     */
211
    public function __construct(Terminal $terminal = null)
212
    {
213
        $this->terminal = $terminal ?: TerminalFactory::fromSystem();
214
215
        $this->setFg(static::$defaultStyleValues['fg']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
216
        $this->setBg(static::$defaultStyleValues['bg']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
217
        $this->setWidth(static::$defaultStyleValues['width']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
218
        $this->setPadding(static::$defaultStyleValues['padding']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
219
        $this->setMargin(static::$defaultStyleValues['margin']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
220
        $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
221
        $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
222
        $this->setItemExtra(static::$defaultStyleValues['itemExtra']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
223
        $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
224
        $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
225
        $this->setBorderTopWidth(static::$defaultStyleValues['borderTopWidth']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
226
        $this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
227
        $this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
228
        $this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
229
        $this->setBorderColour(static::$defaultStyleValues['borderColour']);
0 ignored issues
show
Bug introduced by
Since $defaultStyleValues is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultStyleValues to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
230
    }
231
232
    public function getDisabledItemText(string $text) : string
233
    {
234
        return sprintf(
235
            "\033[%sm%s\033[%sm",
236
            self::$availableOptions['dim']['set'],
237
            $text,
238
            self::$availableOptions['dim']['unset']
239
        );
240
    }
241
242
    /**
243
     * Generates the ansi escape sequence to set the colours
244
     */
245
    private function generateColoursSetCode() : void
246
    {
247
        if (is_string($this->fg)) {
248
            $fgCode = self::$availableForegroundColors[$this->fg];
249
        } else {
250
            $fgCode = sprintf("38;5;%s", $this->fg);
251
        }
252
253
        if (is_string($this->bg)) {
254
            $bgCode = self::$availableBackgroundColors[$this->bg];
255
        } else {
256
            $bgCode = sprintf("48;5;%s", $this->bg);
257
        }
258
259
        $this->coloursSetCode = sprintf("\033[%s;%sm", $fgCode, $bgCode);
260
    }
261
262
    /**
263
     * Get the colour code for Bg and Fg
264
     */
265
    public function getColoursSetCode() : string
266
    {
267
        return $this->coloursSetCode;
268
    }
269
270
    /**
271
     * Get the inverted escape sequence (used for selected elements)
272
     */
273
    public function getInvertedColoursSetCode() : string
274
    {
275
        return $this->invertedColoursSetCode;
276
    }
277
278
    /**
279
     * Get the inverted escape sequence (used for selected elements)
280
     */
281
    public function getInvertedColoursUnsetCode() : string
282
    {
283
        return $this->invertedColoursUnsetCode;
284
    }
285
286
    /**
287
     * Get the escape sequence used to reset colours to default
288
     */
289
    public function getColoursResetCode() : string
290
    {
291
        return $this->coloursResetCode;
292
    }
293
294
    /**
295
     * Calculate the contents width
296
     */
297
    protected function calculateContentWidth() : void
298
    {
299
        $this->contentWidth = $this->width
300
            - ($this->padding * 2)
301
            - ($this->borderRightWidth + $this->borderLeftWidth);
302
    }
303
304
    public function getFg()
305
    {
306
        return $this->fg;
307
    }
308
309 View Code Duplication
    public function setFg($fg, string $fallback = null) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
310
    {
311
        $this->fg = ColourUtil::validateColour(
312
            $this->terminal,
313
            $fg,
314
            $fallback
315
        );
316
        $this->generateColoursSetCode();
317
318
        return $this;
319
    }
320
321
    public function getBg()
322
    {
323
        return $this->bg;
324
    }
325
326 View Code Duplication
    public function setBg($bg, string $fallback = null) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
    {
328
        $this->bg = ColourUtil::validateColour(
329
            $this->terminal,
330
            $bg,
331
            $fallback
332
        );
333
        $this->generateColoursSetCode();
334
335
        return $this;
336
    }
337
338
    public function getWidth() : int
339
    {
340
        return $this->width;
341
    }
342
343
    public function setWidth(int $width) : self
344
    {
345
        if ($width >= $this->terminal->getWidth()) {
346
            $width = $this->terminal->getWidth();
347
        }
348
349
        $this->width = $width;
350
        if ($this->marginAuto) {
351
            $this->setMarginAuto();
352
        }
353
354
        $this->calculateContentWidth();
355
        $this->generateBorderRows();
356
357
        return $this;
358
    }
359
360
    public function getPadding() : int
361
    {
362
        return $this->padding;
363
    }
364
365
    public function setPadding(int $padding) : self
366
    {
367
        $this->padding = $padding;
368
369
        $this->calculateContentWidth();
370
371
        return $this;
372
    }
373
374
    public function getMargin() : int
375
    {
376
        return $this->margin;
377
    }
378
379
    public function setMarginAuto() : self
380
    {
381
        $this->marginAuto = true;
382
        $this->margin = floor(($this->terminal->getWidth() - $this->width) / 2);
0 ignored issues
show
Documentation Bug introduced by
The property $margin was declared of type integer, but floor(($this->terminal->...() - $this->width) / 2) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
383
        
384
        return $this;
385
    }
386
387
    public function setMargin(int $margin) : self
388
    {
389
        $this->marginAuto = false;
390
        $this->margin = $margin;
391
392
        return $this;
393
    }
394
395
    public function getContentWidth() : int
396
    {
397
        return $this->contentWidth;
398
    }
399
400
    /**
401
     * Get padding for right had side of content
402
     */
403
    public function getRightHandPadding(int $contentLength) : int
404
    {
405
        return $this->getContentWidth() - $contentLength + $this->getPadding();
406
    }
407
408
    public function getSelectedMarker() : string
409
    {
410
        return $this->selectedMarker;
411
    }
412
413
    public function setSelectedMarker(string $marker) : self
414
    {
415
        $this->selectedMarker = mb_substr($marker, 0, 1);
416
417
        return $this;
418
    }
419
420
    public function getUnselectedMarker() : string
421
    {
422
        return $this->unselectedMarker;
423
    }
424
425
    public function setUnselectedMarker(string $marker) : self
426
    {
427
        $this->unselectedMarker = mb_substr($marker, 0, 1);
428
429
        return $this;
430
    }
431
432
    /**
433
     * Get the correct marker for the item
434
     */
435
    public function getMarker(bool $selected) : string
436
    {
437
        return $selected ? $this->selectedMarker : $this->unselectedMarker;
438
    }
439
440
    public function setItemExtra(string $itemExtra) : self
441
    {
442
        $this->itemExtra = $itemExtra;
443
444
        return $this;
445
    }
446
447
    public function getItemExtra() : string
448
    {
449
        return $this->itemExtra;
450
    }
451
452
    public function getDisplaysExtra() : bool
453
    {
454
        return $this->displaysExtra;
455
    }
456
457
    public function setDisplaysExtra(bool $displaysExtra) : self
458
    {
459
        $this->displaysExtra = $displaysExtra;
460
461
        return $this;
462
    }
463
464
    public function getTitleSeparator() : string
465
    {
466
        return $this->titleSeparator;
467
    }
468
469
    public function setTitleSeparator(string $actionSeparator) : self
470
    {
471
        $this->titleSeparator = $actionSeparator;
472
473
        return $this;
474
    }
475
476
    private function generateBorderRows() : void
477
    {
478
        $borderRow = sprintf(
479
            "%s%s%s%s%s\n",
480
            str_repeat(' ', $this->margin),
481
            $this->getBorderColourCode(),
482
            str_repeat(' ', $this->width),
483
            $this->coloursResetCode,
484
            str_repeat(' ', $this->margin)
485
        );
486
487
        $this->borderTopRows = [];
488
        for ($b = 0; $b < $this->borderTopWidth; $b++) {
489
            $this->borderTopRows[] = $borderRow;
490
        }
491
492
        $this->borderBottomRows = [];
493
        for ($b = 0; $b < $this->borderBottomWidth; $b++) {
494
            $this->borderBottomRows[] = $borderRow;
495
        }
496
    }
497
498
    public function getBorderTopRows() : array
499
    {
500
        return $this->borderTopRows;
501
    }
502
503
    public function getBorderBottomRows() : array
504
    {
505
        return $this->borderBottomRows;
506
    }
507
508
    /**
509
     * Shorthand function to set all borders values at once
510
     */
511
    public function setBorder(
512
        int $topWidth,
513
        $rightWidth = null,
514
        $bottomWidth = null,
515
        $leftWidth = null,
516
        string $colour = null
517
    ) : self {
518 View Code Duplication
        if (!is_int($rightWidth)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
519
            $colour = $rightWidth;
520
            $rightWidth = $bottomWidth = $leftWidth = $topWidth;
521
        } elseif (!is_int($bottomWidth)) {
522
            $colour = $bottomWidth;
523
            $bottomWidth = $topWidth;
524
            $leftWidth = $rightWidth;
525
        } elseif (!is_int($leftWidth)) {
526
            $colour = $leftWidth;
527
            $leftWidth = $rightWidth;
528
        }
529
530
        $this->borderTopWidth = $topWidth;
531
        $this->borderRightWidth = $rightWidth;
532
        $this->borderBottomWidth = $bottomWidth;
533
        $this->borderLeftWidth = $leftWidth;
534
535
        if (is_string($colour)) {
536
            $this->setBorderColour($colour);
537
        }
538
539
        $this->calculateContentWidth();
540
        $this->generateBorderRows();
541
542
        return $this;
543
    }
544
545
    public function setBorderTopWidth(int $width) : self
546
    {
547
        $this->borderTopWidth = $width;
548
549
        $this->generateBorderRows();
550
551
        return $this;
552
    }
553
554
    public function setBorderRightWidth(int $width) : self
555
    {
556
        $this->borderRightWidth = $width;
557
        $this->calculateContentWidth();
558
559
        return $this;
560
    }
561
562
    public function setBorderBottomWidth(int $width) : self
563
    {
564
        $this->borderBottomWidth = $width;
565
566
        $this->generateBorderRows();
567
568
        return $this;
569
    }
570
571
    public function setBorderLeftWidth(int $width) : self
572
    {
573
        $this->borderLeftWidth = $width;
574
        $this->calculateContentWidth();
575
576
        return $this;
577
    }
578
579
    public function setBorderColour($colour, $fallback = null) : self
580
    {
581
        if (is_string($colour) && ctype_digit($colour)) {
582
            $colour = intval($colour);
583
        }
584
585
        $this->borderColour = ColourUtil::validateColour(
586
            $this->terminal,
587
            $colour,
588
            $fallback
589
        );
590
591
        $this->generateBorderRows();
592
593
        return $this;
594
    }
595
596
    public function getBorderTopWidth() : int
597
    {
598
        return $this->borderTopWidth;
599
    }
600
601
    public function getBorderRightWidth() : int
602
    {
603
        return $this->borderRightWidth;
604
    }
605
606
    public function getBorderBottomWidth() : int
607
    {
608
        return $this->borderBottomWidth;
609
    }
610
611
    public function getBorderLeftWidth() : int
612
    {
613
        return $this->borderLeftWidth;
614
    }
615
616
    public function getBorderColour()
617
    {
618
        return $this->borderColour;
619
    }
620
621
    public function getBorderColourCode() : string
622
    {
623
        if (is_string($this->borderColour)) {
624
            $borderColourCode = self::$availableBackgroundColors[$this->bg];
625
        } else {
626
            $borderColourCode = sprintf("48;5;%s", $this->bg);
627
        }
628
629
        return sprintf("\033[%sm", $borderColourCode);
630
    }
631
}
632