sfFormatter.class.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfFormatter provides methods to format text to be displayed on a console.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFormatter.class.php 14274 2008-12-23 09:40:12Z FabianLange $
  16. */
  17. class sfFormatter
  18. {
  19. protected
  20. $size = 65;
  21. function __construct($maxLineSize = 65)
  22. {
  23. $this->size = $maxLineSize;
  24. }
  25. /**
  26. * Formats a text according to the given parameters.
  27. *
  28. * @param string $text The test to style
  29. * @param mixed $parameters An array of parameters
  30. * @param stream $stream A stream (default to STDOUT)
  31. *
  32. * @return string The formatted text
  33. */
  34. public function format($text = '', $parameters = array(), $stream = STDOUT)
  35. {
  36. return $text;
  37. }
  38. /**
  39. * Formats a message within a section.
  40. *
  41. * @param string $section The section name
  42. * @param string $text The text message
  43. * @param integer $size The maximum size allowed for a line (65 by default)
  44. */
  45. public function formatSection($section, $text, $size = null)
  46. {
  47. return sprintf(">> %-9s %s", $section, $this->excerpt($text, $size));
  48. }
  49. /**
  50. * Truncates a line.
  51. *
  52. * @param string $text The text
  53. * @param integer $size The maximum size of the returned string (65 by default)
  54. *
  55. * @return string The truncated string
  56. */
  57. public function excerpt($text, $size = null)
  58. {
  59. if (!$size)
  60. {
  61. $size = $this->size;
  62. }
  63. if (strlen($text) < $size)
  64. {
  65. return $text;
  66. }
  67. $subsize = floor(($size - 3) / 2);
  68. return substr($text, 0, $subsize).'...'.substr($text, -$subsize);
  69. }
  70. /**
  71. * Sets the maximum line size.
  72. *
  73. * @param integer $size The maximum line size for a message
  74. */
  75. public function setMaxLineSize($size)
  76. {
  77. $this->size = $size;
  78. }
  79. }