/src/PHPFrame/Base/String.php

https://github.com/willvdmerwe/PHPFrame · PHP · 206 lines · 75 code · 18 blank · 113 comment · 8 complexity · 1952b6cea1812871cff45f594d2ff718 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPFrame/Base/String.php
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHPFrame
  8. * @package Base
  9. * @author Lupo Montero <lupo@e-noise.com>
  10. * @copyright 2010 The PHPFrame Group
  11. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  12. * @link http://github.com/PHPFrame/PHPFrame
  13. */
  14. /**
  15. * This class provides objects used to represent strings in an Object Oriented
  16. * context.
  17. *
  18. * @category PHPFrame
  19. * @package Base
  20. * @author Lupo Montero <lupo@e-noise.com>
  21. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  22. * @link http://github.com/PHPFrame/PHPFrame
  23. * @since 1.0
  24. */
  25. class PHPFrame_String
  26. {
  27. /**
  28. * Private propery used to store the string as a primitive value
  29. *
  30. * @var string
  31. */
  32. private $_str;
  33. /**
  34. * Constructor
  35. *
  36. * @param string $str The string the object will represent.
  37. *
  38. * @return void
  39. * @since 1.0
  40. */
  41. public function __construct($str)
  42. {
  43. $this->_str = trim((string) $str);
  44. }
  45. /**
  46. * Magic method called we try to use a string object as a string
  47. *
  48. * @return string
  49. * @since 1.0
  50. */
  51. public function __toString()
  52. {
  53. return $this->_str;
  54. }
  55. /**
  56. * Get string length
  57. *
  58. * @return int
  59. * @since 1.0
  60. */
  61. public function len()
  62. {
  63. return strlen($this->_str);
  64. }
  65. /**
  66. * Get string in upper case
  67. *
  68. * @return string
  69. * @since 1.0
  70. */
  71. public function upper()
  72. {
  73. return strtoupper($this->_str);
  74. }
  75. /**
  76. * Get string in lower case
  77. *
  78. * @return string
  79. * @since 1.0
  80. */
  81. public function lower()
  82. {
  83. return strtolower($this->_str);
  84. }
  85. /**
  86. * Get string with first character in upper case
  87. *
  88. * @return string
  89. * @since 1.0
  90. */
  91. public function upperFirst()
  92. {
  93. return ucfirst(strtolower($this->_str));
  94. }
  95. /**
  96. * Get string with first character in every word in upper case
  97. *
  98. * @return string
  99. * @since 1.0
  100. */
  101. public function upperWords()
  102. {
  103. return ucwords(strtolower($this->_str));
  104. }
  105. /**
  106. * Limit string to a set number of characters
  107. *
  108. * @param int $max_chars Number of characters we want to limit to.
  109. * @param bool $add_trailing_dots [Optional] Boolean to indicate whether we
  110. * want to add trailing dots or not. Default
  111. * is TRUE.
  112. *
  113. * @return string
  114. * @since 1.0
  115. */
  116. public function limitChars($max_chars, $add_trailing_dots=true)
  117. {
  118. $str = $this->_str;
  119. if (strlen($str) > $max_chars) {
  120. $str = substr($str, 0, $max_chars);
  121. if ($add_trailing_dots === true) {
  122. // Remove another 4 chars to replace with dots
  123. $str = substr($str, 0, (strlen($str)-3));
  124. $str .= "...";
  125. }
  126. }
  127. return $str;
  128. }
  129. /**
  130. * Limit the number of words.
  131. *
  132. * @param int $max_chars Number of characters we want to limit to
  133. * @param bool $add_trailing_dots [Optional] Boolean to indicate whether we
  134. * want to add trailing dots or not. Default
  135. * is TRUE.
  136. *
  137. * @return string
  138. * @since 1.0
  139. */
  140. public function limitWords($max_chars, $add_trailing_dots=true)
  141. {
  142. $str = $this->_str;
  143. if (strlen($str) > $max_chars) {
  144. $str = substr($str, 0, $max_chars);
  145. $str = substr($str, 0, strrpos($str, " "));
  146. if ($add_trailing_dots === true) {
  147. $str .= " ...";
  148. }
  149. }
  150. return $str;
  151. }
  152. /**
  153. * This method is used to format the string into the given length.
  154. * If the string is longer than the specified length it is trimmed to fit.
  155. * If the string is shorter than the specified length it is padded with spaces
  156. * on the left side to fit length.
  157. *
  158. * @param int $length Length we want to format the string to.
  159. * @param bool $add_trailing_dots [Optional] Boolean to indicate whether we
  160. * want to add trailing dots or not. Default
  161. * is TRUE.
  162. *
  163. * @return string
  164. * @since 1.0
  165. */
  166. public function fixLength($length, $add_trailing_dots=true)
  167. {
  168. // Cast input params to strict types
  169. $length = (int) $length;
  170. $add_trailing_dots = (bool) $add_trailing_dots;
  171. $str = $this->_str;
  172. if (strlen($str) > $length) {
  173. // Trim to fixed length
  174. $str = substr($str, 0, $length);
  175. // Add trailing dots if necessary
  176. if ($add_trailing_dots) {
  177. $str = substr($str, 0, ($length-3));
  178. $str .= "...";
  179. }
  180. } else {
  181. // Add space padding
  182. for ($i=0; $i<=($length - strlen($str)); $i++) {
  183. $str .= " ";
  184. }
  185. }
  186. return $str;
  187. }
  188. }