Skip to content

Commit 1495796

Browse files
committed
Simplify builder code
1 parent e3b994e commit 1495796

File tree

6 files changed

+100
-173
lines changed

6 files changed

+100
-173
lines changed

src/Builder/BuilderUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function addSubMenu(string $id, string $text) : CliMenuBuilder
7171
'id' => $id
7272
];
7373

74-
$this->subMenuBuilders[$id] = new CliMenuBuilder($this);
74+
$this->subMenuBuilders[$id] = CliMenuBuilder::newFromParent($this);
7575
return $this->subMenuBuilders[$id];
7676
}
7777

src/Builder/CliMenuBuilder.php

Lines changed: 33 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpSchool\CliMenu\CliMenu;
1212
use PhpSchool\CliMenu\MenuStyle;
1313
use PhpSchool\CliMenu\Terminal\TerminalFactory;
14-
use PhpSchool\CliMenu\Util\ColourUtil;
1514
use PhpSchool\Terminal\Terminal;
1615
use RuntimeException;
1716

@@ -54,7 +53,7 @@ class CliMenuBuilder implements Builder
5453
private $exitButtonText = 'Exit';
5554

5655
/**
57-
* @var array
56+
* @var MenuStyle
5857
*/
5958
private $style;
6059

@@ -78,13 +77,16 @@ class CliMenuBuilder implements Builder
7877
*/
7978
private $disabled = false;
8079

81-
public function __construct(Builder $parent = null)
80+
public function __construct(Terminal $terminal = null, Builder $parent = null)
8281
{
82+
$this->terminal = $terminal ?? TerminalFactory::fromSystem();
8383
$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);
8890
}
8991

9092
public function setTitle(string $title) : self
@@ -170,174 +172,127 @@ public function setExitButtonText(string $exitButtonText) : self
170172

171173
public function setBackgroundColour(string $colour, string $fallback = null) : self
172174
{
173-
$this->style['bg'] = ColourUtil::validateColour(
174-
$this->terminal,
175-
$colour,
176-
$fallback
177-
);
175+
$this->style->setBg($colour, $fallback);
178176

179177
return $this;
180178
}
181179

182180
public function setForegroundColour(string $colour, string $fallback = null) : self
183181
{
184-
$this->style['fg'] = ColourUtil::validateColour(
185-
$this->terminal,
186-
$colour,
187-
$fallback
188-
);
182+
$this->style->setFg($colour, $fallback);
189183

190184
return $this;
191185
}
192186

193187
public function setWidth(int $width) : self
194188
{
195-
$this->style['width'] = $width;
189+
$this->style->setWidth($width);
196190

197191
return $this;
198192
}
199193

200194
public function setPadding(int $topBottom, int $leftRight = null) : self
201195
{
202-
if ($leftRight === null) {
203-
$leftRight = $topBottom;
204-
}
205-
206-
$this->setPaddingTopBottom($topBottom);
207-
$this->setPaddingLeftRight($leftRight);
196+
$this->style->setPadding($topBottom, $leftRight);
208197

209198
return $this;
210199
}
211200

212201
public function setPaddingTopBottom(int $topBottom) : self
213202
{
214-
$this->style['paddingTopBottom'] = $topBottom;
203+
$this->style->setPaddingTopBottom($topBottom);
215204

216205
return $this;
217206
}
218207

219208
public function setPaddingLeftRight(int $leftRight) : self
220209
{
221-
$this->style['paddingLeftRight'] = $leftRight;
210+
$this->style->setPaddingLeftRight($leftRight);
222211

223212
return $this;
224213
}
225214

226215
public function setMarginAuto() : self
227216
{
228-
$this->style['marginAuto'] = true;
217+
$this->style->setMarginAuto();
229218

230219
return $this;
231220
}
232221

233222
public function setMargin(int $margin) : self
234223
{
235-
$this->style['marginAuto'] = false;
236-
$this->style['margin'] = $margin;
224+
$this->style->setMargin($margin);
237225

238226
return $this;
239227
}
240228

241229
public function setUnselectedMarker(string $marker) : self
242230
{
243-
$this->style['unselectedMarker'] = $marker;
231+
$this->style->setUnselectedMarker($marker);
244232

245233
return $this;
246234
}
247235

248236
public function setSelectedMarker(string $marker) : self
249237
{
250-
$this->style['selectedMarker'] = $marker;
238+
$this->style->setSelectedMarker($marker);
251239

252240
return $this;
253241
}
254242

255243
public function setItemExtra(string $extra) : self
256244
{
257-
$this->style['itemExtra'] = $extra;
245+
$this->style->setItemExtra($extra);
258246

259247
return $this;
260248
}
261249

