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

AsciiArtItem::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 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() : int
58
    {
59
        return $this->numberOfRows;
60
    }
61
62
    /**
63
     * Sets the row number the item starts at in the frame
64
     */
65
    public function setStartRowNumber(int $rowNumber) : void
66
    {
67
        $this->startRowNumber = $rowNumber;
68
    }
69
70
    /**
71
     * Returns the row number the item starts at in the frame
72
     */
73
    public function getStartRowNumber() : int
74
    {
75
        return $this->startRowNumber;
76
    }
77
78
    /**
79
     * The output text for the item
80
     */
81
    public function getRows(MenuStyle $style, bool $selected = false) : array
82
    {
83
        $rows = array_map(function ($row) use ($style) {
84
            $length = mb_strlen($row);
85
86
            $padding = $style->getContentWidth() - $length;
87
88
            switch ($this->position) {
89
                case self::POSITION_LEFT:
90
                    return $row;
91
                    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...
92
                case self::POSITION_RIGHT:
93
                    $row = rtrim($row);
94
                    $padding = $padding - ($this->artLength - mb_strlen($row));
95
                    $row = sprintf('%s%s', str_repeat(' ', $padding), $row);
96
                    break;
97
                case self::POSITION_CENTER:
98
                default:
99
                    $row = rtrim($row);
100
                    $padding = $padding - ($this->artLength - mb_strlen($row));
101
                    $left = ceil($padding/2);
102
                    $right = $padding - $left;
103
                    $row = sprintf('%s%s%s', str_repeat(' ', $left), $row, str_repeat(' ', $right));
104
                    break;
105
            }
106
107
            return $row;
108
        }, explode("\n", $this->text));
109
110
        $this->numberOfRows = count($rows);
111
112
        return $rows;
113
    }
114
115
    /**
116
     * Can the item be selected
117
     */
118
    public function canSelect() : bool
119
    {
120
        return false;
121
    }
122
123
    /**
124
     * Execute the items callable if required
125
     */
126
    public function getSelectAction() : ?callable
127
    {
128
        return null;
129
    }
130
131
    /**
132
     * Return the raw string of text
133
     */
134
    public function getText() : string
135
    {
136
        return $this->text;
137
    }
138
139
    /**
140
     * Return the length of the art
141
     */
142
    public function getArtLength() : int
143
    {
144
        return $this->artLength;
145
    }
146
147
    /**
148
     * Whether or not the menu item is showing the menustyle extra value
149
     */
150
    public function showsItemExtra() : bool
151
    {
152
        return false;
153
    }
154
155
    /**
156
     * Enable showing item extra
157
     */
158
    public function showItemExtra() : void
159
    {
160
        //noop
161
    }
162
163
    /**
164
     * Disable showing item extra
165
     */
166
    public function hideItemExtra() : void
167
    {
168
        //noop
169
    }
170
}
171