|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion; |
|
6
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AsciiArtItem |
|
10
|
|
|
* |
|
11
|
|
|
* @package PhpSchool\CliMenu\MenuItem |
|
12
|
|
|
* @author Michael Woodward <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class AsciiArtItem implements MenuItemInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Possible positions of the ascii art |
|
18
|
|
|
*/ |
|
19
|
|
|
const POSITION_CENTER = 'center'; |
|
20
|
|
|
const POSITION_LEFT = 'left'; |
|
21
|
|
|
const POSITION_RIGHT = 'right'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $text; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $position; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $artLength; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $text |
|
40
|
|
|
* @param string $position |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($text, $position = self::POSITION_CENTER) |
|
43
|
|
|
{ |
|
44
|
|
|
Assertion::string($text); |
|
45
|
|
|
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]); |
|
46
|
|
|
|
|
47
|
|
|
$this->text = $text; |
|
48
|
|
|
$this->position = $position; |
|
49
|
|
|
$this->artLength = max(array_map('mb_strlen', explode("\n", $text))); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The output text for the item |
|
54
|
|
|
* |
|
55
|
|
|
* @param MenuStyle $style |
|
56
|
|
|
* @param bool $selected |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getRows(MenuStyle $style, $selected = false) |
|
60
|
|
|
{ |
|
61
|
|
|
$justificationHelper = new Helper\AsciiArtItemJustificationHelper( |
|
62
|
|
|
$this->position, |
|
63
|
|
|
$style->getContentWidth(), |
|
64
|
|
|
$this->artLength); |
|
65
|
|
|
|
|
66
|
|
|
return array_map(function ($row) use ($justificationHelper) { |
|
67
|
|
|
return $justificationHelper->justifyRow($row); |
|
68
|
|
|
}, explode("\n", $this->text)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Can the item be selected |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function canSelect() |
|
77
|
|
|
{ |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Execute the items callable if required |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getSelectAction() |
|
87
|
|
|
{ |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return the raw string of text |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getText() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->text; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Whether or not the menu item is showing the menustyle extra value |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public function showsItemExtra() |
|
107
|
|
|
{ |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|