Completed
Push — master ( c7bdb1...5472d7 )
by Aydin
04:34
created

MenuStyle::setTitleSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PhpSchool\CliMenu;
4
5
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
6
use PhpSchool\CliMenu\Terminal\TerminalFactory;
7
use PhpSchool\CliMenu\Terminal\TerminalInterface;
8
9
//TODO: B/W fallback
10
11
/**
12
 * Class MenuStyle
13
 *
14
 * @package PhpSchool\CliMenu
15
 * @author Michael Woodward <[email protected]>
16
 */
17
class MenuStyle
18
{
19
    /**
20
     * @var TerminalInterface
21
     */
22
    protected $terminal;
23
24
    /**
25
     * @var string
26
     */
27
    protected $fg;
28
29
    /**
30
     * @var string
31
     */
32
    protected $bg;
33
34
    /**
35
     * @var int
36
     */
37
    protected $width;
38
39
    /**
40
     * @var int
41
     */
42
    protected $padding;
43
44
    /**
45
     * @var int
46
     */
47
    protected $margin;
48
49
    /**
50
     * @var int
51
     */
52
    protected $contentWidth;
53
54
    /**
55
     * @var string
56
     */
57
    private $selectedMarker;
58
59
    /**
60
     * @var string
61
     */
62
    private $unselectedMarker;
63
64
    /**
65
     * @var string
66
     */
67
    private $itemExtra;
68
69
    /**
70
     * @var bool
71
     */
72
    private $displaysExtra;
73
74
    /**
75
     * @var string
76
     */
77
    private $titleSeparator;
78
79
    /**
80
     * Default Values
81
     *
82
     * @var array
83
     */
84
    private static $defaultStyleValues = [
85
        'fg' => 'white',
86
        'bg' => 'blue',
87
        'width' => 100,
88
        'padding' => 2,
89
        'margin' => 2,
90
        'selectedMarker' => '●',
91
        'unselectedMarker' => '○',
92
        'itemExtra' => '✔',
93
        'displaysExtra' => false,
94
        'titleSeparator' => '=',
95
    ];
96
97
    /**
98
     * @return array
99
     */
100
    public static function getDefaultStyleValues()
101
    {
102
        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...
103
    }
104
105
    /**
106
     * @var array
107
     */
108
    private static $availableForegroundColors = array(
109
        'black'   => array('set' => 30, 'unset' => 39),
110
        'red'     => array('set' => 31, 'unset' => 39),
111
        'green'   => array('set' => 32, 'unset' => 39),
112
        'yellow'  => array('set' => 33, 'unset' => 39),
113
        'blue'    => array('set' => 34, 'unset' => 39),
114
        'magenta' => array('set' => 35, 'unset' => 39),
115
        'cyan'    => array('set' => 36, 'unset' => 39),
116
        'white'   => array('set' => 37, 'unset' => 39),
117
        'default' => array('set' => 39, 'unset' => 39),
118
    );
119
120
    /**
121
     * @var array
122
     */
123
    private static $availableBackgroundColors = array(
124
        'black'   => array('set' => 40, 'unset' => 49),
125
        'red'     => array('set' => 41, 'unset' => 49),
126
        'green'   => array('set' => 42, 'unset' => 49),
127
        'yellow'  => array('set' => 43, 'unset' => 49),
128
        'blue'    => array('set' => 44, 'unset' => 49),
129
        'magenta' => array('set' => 45, 'unset' => 49),
130
        'cyan'    => array('set' => 46, 'unset' => 49),
131
        'white'   => array('set' => 47, 'unset' => 49),
132
        'default' => array('set' => 49, 'unset' => 49),
133
    );
134
135
    /**
136
     * @var array
137
     */
138
    private static $availableOptions = array(
139
        'bold'       => array('set' => 1, 'unset' => 22),
140
        'dim'        => array('set' => 2, 'unset' => 22),
141
        'underscore' => array('set' => 4, 'unset' => 24),
142
        'blink'      => array('set' => 5, 'unset' => 25),
143
        'reverse'    => array('set' => 7, 'unset' => 27),
144
        'conceal'    => array('set' => 8, 'unset' => 28)
145
    );
146
147
    /**
148
     * Initialise style
149
     *
150
     * @param TerminalInterface $terminal
151
     */
152
    public function __construct(TerminalInterface $terminal = null)
153
    {
154
        $this->terminal = $terminal ?: TerminalFactory::fromSystem();
155
156
        $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...
157
        $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...
158
        $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...
159
        $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...
160
        $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...
161
        $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...
162
        $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...
163
        $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...
164
        $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...
165
        $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...
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public static function getAvailableColours()
172
    {
173
        return array_keys(self::$availableBackgroundColors);
174
    }
175
176
    /**
177
     * @param string $text
178
     * @return string
179
     */
180
    public function getDisabledItemText($text)
181
    {
182
        return sprintf(
183
            "\033[%sm%s\033[%sm",
184
            self::$availableOptions['dim']['set'],
185
            $text,
186
            self::$availableOptions['dim']['unset']
187
        );
188
    }
189
    
190
    /**
191
     * Get the colour code set for Bg and Fg
192
     *
193
     * @return string
194
     */
195 View Code Duplication
    public function getSelectedSetCode()
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...
196
    {
197
        return sprintf(
198
            "\033[%sm",
199
            implode(';', [
200
                self::$availableBackgroundColors[$this->getFg()]['set'],
201
                self::$availableForegroundColors[$this->getBg()]['set'],
202
            ])
203
        );
204
    }
205
206
    /**
207
     * Get the colour unset code for Bg and Fg
208
     *
209
     * @return string
210
     */
211 View Code Duplication
    public function getSelectedUnsetCode()
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...
212
    {
213
        return sprintf(
214
            "\033[%sm",
215
            implode(';', [
216
                self::$availableBackgroundColors[$this->getBg()]['unset'],
217
                self::$availableForegroundColors[$this->getFg()]['unset'],
218
            ])
219
        );
220
    }
221
222
    /**
223
     * Get the inverted colour code
224
     *
225
     * @return string
226
     */
227 View Code Duplication
    public function getUnselectedSetCode()
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...
228
    {
229
        return sprintf(
230
            "\033[%sm",
231
            implode(';', [
232
                self::$availableBackgroundColors[$this->getBg()]['set'],
233
                self::$availableForegroundColors[$this->getFg()]['set'],
234
            ])
235
        );
236
    }
237
238
    /**
239
     * Get the inverted colour unset code
240
     *
241
     * @return string
242
     */
243 View Code Duplication
    public function getUnselectedUnsetCode()
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...
244
    {
245
        return sprintf(
246
            "\033[%sm",
247
            implode(';', [
248
                self::$availableBackgroundColors[$this->getBg()]['unset'],
249
                self::$availableForegroundColors[$this->getFg()]['unset'],
250
            ])
251
        );
252
    }
253
254
    /**
255
     * Calculate the contents width
256
     */
257
    protected function calculateContentWidth()
258
    {
259
        $this->contentWidth = $this->width - ($this->padding*2) - ($this->margin*2);
260
    }
261
262
    /**
263
     * @return string
264
     */
265
    public function getFg()
266
    {
267
        return $this->fg;
268
    }
269
270
    /**
271
     * @param string $fg
272
     * @return MenuStyle
273
     */
274
    public function setFg($fg)
275
    {
276
        $this->fg = $fg;
277
278
        return $this;
279
    }
280
281
    /**
282
     * @return string
283
     */
284
    public function getBg()
285
    {
286
        return $this->bg;
287
    }
288
289
    /**
290
     * @param string $bg
291
     * @return MenuStyle
292
     */
293
    public function setBg($bg)
294
    {
295
        $this->bg = $bg;
296
297
        return $this;
298
    }
299
300
    /**
301
     * @return int
302
     */
303
    public function getWidth()
304
    {
305
        return $this->width;
306
    }
307
308
    /**
309
     * @param int $width
310
     * @return MenuStyle
311
     */
312
    public function setWidth($width)
313
    {
314
        $availableWidth = $this->terminal->getWidth() - ($this->margin * 2) - ($this->padding * 2);
315
316
        if ($width >= $availableWidth) {
317
            $width = $availableWidth;
318
        }
319
320
        $this->width = $width;
321
        $this->calculateContentWidth();
322
323
        return $this;
324
    }
325
326
    /**
327
     * @return int
328
     */
329
    public function getPadding()
330
    {
331
        return $this->padding;
332
    }
333
334
    /**
335
     * @param int $padding
336
     * @return MenuStyle
337
     */
338
    public function setPadding($padding)
339
    {
340
        $this->padding = $padding;
341
342
        $this->calculateContentWidth();
343
344
        return $this;
345
    }
346
347
    /**
348
     * @return int
349
     */
350
    public function getMargin()
351
    {
352
        return $this->margin;
353
    }
354
355
    /**
356
     * @param int $margin
357
     * @return MenuStyle
358
     */
359
    public function setMargin($margin)
360
    {
361
        $this->margin = $margin;
362
363
        $this->calculateContentWidth();
364
365
        return $this;
366
    }
367
368
    /**
369
     * @return int
370
     */
371
    public function getContentWidth()
372
    {
373
        return $this->contentWidth;
374
    }
375
376
    /**
377
     * Get padding for right had side of content
378
     *
379
     * @param $contentLength
380
     * @return int
381
     */
382
    public function getRightHandPadding($contentLength)
383
    {
384
        return $this->getContentWidth() - $contentLength + $this->getPadding();
385
    }
386
387
    /**
388
     * @return string
389
     */
390
    public function getSelectedMarker()
391
    {
392
        return $this->selectedMarker;
393
    }
394
395
    /**
396
     * @param string $marker
397
     * @return $this
398
     */
399
    public function setSelectedMarker($marker)
400
    {
401
        $this->selectedMarker = mb_substr($marker, 0, 1);
402
403
        return $this;
404
    }
405
406
    /**
407
     * @return string
408
     */
409
    public function getUnselectedMarker()
410
    {
411
        return $this->unselectedMarker;
412
    }
413
414
    /**
415
     * @param string $marker
416
     * @return $this
417
     */
418
    public function setUnselectedMarker($marker)
419
    {
420
        $this->unselectedMarker = mb_substr($marker, 0, 1);
421
422
        return $this;
423
    }
424
425
    /**
426
     * Get the correct marker for the item
427
     *
428
     * @param bool $selected
429
     * @return string
430
     */
431
    public function getMarker($selected)
432
    {
433
        return $selected ? $this->selectedMarker : $this->unselectedMarker;
434
    }
435
436
    /**
437
     * @param string $itemExtra
438
     * @return $this
439
     */
440
    public function setItemExtra($itemExtra)
441
    {
442
        $this->itemExtra = $itemExtra;
443
444
        return $this;
445
    }
446
447
    /**
448
     * @return string
449
     */
450
    public function getItemExtra()
451
    {
452
        return $this->itemExtra;
453
    }
454
455
    /**
456
     * @return bool
457
     */
458
    public function getDisplaysExtra()
459
    {
460
        return $this->displaysExtra;
461
    }
462
463
    /**
464
     * @param bool $displaysExtra
465
     * @return $this
466
     */
467
    public function setDisplaysExtra($displaysExtra)
468
    {
469
        $this->displaysExtra = $displaysExtra;
470
471
        return $this;
472
    }
473
474
    /**
475
     * @return string
476
     */
477
    public function getTitleSeparator()
478
    {
479
        return $this->titleSeparator;
480
    }
481
482
    /**
483
     * @param string $actionSeparator
484
     * @return $this
485
     */
486
    public function setTitleSeparator($actionSeparator)
487
    {
488
        $this->titleSeparator = $actionSeparator;
489
490
        return $this;
491
    }
492
}
493