Skip to content

Commit 50eb732

Browse files
authored
Merge pull request #105 from php-school/ascii-art-refactor
Ascii art refactor
2 parents 5c0b307 + 6dc3436 commit 50eb732

File tree

2 files changed

+108
-36
lines changed

2 files changed

+108
-36
lines changed

src/MenuItem/AsciiArtItem.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public function __construct(string $text, string $position = self::POSITION_CENT
4141
{
4242
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);
4343

44-
$this->text = $text;
44+
$this->text = implode("\n", array_map(function (string $line) {
45+
return rtrim($line, ' ');
46+
}, explode("\n", $text)));
4547
$this->position = $position;
4648
$this->alternateText = $alt;
4749
$this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
@@ -57,26 +59,19 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
5759
return $alternate->getRows($style, false);
5860
}
5961

60-
return array_map(function ($row) use ($style) {
61-
$length = mb_strlen($row);
62-
63-
$padding = $style->getContentWidth() - $length;
64-
62+
$padding = $style->getContentWidth() - $this->artLength;
63+
64+
return array_map(function ($row) use ($padding) {
6565
switch ($this->position) {
6666
case self::POSITION_LEFT:
67-
return $row;
6867
break;
6968
case self::POSITION_RIGHT:
70-
$row = rtrim($row);
71-
$padding = $padding - ($this->artLength - mb_strlen($row));
7269
$row = sprintf('%s%s', str_repeat(' ', $padding), $row);
7370
break;
7471
case self::POSITION_CENTER:
7572
default:
76-
$padding = $padding - ($this->artLength - $length);
77-
$left = ceil($padding/2);
78-
$right = $padding - $left;
79-
$row = sprintf('%s%s%s', str_repeat(' ', $left), $row, str_repeat(' ', $right));
73+
$left = ceil($padding / 2);
74+
$row = sprintf('%s%s', str_repeat(' ', $left), $row);
8075
break;
8176
}
8277

test/MenuItem/AsciiArtItemTest.php

Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,8 @@ public function testGetRowsCenterAligned() : void
9191
$item = new AsciiArtItem("//\n//", AsciiArtItem::POSITION_CENTER);
9292
$this->assertEquals(
9393
[
94-
" // ",
95-
" // ",
96-
],
97-
$item->getRows($menuStyle)
98-
);
99-
100-
$item = new AsciiArtItem(" // \n//////////", AsciiArtItem::POSITION_CENTER);
101-
$this->assertEquals(
102-
[
103-
" // ",
104-
"//////////",
94+
" //",
95+
" //",
10596
],
10697
$item->getRows($menuStyle)
10798
);
@@ -119,17 +110,8 @@ public function testGetRowsCenterAlignedWithOddWidth() : void
119110
$item = new AsciiArtItem("//\n//", AsciiArtItem::POSITION_CENTER);
120111
$this->assertEquals(
121112
[
122-
" // ",
123-
" // ",
124-
],
125-
$item->getRows($menuStyle)
126-
);
127-
128-
$item = new AsciiArtItem(" // \n//////////", AsciiArtItem::POSITION_CENTER);
129-
$this->assertEquals(
130-
[
131-
" // ",
132-
" //////////",
113+
" //",
114+
" //",
133115
],
134116
$item->getRows($menuStyle)
135117
);
@@ -146,7 +128,7 @@ public function testHideAndShowItemExtraHasNoEffect() : void
146128
$this->assertFalse($item->showsItemExtra());
147129
}
148130

149-
public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall()
131+
public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall() : void
150132
{
151133
$menuStyle = $this->createMock(MenuStyle::class);
152134

@@ -159,4 +141,99 @@ public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall()
159141

160142
self::assertSame(['my alt'], $item->getRows($menuStyle));
161143
}
144+
145+
public function testWithRealAsciiArtCenterAligned() : void
146+
{
147+
$menuStyle = $this->createMock(MenuStyle::class);
148+
149+
$menuStyle
150+
->expects($this->any())
151+
->method('getContentWidth')
152+
->will($this->returnValue(30));
153+
154+
$art = <<<ART
155+
_ __ _
156+
/ |..| \
157+
\/ || \/
158+
|_''_|
159+
PHP SCHOOL
160+
LEARNING FOR ELEPHANTS
161+
ART;
162+
163+
$item = new AsciiArtItem($art, AsciiArtItem::POSITION_CENTER);
164+
$this->assertEquals(
165+
[
166+
' _ __ _',
167+
' / |..| \\',
168+
' \/ || \/',
169+
" |_''_|",
170+
' PHP SCHOOL',
171+
' LEARNING FOR ELEPHANTS'
172+
],
173+
$item->getRows($menuStyle)
174+
);
175+
}
176+
177+
public function testWithRealAsciiArtRightAligned() : void
178+
{
179+
$menuStyle = $this->createMock(MenuStyle::class);
180+
181+
$menuStyle
182+
->expects($this->any())
183+
->method('getContentWidth')
184+
->will($this->returnValue(30));
185+
186+
$art = <<<ART
187+
_ __ _
188+
/ |..| \
189+
\/ || \/
190+
|_''_|
191+
PHP SCHOOL
192+
LEARNING FOR ELEPHANTS
193+
ART;
194+
195+
$item = new AsciiArtItem($art, AsciiArtItem::POSITION_RIGHT);
196+
$this->assertEquals(
197+
[
198+
' _ __ _',
199+
' / |..| \\',
200+
' \/ || \/',
201+
" |_''_|",
202+
' PHP SCHOOL',
203+
' LEARNING FOR ELEPHANTS'
204+
],
205+
$item->getRows($menuStyle)
206+
);
207+
}
208+
209+
public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLine() : void
210+
{
211+
$menuStyle = $this->createMock(MenuStyle::class);
212+
213+
$menuStyle
214+
->expects($this->any())
215+
->method('getContentWidth')
216+
->will($this->returnValue(30));
217+
218+
$art = <<<ART
219+
_ __ _
220+
/ |..| \
221+
\/ || \/
222+
|_''_|
223+
PHP SCHOOL
224+
LEARNING FOR ELEPHANTS
225+
ART;
226+
$item = new AsciiArtItem($art, AsciiArtItem::POSITION_CENTER);
227+
$this->assertEquals(
228+
[
229+
' _ __ _',
230+
' / |..| \\',
231+
' \/ || \/',
232+
" |_''_|",
233+
' PHP SCHOOL',
234+
' LEARNING FOR ELEPHANTS'
235+
],
236+
$item->getRows($menuStyle)
237+
);
238+
}
162239
}

0 commit comments

Comments
 (0)