@@ -16,7 +16,7 @@ class SplitItem implements MenuItemInterface
16
16
/**
17
17
* @var array
18
18
*/
19
- private $ items ;
19
+ private $ items = [] ;
20
20
21
21
/**
22
22
* @var int|null
@@ -44,12 +44,11 @@ class SplitItem implements MenuItemInterface
44
44
45
45
public function __construct (array $ items = [])
46
46
{
47
- $ this ->items = $ items ;
48
-
47
+ $ this ->addItems ($ items );
49
48
$ this ->setDefaultSelectedItem ();
50
49
}
51
50
52
- public function addMenuItem (MenuItemInterface $ item ) : self
51
+ public function addItem (MenuItemInterface $ item ) : self
53
52
{
54
53
foreach (self ::$ blacklistedItems as $ bl ) {
55
54
if ($ item instanceof $ bl ) {
@@ -61,18 +60,19 @@ public function addMenuItem(MenuItemInterface $item) : self
61
60
return $ this ;
62
61
}
63
62
64
- public function addMenuItems (array $ items ) : self
63
+ public function addItems (array $ items ) : self
65
64
{
66
65
foreach ($ items as $ item ) {
67
- $ this ->addMenuItem ($ item );
66
+ $ this ->addItem ($ item );
68
67
}
68
+
69
69
return $ this ;
70
70
}
71
-
71
+
72
72
public function setItems (array $ items ) : self
73
73
{
74
74
$ this ->items = [];
75
- $ this ->addMenuItems ($ items );
75
+ $ this ->addItems ($ items );
76
76
return $ this ;
77
77
}
78
78
@@ -100,72 +100,90 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
100
100
{
101
101
$ numberOfItems = count ($ this ->items );
102
102
103
+ if ($ numberOfItems === 0 ) {
104
+ throw new \RuntimeException (sprintf ('There should be at least one item added to: %s ' , __CLASS__ ));
105
+ }
106
+
103
107
if (!$ selected ) {
104
108
$ this ->setDefaultSelectedItem ();
105
109
}
106
110
107
- $ length = $ style ->getDisplaysExtra ()
108
- ? floor (($ style ->getContentWidth () - mb_strlen ($ style ->getItemExtra ()) + 2 ) / $ numberOfItems )
109
- : floor ($ style ->getContentWidth () / $ numberOfItems );
110
-
111
- $ length -= $ this ->margin ;
112
-
111
+ $ length = floor ($ style ->getContentWidth () / $ numberOfItems ) - $ this ->margin ;
113
112
$ missingLength = $ style ->getContentWidth () % $ numberOfItems ;
114
-
115
- $ cells = array_map (function ($ index , $ item ) use ($ selected , $ length , $ style ) {
116
- $ isSelected = $ selected && $ index === $ this ->selectedItemIndex ;
117
- $ marker = $ item ->canSelect ()
118
- ? sprintf ("%s " , $ style ->getMarker ($ isSelected ))
119
- : sprintf ("%s " , str_repeat (' ' , mb_strlen ($ style ->getMarker (false ))));
120
- $ content = StringUtil::wordwrap (
121
- sprintf ('%s%s ' , $ marker , $ item ->getText ()),
122
- $ length
123
- );
124
- return array_map (function ($ row ) use ($ length , $ style , $ isSelected ) {
125
- $ invertedColoursSetCode = $ isSelected
126
- ? $ style ->getInvertedColoursSetCode ()
127
- : '' ;
128
- $ invertedColoursUnsetCode = $ isSelected
129
- ? $ style ->getInvertedColoursUnsetCode ()
113
+
114
+ return $ this ->buildRows (
115
+ array_map (function ($ index , $ item ) use ($ selected , $ length , $ style ) {
116
+ $ isSelected = $ selected && $ index === $ this ->selectedItemIndex ;
117
+ $ marker = $ item ->canSelect ()
118
+ ? sprintf ('%s ' , $ style ->getMarker ($ isSelected ))
130
119
: '' ;
131
120
132
- return sprintf (
133
- "%s%s%s%s%s " ,
134
- $ invertedColoursSetCode ,
135
- $ row ,
136
- str_repeat (' ' , $ length - mb_strlen ($ row )),
137
- $ invertedColoursUnsetCode ,
138
- str_repeat (' ' , $ this ->margin )
121
+ return $ this ->buildCell (
122
+ explode ("\n" , StringUtil::wordwrap (sprintf ('%s%s ' , $ marker , $ item ->getText ()), $ length )),
123
+ $ length ,
124
+ $ style ,
125
+ $ isSelected
139
126
);
140
- }, explode ("\n" , $ content ));
141
- }, array_keys ($ this ->items ), $ this ->items );
142
-
143
- $ lines = max (array_map ('count ' , $ cells ));
144
-
145
- $ rows = [];
146
- for ($ i = 0 ; $ i < $ lines ; $ i ++) {
147
- $ row = "" ;
148
- if ($ i > 0 ) {
149
- $ row .= str_repeat (' ' , 2 );
150
- }
151
- foreach ($ cells as $ cell ) {
152
- if (isset ($ cell [$ i ])) {
153
- $ row .= $ cell [$ i ];
154
- } else {
155
- $ row .= str_repeat (' ' , $ length );
156
- }
157
- }
158
- if ($ missingLength ) {
159
- $ row .= str_repeat (' ' , $ missingLength );
160
- }
161
- $ rows [] = $ row ;
162
- }
163
-
164
- return $ rows ;
127
+ }, array_keys ($ this ->items ), $ this ->items ),
128
+ $ missingLength ,
129
+ $ length
130
+ );
131
+ }
132
+
133
+ private function buildRows (array $ cells , int $ missingLength , int $ length ) : array
134
+ {
135
+ return array_map (
136
+ function ($ i ) use ($ cells , $ length , $ missingLength ) {
137
+ return $ this ->buildRow ($ cells , $ i , $ length , $ missingLength );
138
+ },
139
+ range (0 , max (array_map ('count ' , $ cells )) - 1 )
140
+ );
141
+ }
142
+
143
+ private function buildRow (array $ cells , int $ index , int $ length , int $ missingLength ) : string
144
+ {
145
+ return sprintf (
146
+ '%s%s ' ,
147
+ implode (
148
+ '' ,
149
+ array_map (
150
+ function ($ cell ) use ($ index , $ length ) {
151
+ return $ cell [$ index ] ?? str_repeat (' ' , $ length + $ this ->margin );
152
+ },
153
+ $ cells
154
+ )
155
+ ),
156
+ str_repeat (' ' , $ missingLength )
157
+ );
158
+ }
159
+
160
+ private function buildCell (array $ content , int $ length , MenuStyle $ style , bool $ isSelected ) : array
161
+ {
162
+ return array_map (function ($ row ) use ($ length , $ style , $ isSelected ) {
163
+ $ invertedColoursSetCode = $ isSelected
164
+ ? $ style ->getInvertedColoursSetCode ()
165
+ : '' ;
166
+ $ invertedColoursUnsetCode = $ isSelected
167
+ ? $ style ->getInvertedColoursUnsetCode ()
168
+ : '' ;
169
+
170
+ return sprintf (
171
+ '%s%s%s%s%s ' ,
172
+ $ invertedColoursSetCode ,
173
+ $ row ,
174
+ str_repeat (' ' , $ length - mb_strlen ($ row )),
175
+ $ invertedColoursUnsetCode ,
176
+ str_repeat (' ' , $ this ->margin )
177
+ );
178
+ }, $ content );
165
179
}
166
180
167
181
public function setSelectedItemIndex (int $ index ) : void
168
182
{
183
+ if (!isset ($ this ->items [$ index ])) {
184
+ throw new \InvalidArgumentException (sprintf ('Index: "%s" does not exist ' , $ index ));
185
+ }
186
+
169
187
$ this ->selectedItemIndex = $ index ;
170
188
}
171
189
@@ -232,6 +250,6 @@ public function hideItemExtra() : void
232
250
*/
233
251
public function getText () : string
234
252
{
235
- throw new \BadMethodCallException (sprintf ('Not supported on: %s ' , SplitItem::class ));
253
+ throw new \BadMethodCallException (sprintf ('Not supported on: %s ' , __CLASS__ ));
236
254
}
237
255
}
0 commit comments