Completed
Pull Request — master (#203)
by
unknown
01:50
created

SelectableTrait::showItemExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\MenuStyle;
6
use PhpSchool\CliMenu\Style;
7
use PhpSchool\CliMenu\Util\StringUtil;
8
9
/**
10
 * @author Michael Woodward <[email protected]>
11
 */
12
trait SelectableTrait
13
{
14
    private $text = '';
15
16
    private $showItemExtra = false;
17
18
    private $disabled = false;
19
20
    /**
21
     * @var Style\ItemStyleInterface;
22
     */
23
    private $style;
24
25
    public function getStyle() : Style\ItemStyleInterface
26
    {
27
        if (!$this->style) {
28
            $this->style = new Style\SelectableStyle();
29
        }
30
31
        return $this->style;
32
    }
33
34
    /**
35
     * @param Style\ItemStyleInterface|Style\SelectableStyle $style
36
     * @return $this
37
     */
38
    public function setStyle(Style\ItemStyleInterface $style) : self
39
    {
40
        $this->style = $style;
41
42
        return $this;
43
    }
44
45
    /**
46
     * The output text for the item
47
     */
48
    public function getRows(MenuStyle $style, bool $selected = false) : array
49
    {
50
        $marker = sprintf("%s", $this->style->getMarker($selected));
51
52
        $length = $this->style->getDisplaysExtra()
53
            ? $style->getContentWidth() - (mb_strlen($this->style->getItemExtra()) + 2)
54
            : $style->getContentWidth();
55
56
        $rows = explode(
57
            "\n",
58
            StringUtil::wordwrap(
59
                sprintf('%s%s', $marker, $this->text),
60
                $length,
61
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
62
            )
63
        );
64
65
        return array_map(function ($row, $key) use ($style, $length) {
66
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
67
68
            if ($key === 0) {
69
                return $this->showItemExtra
70
                    ? sprintf('%s%s  %s', $text, str_repeat(' ', $length - mb_strlen($row)), $this->style->getItemExtra())
71
                    : $text;
72
            }
73
74
            return $text;
75
        }, $rows, array_keys($rows));
76
    }
77
78
    /**
79
     * Can the item be selected
80
     */
81
    public function canSelect() : bool
82
    {
83
        return !$this->disabled;
84
    }
85
86
    public function showsItemExtra() : bool
87
    {
88
        return $this->showItemExtra;
89
    }
90
91
    /**
92
     * Enable showing item extra
93
     */
94
    public function showItemExtra() : void
95
    {
96
        $this->showItemExtra = true;
97
    }
98
99
    /**
100
     * Disable showing item extra
101
     */
102
    public function hideItemExtra() : void
103
    {
104
        $this->showItemExtra = false;
105
    }
106
}
107