Skip to content

Commit 5909ec5

Browse files
authored
Merge pull request #153 from php-school/add-set-text
Add setText to Items + small AsciiArt fix
2 parents 3061311 + 483eaeb commit 5909ec5

File tree

9 files changed

+149
-4
lines changed

9 files changed

+149
-4
lines changed

src/MenuItem/AsciiArtItem.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +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 = implode("\n", array_map(function (string $line) {
45-
return rtrim($line, ' ');
46-
}, explode("\n", $text)));
44+
$this->setText($text);
4745
$this->position = $position;
4846
$this->alternateText = $alt;
49-
$this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
5047
}
5148

5249
/**
@@ -103,6 +100,26 @@ public function getText() : string
103100
return $this->text;
104101
}
105102

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+
106123
/**
107124
* Return the length of the art
108125
*/

src/MenuItem/LineBreakItem.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function getText() : string
6464
return $this->breakChar;
6565
}
6666

67+
/**
68+
* Set the raw string of text
69+
*/
70+
public function setText(string $text) : void
71+
{
72+
$this->breakChar = $text;
73+
}
74+
6775
/**
6876
* Whether or not the menu item is showing the menustyle extra value
6977
*/

src/MenuItem/MenuMenuItem.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function getText() : string
3939
{
4040
return $this->text;
4141
}
42+
43+
/**
44+
* Set the raw string of text
45+
*/
46+
public function setText(string $text) : void
47+
{
48+
$this->text = $text;
49+
}
4250

4351
/**
4452
* Returns the sub menu

src/MenuItem/SelectableItem.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ public function getText() : string
4141
{
4242
return $this->text;
4343
}
44+
45+
/**
46+
* Set the raw string of text
47+
*/
48+
public function setText(string $text) : void
49+
{
50+
$this->text = $text;
51+
}
4452
}

src/MenuItem/StaticItem.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public function getText() : string
5353
return $this->text;
5454
}
5555

56+
/**
57+
* Set the raw string of text
58+
*/
59+
public function setText(string $text) : void
60+
{
61+
$this->text = $text;
62+
}
63+
5664
/**
5765
* Whether or not the menu item is showing the menustyle extra value
5866
*/

test/MenuItem/AsciiArtItemTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall() : void
142142
self::assertSame(['my alt'], $item->getRows($menuStyle));
143143
}
144144

145+
public function testGetRowsDoesNotReturnsStaticAltItemWhenOnlySpacesOverflow() : void
146+
{
147+
$menuStyle = $this->createMock(MenuStyle::class);
148+
149+
$menuStyle
150+
->expects($this->any())
151+
->method('getContentWidth')
152+
->will($this->returnValue(15));
153+
154+
$item = new AsciiArtItem('NOT TOO LONG ', AsciiArtItem::POSITION_LEFT, 'my alt');
155+
156+
self::assertSame(['NOT TOO LONG'], $item->getRows($menuStyle));
157+
}
158+
145159
public function testWithRealAsciiArtCenterAligned() : void
146160
{
147161
$menuStyle = $this->createMock(MenuStyle::class);
@@ -236,4 +250,37 @@ public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLin
236250
$item->getRows($menuStyle)
237251
);
238252
}
253+
254+
public function testSetText() : void
255+
{
256+
$menuStyle = $this->createMock(MenuStyle::class);
257+
258+
$menuStyle
259+
->expects($this->any())
260+
->method('getContentWidth')
261+
->will($this->returnValue(30));
262+
263+
$art = <<<ART
264+
_ __ _
265+
/ |..| \
266+
\/ || \/
267+
|_''_|
268+
PHP SCHOOL
269+
LEARNING FOR ELEPHANTS
270+
ART;
271+
$item = new AsciiArtItem("//", AsciiArtItem::POSITION_CENTER);
272+
$item->setText($art);
273+
274+
$this->assertEquals(
275+
[
276+
' _ __ _',
277+
' / |..| \\',
278+
' \/ || \/',
279+
" |_''_|",
280+
' PHP SCHOOL',
281+
' LEARNING FOR ELEPHANTS'
282+
],
283+
$item->getRows($menuStyle)
284+
);
285+
}
239286
}

test/MenuItem/LineBreakItemTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ public function testGetRowsWithMultiByteChars() : void
9090
$this->assertEquals(['❅❅❅❅❅', '❅❅❅❅❅'], $item->getRows($menuStyle));
9191
}
9292

93+
public function testSetText() : void
94+
{
95+
$menuStyle = $this->createMock(MenuStyle::class);
96+
97+
$menuStyle
98+
->expects($this->any())
99+
->method('getContentWidth')
100+
->will($this->returnValue(5));
101+
102+
$item = new LineBreakItem('ABC', 2);
103+
$item->setText('❅-');
104+
$this->assertEquals(['❅-❅-❅', '❅-❅-❅'], $item->getRows($menuStyle));
105+
}
106+
93107
public function testHideAndShowItemExtraHasNoEffect() : void
94108
{
95109
$item = new LineBreakItem('*');

test/MenuItem/SelectableItemTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ public function testGetRows() : void
6060
$this->assertEquals([' Item'], $item->getRows($menuStyle, true));
6161
}
6262

63+
public function testSetText() : void
64+
{
65+
$menuStyle = $this->createMock(MenuStyle::class);
66+
67+
$menuStyle
68+
->expects($this->any())
69+
->method('getContentWidth')
70+
->will($this->returnValue(10));
71+
72+
$item = new SelectableItem('Item', function () {
73+
});
74+
$item->setText('New Text');
75+
$this->assertEquals([' New Text'], $item->getRows($menuStyle));
76+
$this->assertEquals([' New Text'], $item->getRows($menuStyle, false));
77+
$this->assertEquals([' New Text'], $item->getRows($menuStyle, true));
78+
}
79+
6380
public function testGetRowsWithUnSelectedMarker() : void
6481
{
6582
$menuStyle = $this->createMock(MenuStyle::class);

test/MenuItem/StaticItemTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped() : void
6969
);
7070
}
7171

72+
public function testSetText() : void
73+
{
74+
$menuStyle = $this->createMock(MenuStyle::class);
75+
76+
$menuStyle
77+
->expects($this->once())
78+
->method('getContentWidth')
79+
->will($this->returnValue(10));
80+
81+
$item = new StaticItem('CONTENT');
82+
$item->setText('CONTENT 2 LINES');
83+
84+
$this->assertEquals(
85+
['CONTENT 2', 'LINES'],
86+
$item->getRows($menuStyle)
87+
);
88+
}
89+
7290
public function testHideAndShowItemExtraHasNoEffect() : void
7391
{
7492
$item = new StaticItem('CONTENT 1 LINE');

0 commit comments

Comments
 (0)