| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfFormatter |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$size = 65; |
|---|
| 23 |
|
|---|
| 24 |
function __construct($maxLineSize = 65) |
|---|
| 25 |
{ |
|---|
| 26 |
$this->size = $maxLineSize; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
* Formats a text according to the given parameters. |
|---|
| 31 |
* |
|---|
| 32 |
* @param string $text The test to style |
|---|
| 33 |
* @param mixed $parameters An array of parameters |
|---|
| 34 |
* @param stream $stream A stream (default to STDOUT) |
|---|
| 35 |
* |
|---|
| 36 |
* @return string The formatted text |
|---|
| 37 |
*/ |
|---|
| 38 |
public function format($text = '', $parameters = array(), $stream = STDOUT) |
|---|
| 39 |
{ |
|---|
| 40 |
return $text; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* Formats a message within a section. |
|---|
| 45 |
* |
|---|
| 46 |
* @param string $section The section name |
|---|
| 47 |
* @param string $text The text message |
|---|
| 48 |
* @param integer $size The maximum size allowed for a line (65 by default) |
|---|
| 49 |
*/ |
|---|
| 50 |
public function formatSection($section, $text, $size = null) |
|---|
| 51 |
{ |
|---|
| 52 |
return sprintf(">> %-9s %s", $section, $this->excerpt($text, $size)); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* Truncates a line. |
|---|
| 57 |
* |
|---|
| 58 |
* @param string $text The text |
|---|
| 59 |
* @param integer $size The maximum size of the returned string (65 by default) |
|---|
| 60 |
* |
|---|
| 61 |
* @return string The truncated string |
|---|
| 62 |
*/ |
|---|
| 63 |
public function excerpt($text, $size = null) |
|---|
| 64 |
{ |
|---|
| 65 |
if (!$size) |
|---|
| 66 |
{ |
|---|
| 67 |
$size = $this->size; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
if (strlen($text) < $size) |
|---|
| 71 |
{ |
|---|
| 72 |
return $text; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
$subsize = floor(($size - 3) / 2); |
|---|
| 76 |
|
|---|
| 77 |
return substr($text, 0, $subsize).'...'.substr($text, -$subsize); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
* Sets the maximum line size. |
|---|
| 82 |
* |
|---|
| 83 |
* @param integer $size The maximum line size for a message |
|---|
| 84 |
*/ |
|---|
| 85 |
public function setMaxLineSize($size) |
|---|
| 86 |
{ |
|---|
| 87 |
$this->size = $size; |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|