Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit e1fb28e

Browse files
committed
Merge branch 'feature/5711' into develop
Close zendframework/zendframework#5711
161 parents 6f6a702 + c45b896 + 739e942 + 29a4517 + 3179585 + ae205d0 + 6d3ab5a + 5b7a64f + 7591112 + 7610b9a + f91c471 + acef74d + d753e94 + 7a829bf + 3b93376 + 6c2eca4 + 48bc01e + 3c88cd5 + fbfc750 + 441b9de + b57e2b9 + 2beef92 + 3ae4add + 994832b + 6db3cde + e0f83ee + 712223d + 3b4ec35 + e53a267 + 51bf3c7 + 10a1e95 + 30ee303 + 96417b9 + 50ea13f + 24d1c0c + b09295e + 13758aa + 99316f8 + 40b8ac3 + e284e0b + 8ff51b4 + 4b5ea1e + 5b24371 + ed932b6 + 357852d + 262e5d6 + 9057a3f + 0e1e846 + 5d11876 + 0ae0984 + 6ff135c + 0b45aba + 1778b0c + a574713 + ba3f132 + 56365a6 + 019f89a + b09b3ba + 2af32d0 + cb39fc7 + 3f8a6fd + 8999753 + a7cd86b + da1c469 + 5371cdb + fd9f625 + 99d3fcd + 1e3ed17 + a360459 + 1a1e9aa + 4c8955c + 94f2feb + 5b450ef + e64e5b0 + e9d19a6 + a5a9b6e + 5e59c3a + a47f2db + 3cf915a + a2a5c91 + 1cd3439 + ef9c643 + 298a619 + 001ae99 + c37ab8e + 017f6f1 + 0711f77 + 38af804 + 23120b4 + 5f96f53 + a257169 + 8085bc4 + 3110cd4 + f270d41 + 9536f09 + 4ec1292 + adcdd78 + e80fff6 + 99fc5ce + 1d23e94 + fa167cb + a93e92f + 13a2df8 + 06689fb + 2d601eb + 72a3295 + a59f42c + 0f4489d + b3f7029 + 3c492ce + 259bcdc + a811ae5 + f66f985 + 173e949 + b93077e + f11a10d + 5ac4be0 + cb31aff + d34bdbb + 725b978 + 27b423e + 624ac5e + a3340ac + 80ab2e3 + 3429053 + 998a6d7 + 8b9ab71 + d4dffb0 + 5456435 + 97180fa + 126be7f + c16eb72 + 8ccb096 + 50481b9 + 411738a + 50b5c0b + 0721376 + ca3399f + 5abf474 + 7783b57 + a53deb5 + c8885be + 1adf3e3 + 5a45e41 + 3370b44 + b4caa32 + 2a7fc79 + 755e064 + fde42e0 + fb9bce8 + 3ff447c + 479a18b + ef496f7 + e4ffccf + 14f6712 + ffa2c78 + 1b4ee9c + d579ec1 + 8c34fba + e4950e1 + 4c59281 commit e1fb28e

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

