Completed
Pull Request — master (#82)
by
unknown
01:39
created

AsciiArtItem::setStartRowNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
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 Assert\Assertion;
6
use PhpSchool\CliMenu\MenuStyle;
7
8
/**
9
 * @author Michael Woodward <[email protected]>
10
 */
11
class AsciiArtItem implements MenuItemInterface
12
{
13
    /**
14
     * Possible positions of the ascii art
15
     */
16
    const POSITION_CENTER = 'center';
17
    const POSITION_LEFT   = 'left';
18
    const POSITION_RIGHT  = 'right';
19
    
20
    /**
21
     * @var string
22
     */
23
    private $text;
24
25
    /**
26
     * @var string
27
     */
28
    private $position;
29
30
    /**
31
     * @var int
32
     */
33
    private $artLength;
34
35
    /**
36
     * @var int
37
     */
38
    private $numberOfRows = 0;
39
40
    /**
41
     * @var int
42
     */
43
    private $startRowNumber = 0;
44
45
    public function __construct(string $text, string $position = self::POSITION_CENTER)
46
    {
47
        Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);
48
        
49
        $this->text      = $text;
50
        $this->position  = $position;
51
        $this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
52
    }
53
54
    /**
55
     * Returns the number of terminal rows the item takes
56
     */
57
    public function getNumberOfRows() {
58
        return $this->numberOfRows;
59
    }
60
61
    /**
62
     * Sets the row number the item starts at in the frame
63
     */
64
    public function setStartRowNumber(int $rowNumber) {
65
        $this->startRowNumber = $rowNumber;
66
    }
67
68
    /**
69
     * Returns the row number the item starts at in the frame
70
     */
71
    public function getStartRowNumber() {
72
        return $this->startRowNumber;
73
    }
74
75
    /**
76
     * The output text for the item
77
     */
78
    public function getRows(MenuStyle $style, bool $selected = false) : array
79
    {
80
        $rows = array_map(function ($row) use ($style) {
81
            $length = mb_strlen($row);
82
83
            $padding = $style->getContentWidth() - $length;
84
85
            switch ($this->position) {
86
                case self::POSITION_LEFT:
87
                    return $row;
88
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
89
                case self::POSITION_RIGHT:
90
                    $row = rtrim($row);
91
                    $padding = $padding - ($this->artLength - mb_strlen($row));
92
                    $row = sprintf('%s%s', str_repeat(' ', $padding), $row);
93
                    break;
94
                case self::POSITION_CENTER:
95
                default:
96
                    $row = rtrim($row);
97
                    $padding = $padding - ($this->artLength - mb_strlen($row));
98
                    $left = ceil($padding/2);
99
                    $right = $padding - $left;
100
                    $row = sprintf('%s%s%s', str_repeat(' ', $left), $row, str_repeat(' ', $right));
101
                    break;
102
            }
103
104
            return $row;
105
        }, explode("\n", $this->text));
106
107
        $this->numberOfRows = count($rows);
108
109
        return $rows;
110
    }
111
112
    /**
113
     * Can the item be selected
114
     */
115
    public function canSelect() : bool
116
    {
117
        return false;
118
    }
119
120
    /**
121
     * Execute the items callable if required
122
     */
123
    public function getSelectAction() : ?callable
124
    {
125
        return null;
126
    }
127
128
    /**
129
     * Return the raw string of text
130
     */
131
    public function getText() : string
132
    {
133
        return $this->text;
134
    }
135
136
    /**
137
     * Return the length of the art
138
     */
139
    public function getArtLength() : int
140
    {
141
        return $this->artLength;
142
    }
143
144
    /**
145
     * Whether or not the menu item is showing the menustyle extra value
146
     */
147
    public function showsItemExtra() : bool
148
    {
149
        return false;
150
    }
151
152
    /**
153
     * Enable showing item extra
154
     */
155
    public function showItemExtra() : void
156
    {
157
        //noop
158
    }
159
160
    /**
161
     * Disable showing item extra
162
     */
163
    public function hideItemExtra() : void
164
    {
165
        //noop
166
    }
167
}
168