11
11
use PhpSchool \CliMenu \CliMenu ;
12
12
use PhpSchool \CliMenu \MenuStyle ;
13
13
use PhpSchool \CliMenu \Terminal \TerminalFactory ;
14
- use PhpSchool \CliMenu \Util \ColourUtil ;
15
14
use PhpSchool \Terminal \Terminal ;
16
15
use RuntimeException ;
17
16
@@ -54,7 +53,7 @@ class CliMenuBuilder implements Builder
54
53
private $ exitButtonText = 'Exit ' ;
55
54
56
55
/**
57
- * @var array
56
+ * @var MenuStyle
58
57
*/
59
58
private $ style ;
60
59
@@ -78,13 +77,16 @@ class CliMenuBuilder implements Builder
78
77
*/
79
78
private $ disabled = false ;
80
79
81
- public function __construct (Builder $ parent = null )
80
+ public function __construct (Terminal $ terminal = null , Builder $ parent = null )
82
81
{
82
+ $ this ->terminal = $ terminal ?? TerminalFactory::fromSystem ();
83
83
$ this ->parent = $ parent ;
84
- $ this ->terminal = $ this ->parent !== null
85
- ? $ this ->parent ->getTerminal ()
86
- : TerminalFactory::fromSystem ();
87
- $ this ->style = MenuStyle::getDefaultStyleValues ();
84
+ $ this ->style = new MenuStyle ($ this ->terminal );
85
+ }
86
+
87
+ public static function newFromParent (Builder $ parent ) : self
88
+ {
89
+ return new self ($ parent ->getTerminal (), $ parent );
88
90
}
89
91
90
92
public function setTitle (string $ title ) : self
@@ -170,174 +172,127 @@ public function setExitButtonText(string $exitButtonText) : self
170
172
171
173
public function setBackgroundColour (string $ colour , string $ fallback = null ) : self
172
174
{
173
- $ this ->style ['bg ' ] = ColourUtil::validateColour (
174
- $ this ->terminal ,
175
- $ colour ,
176
- $ fallback
177
- );
175
+ $ this ->style ->setBg ($ colour , $ fallback );
178
176
179
177
return $ this ;
180
178
}
181
179
182
180
public function setForegroundColour (string $ colour , string $ fallback = null ) : self
183
181
{
184
- $ this ->style ['fg ' ] = ColourUtil::validateColour (
185
- $ this ->terminal ,
186
- $ colour ,
187
- $ fallback
188
- );
182
+ $ this ->style ->setFg ($ colour , $ fallback );
189
183
190
184
return $ this ;
191
185
}
192
186
193
187
public function setWidth (int $ width ) : self
194
188
{
195
- $ this ->style [ ' width ' ] = $ width ;
189
+ $ this ->style -> setWidth ( $ width) ;
196
190
197
191
return $ this ;
198
192
}
199
193
200
194
public function setPadding (int $ topBottom , int $ leftRight = null ) : self
201
195
{
202
- if ($ leftRight === null ) {
203
- $ leftRight = $ topBottom ;
204
- }
205
-
206
- $ this ->setPaddingTopBottom ($ topBottom );
207
- $ this ->setPaddingLeftRight ($ leftRight );
196
+ $ this ->style ->setPadding ($ topBottom , $ leftRight );
208
197
209
198
return $ this ;
210
199
}
211
200
212
201
public function setPaddingTopBottom (int $ topBottom ) : self
213
202
{
214
- $ this ->style [ ' paddingTopBottom ' ] = $ topBottom ;
203
+ $ this ->style -> setPaddingTopBottom ( $ topBottom) ;
215
204
216
205
return $ this ;
217
206
}
218
207
219
208
public function setPaddingLeftRight (int $ leftRight ) : self
220
209
{
221
- $ this ->style [ ' paddingLeftRight ' ] = $ leftRight ;
210
+ $ this ->style -> setPaddingLeftRight ( $ leftRight) ;
222
211
223
212
return $ this ;
224
213
}
225
214
226
215
public function setMarginAuto () : self
227
216
{
228
- $ this ->style [ ' marginAuto ' ] = true ;
217
+ $ this ->style -> setMarginAuto () ;
229
218
230
219
return $ this ;
231
220
}
232
221
233
222
public function setMargin (int $ margin ) : self
234
223
{
235
- $ this ->style ['marginAuto ' ] = false ;
236
- $ this ->style ['margin ' ] = $ margin ;
224
+ $ this ->style ->setMargin ($ margin );
237
225
238
226
return $ this ;
239
227
}
240
228
241
229
public function setUnselectedMarker (string $ marker ) : self
242
230
{
243
- $ this ->style [ ' unselectedMarker ' ] = $ marker ;
231
+ $ this ->style -> setUnselectedMarker ( $ marker) ;
244
232
245
233
return $ this ;
246
234
}
247
235
248
236
public function setSelectedMarker (string $ marker ) : self
249
237
{
250
- $ this ->style [ ' selectedMarker ' ] = $ marker ;
238
+ $ this ->style -> setSelectedMarker ( $ marker) ;
251
239
252
240
return $ this ;
253
241
}
254
242
255
243
public function setItemExtra (string $ extra ) : self
256
244
{
257
- $ this ->style [ ' itemExtra ' ] = $ extra ;
245
+ $ this ->style -> setItemExtra ( $ extra) ;
258
246
259
247
return $ this ;
260
248
}
261
249
262
250
public function setTitleSeparator (string $ separator ) : self
263
251
{
264
- $ this ->style [ ' titleSeparator ' ] = $ separator ;
252
+ $ this ->style -> setTitleSeparator ( $ separator) ;
265
253
266
254
return $ this ;
267
255
}
268
256
269
- public function setBorder (
270
- int $ topWidth ,
271
- $ rightWidth = null ,
272
- $ bottomWidth = null ,
273
- $ leftWidth = null ,
274
- string $ colour = null
275
- ) : self {
276
- if (!is_int ($ rightWidth )) {
277
- $ colour = $ rightWidth ;
278
- $ rightWidth = $ bottomWidth = $ leftWidth = $ topWidth ;
279
- } elseif (!is_int ($ bottomWidth )) {
280
- $ colour = $ bottomWidth ;
281
- $ bottomWidth = $ topWidth ;
282
- $ leftWidth = $ rightWidth ;
283
- } elseif (!is_int ($ leftWidth )) {
284
- $ colour = $ leftWidth ;
285
- $ leftWidth = $ rightWidth ;
286
- }
287
-
288
- $ this ->style ['borderTopWidth ' ] = $ topWidth ;
289
- $ this ->style ['borderRightWidth ' ] = $ rightWidth ;
290
- $ this ->style ['borderBottomWidth ' ] = $ bottomWidth ;
291
- $ this ->style ['borderLeftWidth ' ] = $ leftWidth ;
292
-
293
- if (is_string ($ colour )) {
294
- $ this ->style ['borderColour ' ] = $ colour ;
295
- } elseif ($ colour !== null ) {
296
- throw new \InvalidArgumentException ('Invalid colour ' );
297
- }
257
+ public function setBorder (int $ top , $ right = null , $ bottom = null , $ left = null , string $ colour = null ) : self
258
+ {
259
+ $ this ->style ->setBorder ($ top , $ right , $ bottom , $ left , $ colour );
298
260
299
261
return $ this ;
300
262
}
301
263
302
264
public function setBorderTopWidth (int $ width ) : self
303
265
{
304
- $ this ->style [ ' borderTopWidth ' ] = $ width ;
266
+ $ this ->style -> setBorderTopWidth ( $ width) ;
305
267
306
268
return $ this ;
307
269
}
308
270
309
271
public function setBorderRightWidth (int $ width ) : self
310
272
{
311
- $ this ->style [ ' borderRightWidth ' ] = $ width ;
273
+ $ this ->style -> setBorderRightWidth ( $ width) ;
312
274
313
275
return $ this ;
314
276
}
315
277
316
278
public function setBorderBottomWidth (int $ width ) : self
317
279
{
318
- $ this ->style [ ' borderBottomWidth ' ] = $ width ;
280
+ $ this ->style -> setBorderBottomWidth ( $ width) ;
319
281
320
282
return $ this ;
321
283
}
322
284
323
285
public function setBorderLeftWidth (int $ width ) : self
324
286
{
325
- $ this ->style [ ' borderLeftWidth ' ] = $ width ;
287
+ $ this ->style -> setBorderLeftWidth ( $ width) ;
326
288
327
289
return $ this ;
328
290
}
329
291
330
292
public function setBorderColour (string $ colour , $ fallback = null ) : self
331
293
{
332
- $ this ->style ['borderColour ' ] = $ colour ;
333
- $ this ->style ['borderColourFallback ' ] = $ fallback ;
334
-
335
- return $ this ;
336
- }
294
+ $ this ->style ->setBorderColour ($ colour , $ fallback );
337
295
338
- public function setTerminal (Terminal $ terminal ) : self
339
- {
340
- $ this ->terminal = $ terminal ;
341
296
return $ this ;
342
297
}
343
298
@@ -378,40 +333,16 @@ private function itemsHaveExtra(array $items) : bool
378
333
public function getMenuStyle () : MenuStyle
379
334
{
380
335
if (null === $ this ->parent ) {
381
- return $ this ->buildStyle () ;
336
+ return $ this ->style ;
382
337
}
383
338
384
- if ($ this ->style !== MenuStyle:: getDefaultStyleValues ()) {
385
- return $ this ->buildStyle () ;
339
+ if ($ this ->style -> hasChangedFromDefaults ()) {
340
+ return $ this ->style ;
386
341
}
387
342
388
343
return $ this ->parent ->getMenuStyle ();
389
344
}
390
345
391
- private function buildStyle () : MenuStyle
392
- {
393
- $ style = (new MenuStyle ($ this ->terminal ))
394
- ->setFg ($ this ->style ['fg ' ])
395
- ->setBg ($ this ->style ['bg ' ])
396
- ->setWidth ($ this ->style ['width ' ])
397
- ->setPaddingTopBottom ($ this ->style ['paddingTopBottom ' ])
398
- ->setPaddingLeftRight ($ this ->style ['paddingLeftRight ' ])
399
- ->setSelectedMarker ($ this ->style ['selectedMarker ' ])
400
- ->setUnselectedMarker ($ this ->style ['unselectedMarker ' ])
401
- ->setItemExtra ($ this ->style ['itemExtra ' ])
402
- ->setDisplaysExtra ($ this ->style ['displaysExtra ' ])
403
- ->setTitleSeparator ($ this ->style ['titleSeparator ' ])
404
- ->setBorderTopWidth ($ this ->style ['borderTopWidth ' ])
405
- ->setBorderRightWidth ($ this ->style ['borderRightWidth ' ])
406
- ->setBorderBottomWidth ($ this ->style ['borderBottomWidth ' ])
407
- ->setBorderLeftWidth ($ this ->style ['borderLeftWidth ' ])
408
- ->setBorderColour ($ this ->style ['borderColour ' ], $ this ->style ['borderColourFallback ' ]);
409
-
410
- $ this ->style ['marginAuto ' ] ? $ style ->setMarginAuto () : $ style ->setMargin ($ this ->style ['margin ' ]);
411
-
412
- return $ style ;
413
- }
414
-
415
346
/**
416
347
* @throws RuntimeException
417
348
*/
@@ -450,7 +381,7 @@ public function build() : CliMenu
450
381
$ menuItems = $ this ->buildSplitItems ($ mergedItems );
451
382
$ menuItems = $ this ->buildSubMenus ($ menuItems );
452
383
453
- $ this ->style [ ' displaysExtra ' ] = $ this ->itemsHaveExtra ($ menuItems );
384
+ $ this ->style -> setDisplaysExtra ( $ this ->itemsHaveExtra ($ menuItems) );
454
385
455
386
$ menu = new CliMenu (
456
387
$ this ->menuTitle ,
0 commit comments