Completed
Push — master ( 389ef3...499201 )
by Aydin
24s queued 11s
created

SelectableItem::getRows()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 22
nc 2
nop 2
dl 0
loc 33
rs 9.2568
c 1
b 1
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\MenuStyle;
6
use PhpSchool\CliMenu\Util\StringUtil;
7
use PhpSchool\CliMenu\Style\SelectableStyle;
8
9
/**
10
 * @author Michael Woodward <[email protected]>
11
 */
12
class SelectableItem implements MenuItemInterface
13
{
14
    /**
15
     * @var callable
16
     */
17
    private $selectAction;
18
19
    private $text = '';
20
21
    private $showItemExtra = false;
22
23
    private $disabled = false;
24
25
    /**
26
     * @var SelectableStyle;
27
     */
28
    private $style;
29
30
    public function __construct(
31
        string $text,
32
        callable $selectAction,
33
        bool $showItemExtra = false,
34
        bool $disabled = false
35
    ) {
36
        $this->text          = $text;
37
        $this->selectAction  = $selectAction;
38
        $this->showItemExtra = $showItemExtra;
39
        $this->disabled      = $disabled;
40
41
        $this->style = new SelectableStyle();
42
    }
43
44
    /**
45
     * The output text for the item
46
     */
47
    public function getRows(MenuStyle $style, bool $selected = false) : array
48
    {
49
        $marker = sprintf("%s", $this->style->getMarker($selected));
50
51
        $length = $this->style->getDisplaysExtra()
52
            ? $style->getContentWidth() - (mb_strlen($this->style->getItemExtra()) + 2)
53
            : $style->getContentWidth();
54
55
        $rows = explode(
56
            "\n",
57
            StringUtil::wordwrap(
58
                sprintf('%s%s', $marker, $this->text),
59
                $length,
60
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
61
            )
62
        );
63
64
        return array_map(function ($row, $key) use ($style, $length) {
65
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
66
67
            if ($key === 0) {
68
                return $this->showItemExtra
69
                    ? sprintf(
70
                        '%s%s  %s',
71
                        $text,
72
                        str_repeat(' ', $length - mb_strlen($row)),
73
                        $this->style->getItemExtra()
74
                    )
75
                    : $text;
76
            }
77
78
            return $text;
79
        }, $rows, array_keys($rows));
80
    }
81
82
    /**
83
     * Execute the items callable if required
84
     */
85
    public function getSelectAction() : ?callable
86
    {
87
        return $this->selectAction;
88
    }
89
90
    public function getStyle() : SelectableStyle
91
    {
92
        return $this->style;
93
    }
94
95
    public function setStyle(SelectableStyle $style) : self
96
    {
97
        $this->style = $style;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Return the raw string of text
104
     */
105
    public function getText() : string
106
    {
107
        return $this->text;
108
    }
109
110
    /**
111
     * Set the raw string of text
112
     */
113
    public function setText(string $text) : void
114
    {
115
        $this->text = $text;
116
    }
117
118
    /**
119
     * Can the item be selected
120
     */
121
    public function canSelect() : bool
122
    {
123
        return !$this->disabled;
124
    }
125
126
    public function showsItemExtra() : bool
127
    {
128
        return $this->showItemExtra;
129
    }
130
131
    /**
132
     * Enable showing item extra
133
     */
134
    public function showItemExtra() : void
135
    {
136
        $this->showItemExtra = true;
137
    }
138
139
    /**
140
     * Disable showing item extra
141
     */
142
    public function hideItemExtra() : void
143
    {
144
        $this->showItemExtra = false;
145
    }
146
}
147