262250
public function setTitleSeparator(string $separator) : self
263251
{
264-
$this->style['titleSeparator'] = $separator;
252+
$this->style->setTitleSeparator($separator);
265253

266254
return $this;
267255
}
268256

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);
298260

299261
return $this;
300262
}
301263

302264
public function setBorderTopWidth(int $width) : self
303265
{
304-
$this->style['borderTopWidth'] = $width;
266+
$this->style->setBorderTopWidth($width);
305267

306268
return $this;
307269
}
308270

309271
public function setBorderRightWidth(int $width) : self
310272
{
311-
$this->style['borderRightWidth'] = $width;
273+
$this->style->setBorderRightWidth($width);
312274

313275
return $this;
314276
}
315277

316278
public function setBorderBottomWidth(int $width) : self
317279
{
318-
$this->style['borderBottomWidth'] = $width;
280+
$this->style->setBorderBottomWidth($width);
319281

320282
return $this;
321283
}
322284

323285
public function setBorderLeftWidth(int $width) : self
324286
{
325-
$this->style['borderLeftWidth'] = $width;
287+
$this->style->setBorderLeftWidth($width);
326288

327289
return $this;
328290
}
329291

330292
public function setBorderColour(string $colour, $fallback = null) : self
331293
{
332-
$this->style['borderColour'] = $colour;
333-
$this->style['borderColourFallback'] = $fallback;
334-
335-
return $this;
336-
}
294+
$this->style->setBorderColour($colour, $fallback);
337295

338-
public function setTerminal(Terminal $terminal) : self
339-
{
340-
$this->terminal = $terminal;
341296
return $this;
342297
}
343298

@@ -378,40 +333,16 @@ private function itemsHaveExtra(array $items) : bool
378333
public function getMenuStyle() : MenuStyle
379334
{
380335
if (null === $this->parent) {
381-
return $this->buildStyle();
336+
return $this->style;
382337
}
383338

384-
if ($this->style !== MenuStyle::getDefaultStyleValues()) {
385-
return $this->buildStyle();
339+
if ($this->style->hasChangedFromDefaults()) {
340+
return $this->style;
386341
}
387342

388343
return $this->parent->getMenuStyle();
389344
}
390345

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-
415346
/**
416347
* @throws RuntimeException
417348
*/
@@ -450,7 +381,7 @@ public function build() : CliMenu
450381
$menuItems = $this->buildSplitItems($mergedItems);
451382
$menuItems = $this->buildSubMenus($menuItems);
452383

453-
$this->style['displaysExtra'] = $this->itemsHaveExtra($menuItems);
384+
$this->style->setDisplaysExtra($this->itemsHaveExtra($menuItems));
454385

455386
$menu = new CliMenu(
456387
$this->menuTitle,

src/Builder/SplitItemBuilder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ class SplitItemBuilder implements Builder
1414
{
1515
use BuilderUtils;
1616

17-
public function __construct(Builder $parent)
17+
/**
18+
* @var CliMenuBuilder
19+
*/
20+
private $parent;
21+
22+
public function __construct(CliMenuBuilder $parent)
1823
{
1924
$this->parent = $parent;
2025
}

src/MenuStyle.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,9 @@ class MenuStyle
167167
'borderBottomWidth' => 0,
168168
'borderLeftWidth' => 0,
169169
'borderColour' => 'white',
170-
'borderColourFallback' => null,
171170
'marginAuto' => false,
172171
];
173172

174-
public static function getDefaultStyleValues() : array
175-
{
176-
return static::$defaultStyleValues;
177-
}
178-
179173
/**
180174
* @var array
181175
*/
@@ -243,10 +237,32 @@ public function __construct(Terminal $terminal = null)
243237
$this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']);
244238
$this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']);
245239
$this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']);
246-
$this->setBorderColour(
247-
static::$defaultStyleValues['borderColour'],
248-
static::$defaultStyleValues['borderColourFallback']
249-
);
240+
$this->setBorderColour(static::$defaultStyleValues['borderColour']);
241+
}
242+
243+
public function hasChangedFromDefaults() : bool
244+
{
245+
$currentValues = [
246+
$this->fg,
247+
$this->bg,
248+
$this->width,
249+
$this->paddingTopBottom,
250+
$this->paddingLeftRight,
251+
$this->margin,
252+
$this->selectedMarker,
253+
$this->unselectedMarker,
254+
$this->itemExtra,
255+
$this->displaysExtra,
256+
$this->titleSeparator,
257+
$this->borderTopWidth,
258+
$this->borderRightWidth,
259+
$this->borderBottomWidth,
260+
$this->borderLeftWidth,
261+
$this->borderColour,
262+
$this->marginAuto,
263+
];
264+
265+
return $currentValues !== array_values(static::$defaultStyleValues);
250266
}
251267

252268
public function getDisabledItemText(string $text) : string

0 commit comments

Comments
 (0)