sfFormatter.class.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 17858 2009-05-01 21:22:50Z 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. }