Completed
Pull Request — master (#82)
by
unknown
05:12
created

SelectableTrait::setStartRowNumber()   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 1
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\MenuStyle;
6
use PhpSchool\CliMenu\Util\StringUtil;
7
8
/**
9
 * @author Michael Woodward <[email protected]>
10
 */
11
trait SelectableTrait
12
{
13
    /**
14
     * @var string
15
     */
16
    private $text = '';
17
18
    /**
19
     * @var bool
20
     */
21
    private $showItemExtra = false;
22
23
    /**
24
     * @var bool
25
     */
26
    private $disabled = false;
27
28
    /**
29
     * @var int
30
     */
31
    private $numberOfRows = 0;
32
33
    /**
34
     * @var int
35
     */
36
    private $startRowNumber = 0;
37
38
    /**
39
     * Returns the number of terminal rows the item takes
40
     */
41
    public function getNumberOfRows() : int
42
    {
43
        return $this->numberOfRows;
44
    }
45
46
    /**
47
     * Sets the row number the item starts at in the frame
48
     */
49
    public function setStartRowNumber(int $rowNumber) : void
50
    {
51
        $this->startRowNumber = $rowNumber;
52
    }
53
54
    /**
55
     * Returns the row number the item starts at in the frame
56
     */
57
    public function getStartRowNumber() : int
58
    {
59
        return $this->startRowNumber;
60
    }
61
62
    /**
63
     * The output text for the item
64
     */
65
    public function getRows(MenuStyle $style, bool $selected = false) : array
66
    {
67
        $marker = sprintf("%s ", $style->getMarker($selected));
68
69
        $length = $style->getDisplaysExtra()
70
            ? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2)
71
            : $style->getContentWidth();
72
73
        $rows = explode(
74
            "\n",
75
            StringUtil::wordwrap(
76
                sprintf('%s%s', $marker, $this->text),
77
                $length,
78
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
79
            )
80
        );
81
82
        $this->numberOfRows = count($rows);
83
84
        return array_map(function ($row, $key) use ($style, $length) {
85
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
86
87
            if ($key === 0) {
88
                return $this->showItemExtra
89
                    ? sprintf('%s%s  %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra())
90
                    : $text;
91
            }
92
93
            return $text;
94
        }, $rows, array_keys($rows));
95
    }
96
97
    /**
98
     * Can the item be selected
99
     */
100
    public function canSelect() : bool
101
    {
102
        return !$this->disabled;
103
    }
104
105
    public function showsItemExtra() : bool
106
    {
107
        return $this->showItemExtra;
108
    }
109
110
    /**
111
     * Enable showing item extra
112
     */
113
    public function showItemExtra() : void
114
    {
115
        $this->showItemExtra = true;
116
    }
117
118
    /**
119
     * Disable showing item extra
120
     */
121
    public function hideItemExtra() : void
122
    {
123
        $this->showItemExtra = false;
124
    }
125
}
126