1 | <?php |
||
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 string |
||
32 | */ |
||
33 | private $alternateText; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $artLength; |
||
39 | |||
40 | public function __construct(string $text, string $position = self::POSITION_CENTER, string $alt = '') |
||
41 | { |
||
42 | Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]); |
||
43 | |||
44 | $this->setText($text); |
||
45 | $this->position = $position; |
||
46 | $this->alternateText = $alt; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * The output text for the item |
||
51 | */ |
||
52 | public function getRows(MenuStyle $style, bool $selected = false) : array |
||
53 | { |
||
54 | if ($this->artLength > $style->getContentWidth()) { |
||
55 | $alternate = new StaticItem($this->alternateText); |
||
56 | return $alternate->getRows($style, false); |
||
57 | } |
||
58 | |||
59 | $padding = $style->getContentWidth() - $this->artLength; |
||
60 | |||
61 | return array_map(function ($row) use ($padding) { |
||
62 | switch ($this->position) { |
||
63 | case self::POSITION_LEFT: |
||
64 | break; |
||
65 | case self::POSITION_RIGHT: |
||
66 | $row = sprintf('%s%s', str_repeat(' ', $padding), $row); |
||
67 | break; |
||
68 | case self::POSITION_CENTER: |
||
69 | default: |
||
70 | $left = ceil($padding / 2); |
||
71 | $row = sprintf('%s%s', str_repeat(' ', $left), $row); |
||
72 | break; |
||
73 | } |
||
74 | |||
75 | return $row; |
||
76 | }, explode("\n", $this->text)); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Can the item be selected |
||
81 | */ |
||
82 | public function canSelect() : bool |
||
83 | { |
||
84 | return false; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Execute the items callable if required |
||
89 | */ |
||
90 | public function getSelectAction() : ?callable |
||
91 | { |
||
92 | return null; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return the raw string of text |
||
97 | */ |
||
98 | public function getText() : string |
||
99 | { |
||
100 | return $this->text; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Set the raw string of text |
||
105 | */ |
||
106 | public function setText(string $text) : void |
||
107 | { |
||
108 | $this->text = implode("\n", array_map(function (string $line) { |
||
109 | return rtrim($line, ' '); |
||
110 | }, explode("\n", $text))); |
||
111 | |||
112 | $this->calculateArtLength(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Calculate the length of the art |
||
117 | */ |
||
118 | private function calculateArtLength() : void |
||
119 | { |
||
120 | $this->artLength = max(array_map('mb_strlen', explode("\n", $this->text))); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Return the length of the art |
||
125 | */ |
||
126 | public function getArtLength() : int |
||
127 | { |
||
128 | return $this->artLength; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Whether or not the menu item is showing the menustyle extra value |
||
133 | */ |
||
134 | public function showsItemExtra() : bool |
||
138 | |||
139 | /** |
||
140 | * Enable showing item extra |
||
141 | */ |
||
142 | public function showItemExtra() : void |
||
143 | { |
||
144 | //noop |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Disable showing item extra |
||
149 | */ |
||
150 | public function hideItemExtra() : void |
||
154 | } |
||
155 |