AbstractStringWrapper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. declare(strict_types=1);
  3. namespace Laminas\Stdlib\StringWrapper;
  4. use Laminas\Stdlib\Exception;
  5. use Laminas\Stdlib\StringUtils;
  6. use function floor;
  7. use function in_array;
  8. use function sprintf;
  9. use function str_pad;
  10. use function str_repeat;
  11. use function strtoupper;
  12. use function wordwrap;
  13. use const STR_PAD_BOTH;
  14. use const STR_PAD_LEFT;
  15. use const STR_PAD_RIGHT;
  16. abstract class AbstractStringWrapper implements StringWrapperInterface
  17. {
  18. /**
  19. * The character encoding working on
  20. *
  21. * @var string|null
  22. */
  23. protected $encoding = 'UTF-8';
  24. /**
  25. * An optionally character encoding to convert to
  26. *
  27. * @var string|null
  28. */
  29. protected $convertEncoding;
  30. /**
  31. * Check if the given character encoding is supported by this wrapper
  32. * and the character encoding to convert to is also supported.
  33. *
  34. * @param string $encoding
  35. * @param string|null $convertEncoding
  36. * @return bool
  37. */
  38. public static function isSupported($encoding, $convertEncoding = null)
  39. {
  40. $supportedEncodings = static::getSupportedEncodings();
  41. if (! in_array(strtoupper($encoding), $supportedEncodings)) {
  42. return false;
  43. }
  44. if ($convertEncoding !== null && ! in_array(strtoupper($convertEncoding), $supportedEncodings)) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. /**
  50. * Set character encoding working with and convert to
  51. *
  52. * @param string $encoding The character encoding to work with
  53. * @param string|null $convertEncoding The character encoding to convert to
  54. * @return StringWrapperInterface
  55. */
  56. public function setEncoding($encoding, $convertEncoding = null)
  57. {
  58. $supportedEncodings = static::getSupportedEncodings();
  59. $encodingUpper = strtoupper($encoding);
  60. if (! in_array($encodingUpper, $supportedEncodings)) {
  61. throw new Exception\InvalidArgumentException(
  62. 'Wrapper doesn\'t support character encoding "' . $encoding . '"'
  63. );
  64. }
  65. if ($convertEncoding !== null) {
  66. $convertEncodingUpper = strtoupper($convertEncoding);
  67. if (! in_array($convertEncodingUpper, $supportedEncodings)) {
  68. throw new Exception\InvalidArgumentException(
  69. 'Wrapper doesn\'t support character encoding "' . $convertEncoding . '"'
  70. );
  71. }
  72. $this->convertEncoding = $convertEncodingUpper;
  73. } else {
  74. $this->convertEncoding = null;
  75. }
  76. $this->encoding = $encodingUpper;
  77. return $this;
  78. }
  79. /**
  80. * Get the defined character encoding to work with
  81. *
  82. * @return null|string
  83. * @throws Exception\LogicException If no encoding was defined.
  84. */
  85. public function getEncoding()
  86. {
  87. return $this->encoding;
  88. }
  89. /**
  90. * Get the defined character encoding to convert to
  91. *
  92. * @return string|null
  93. */
  94. public function getConvertEncoding()
  95. {
  96. return $this->convertEncoding;
  97. }
  98. /**
  99. * Convert a string from defined character encoding to the defined convert encoding
  100. *
  101. * @param string $str
  102. * @param bool $reverse
  103. * @return string|false
  104. */
  105. public function convert($str, $reverse = false)
  106. {
  107. $encoding = $this->getEncoding();
  108. $convertEncoding = $this->getConvertEncoding();
  109. if ($convertEncoding === null) {
  110. throw new Exception\LogicException(
  111. 'No convert encoding defined'
  112. );
  113. }
  114. if ($encoding === $convertEncoding) {
  115. return $str;
  116. }
  117. $from = $reverse ? $convertEncoding : $encoding;
  118. $to = $reverse ? $encoding : $convertEncoding;
  119. throw new Exception\RuntimeException(sprintf(
  120. 'Converting from "%s" to "%s" isn\'t supported by this string wrapper',
  121. $from ?? '',
  122. $to ?? ''
  123. ));
  124. }
  125. /**
  126. * Wraps a string to a given number of characters
  127. *
  128. * @param string $string
  129. * @param int $width
  130. * @param string $break
  131. * @param bool $cut
  132. * @return string|false
  133. */
  134. public function wordWrap($string, $width = 75, $break = "\n", $cut = false)
  135. {
  136. $string = (string) $string;
  137. if ($string === '') {
  138. return '';
  139. }
  140. $break = (string) $break;
  141. if ($break === '') {
  142. throw new Exception\InvalidArgumentException('Break string cannot be empty');
  143. }
  144. $width = (int) $width;
  145. if ($width === 0 && $cut) {
  146. throw new Exception\InvalidArgumentException('Cannot force cut when width is zero');
  147. }
  148. if (null === $this->getEncoding() || StringUtils::isSingleByteEncoding($this->getEncoding())) {
  149. return wordwrap($string, $width, $break, $cut);
  150. }
  151. $stringWidth = $this->strlen($string);
  152. $breakWidth = $this->strlen($break);
  153. $result = '';
  154. $lastStart = $lastSpace = 0;
  155. for ($current = 0; $current < $stringWidth; $current++) {
  156. $char = $this->substr($string, $current, 1);
  157. $possibleBreak = $char;
  158. if ($breakWidth !== 1) {
  159. $possibleBreak = $this->substr($string, $current, $breakWidth);
  160. }
  161. if ($possibleBreak === $break) {
  162. $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth);
  163. $current += $breakWidth - 1;
  164. $lastStart = $lastSpace = $current + 1;
  165. continue;
  166. }
  167. if ($char === ' ') {
  168. if ($current - $lastStart >= $width) {
  169. $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
  170. $lastStart = $current + 1;
  171. }
  172. $lastSpace = $current;
  173. continue;
  174. }
  175. if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
  176. $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
  177. $lastStart = $lastSpace = $current;
  178. continue;
  179. }
  180. if ($current - $lastStart >= $width && $lastStart < $lastSpace) {
  181. $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart) . $break;
  182. $lastStart = $lastSpace += 1;
  183. continue;
  184. }
  185. }
  186. if ($lastStart !== $current) {
  187. $result .= $this->substr($string, $lastStart, $current - $lastStart);
  188. }
  189. return $result;
  190. }
  191. /**
  192. * Pad a string to a certain length with another string
  193. *
  194. * @param string $input
  195. * @param int $padLength
  196. * @param string $padString
  197. * @param int $padType
  198. * @return string
  199. */
  200. public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT)
  201. {
  202. if (null === $this->getEncoding() || StringUtils::isSingleByteEncoding($this->getEncoding())) {
  203. return str_pad($input, $padLength, $padString, $padType);
  204. }
  205. $lengthOfPadding = $padLength - $this->strlen($input);
  206. if ($lengthOfPadding <= 0) {
  207. return $input;
  208. }
  209. $padStringLength = $this->strlen($padString);
  210. if ($padStringLength === 0) {
  211. return $input;
  212. }
  213. $repeatCount = (int) floor($lengthOfPadding / $padStringLength);
  214. if ($padType === STR_PAD_BOTH) {
  215. $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
  216. $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
  217. $lastStringLeftLength = $lastStringRightLength = (int) floor($lastStringLength / 2);
  218. $lastStringRightLength += $lastStringLength % 2;
  219. $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength);
  220. $lastStringRight = $this->substr($padString, 0, $lastStringRightLength);
  221. return str_repeat($padString, $repeatCountLeft) . $lastStringLeft
  222. . $input
  223. . str_repeat($padString, $repeatCountRight) . $lastStringRight;
  224. }
  225. $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength);
  226. if ($padType === STR_PAD_LEFT) {
  227. return str_repeat($padString, $repeatCount) . $lastString . $input;
  228. }
  229. return $input . str_repeat($padString, $repeatCount) . $lastString;
  230. }
  231. }