src/Adapter/AbstractAdapter.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public function writeBox(
288288
* @param int $y Block Y coordinate (row)
289289
* @param null|int $color (optional) Text color
290290
* @param null|int $bgColor (optional) Text background color
291+
* @throws Exception\InvalidArgumentException
291292
*/
292293
public function writeTextBlock(
293294
$text,
@@ -298,6 +299,43 @@ public function writeTextBlock(
298299
$color = null,
299300
$bgColor = null
300301
) {
302+
if ($x < 0 || $y < 0) {
303+
throw new Exception\InvalidArgumentException('Supplied X,Y coordinates are invalid.');
304+
}
305+
306+
if ($width < 1) {
307+
throw new Exception\InvalidArgumentException('Invalid width supplied.');
308+
}
309+
310+
if (null !== $height && $height < 1) {
311+
throw new Exception\InvalidArgumentException('Invalid height supplied.');
312+
}
313+
314+
// ensure the text is not wider than the width
315+
if (strlen($text) <= $width) {
316+
// just write the line at the spec'd position
317+
$this->setPos($x, $y);
318+
$this->write($text, $color, $bgColor);
319+
return;
320+
}
321+
322+
$text = wordwrap($text, $width, PHP_EOL, true);
323+
324+
// convert to array of lines
325+
$lines = explode(PHP_EOL, $text);
326+
327+
// truncate if height was specified
328+
if (null !== $height && count($lines) > $height) {
329+
$lines = array_slice($lines, 0, $height);
330+
}
331+
332+
// write each line
333+
$curY = $y;
334+
foreach ($lines as $line) {
335+
$this->setPos($x, $curY);
336+
$this->write($line, $color, $bgColor);
337+
$curY++;//next line
338+
}
301339
}
302340

303341
/**

src/Adapter/Posix.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ protected function setTTYMode($mode)
373373
* Get the final color code and throw exception on error
374374
*
375375
* @param null|int|Xterm256 $color
376+
* @param string $type (optional) Foreground 'fg' or background 'bg'.
376377
* @throws Exception\BadMethodCallException
377378
* @return string
378379
*/

test/Adapter/AbstractAdapterTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,72 @@ public function testEncodeText()
144144
$encodedText = $this->adapter->encodeText(utf8_decode($text));
145145
$this->assertEquals(utf8_decode($text),$encodedText);
146146
}
147+
148+
public function testWriteTextBlockSameAsWidth()
149+
{
150+
//set some text that's the same size as the width
151+
$text = 'hello there, I am short!';
152+
$width = strlen($text);
153+
154+
ob_start();
155+
$this->adapter->writeTextBlock($text, $width);
156+
$this->assertSame($text, ob_get_clean());
157+
}
158+
159+
public function testTextBlockLongUnbreakableWord()
160+
{
161+
$text = 'thisisaverylongwordthatwontbreakproperlysothereyouhaveit and here is some more text';
162+
$expected = array('thisisaver', 'ylongwordt', 'hatwontbre', 'akproperly'
163+
, 'sothereyou', 'haveit and', 'here is', 'some more'
164+
, 'text');
165+
166+
ob_start();
167+
$this->adapter->writeTextBlock($text, 10);
168+
$this->assertSame($expected, $this->adapter->writtenData);
169+
170+
//just get rid of the data in ob
171+
ob_get_clean();
172+
}
173+
public function testTextBlockLongerThanHeight()
174+
{
175+
$text = 'thisisaverylongwordthatwontbreakproperlysothereyouhaveit and here is some more text';
176+
$expected = array('thisisaver', 'ylongwordt', 'hatwontbre');
177+
178+
//reset tracking of written data
179+
$this->adapter->writtenData = array();
180+
181+
ob_start();
182+
$this->adapter->writeTextBlock($text, 10, 3);
183+
$this->assertSame($expected, $this->adapter->writtenData);
184+
185+
//just get rid of the data in ob
186+
ob_get_clean();
187+
}
188+
189+
/**
190+
* @expectedException \Zend\Console\Exception\InvalidArgumentException
191+
* @expectedExceptionMessage Supplied X,Y coordinates are invalid.
192+
*/
193+
public function testInvalidCoords()
194+
{
195+
$this->adapter->writeTextBlock('', 1, 1, -1, -9);
196+
}
197+
198+
/**
199+
* @expectedException \Zend\Console\Exception\InvalidArgumentException
200+
* @expectedExceptionMessage Invalid width supplied.
201+
*/
202+
public function testInvalidWidth()
203+
{
204+
$this->adapter->writeTextBlock('', 0);
205+
}
206+
207+
/**
208+
* @expectedException \Zend\Console\Exception\InvalidArgumentException
209+
* @expectedExceptionMessage Invalid height supplied.
210+
*/
211+
public function testInvalidHeight()
212+
{
213+
$this->adapter->writeTextBlock('', 80, 0, 2, 2);
214+
}
147215
}

test/TestAssets/ConsoleAdapter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ConsoleAdapter extends AbstractAdapter
2424

2525
public $testIsUtf8 = true;
2626

27+
public $writtenData = array();
28+
2729
/**
2830
* Read a single line from the console input
2931
*
@@ -86,4 +88,16 @@ public function getWidth()
8688
{
8789
return $this->testWidth;
8890
}
91+
92+
/**
93+
* Tracks exactly what data has been written
94+
* @param string $text
95+
* @param null $color
96+
* @param null $bgColor
97+
*/
98+
public function write($text, $color = null, $bgColor = null)
99+
{
100+
$this->writtenData[] = $text;
101+
parent::write($text, $color, $bgColor);
102+
}
89103
}

0 commit comments

Comments
 (0)