3 declare(strict_types=1);
5 namespace Laminas\Stdlib\StringWrapper;
7 use Laminas\Stdlib\Exception;
8 use Laminas\Stdlib\StringUtils;
11 use function in_array;
14 use function str_repeat;
15 use function strtoupper;
16 use function wordwrap;
18 use const STR_PAD_BOTH;
19 use const STR_PAD_LEFT;
20 use const STR_PAD_RIGHT;
22 abstract class AbstractStringWrapper implements StringWrapperInterface
25 * The character encoding working on
29 protected $encoding = 'UTF-8';
32 * An optionally character encoding to convert to
36 protected $convertEncoding;
39 * Check if the given character encoding is supported by this wrapper
40 * and the character encoding to convert to is also supported.
42 * @param string $encoding
43 * @param string|null $convertEncoding
46 public static function isSupported($encoding, $convertEncoding = null)
48 $supportedEncodings = static::getSupportedEncodings();
50 if (! in_array(strtoupper($encoding), $supportedEncodings)) {
54 if ($convertEncoding !== null && ! in_array(strtoupper($convertEncoding), $supportedEncodings)) {
62 * Set character encoding working with and convert to
64 * @param string $encoding The character encoding to work with
65 * @param string|null $convertEncoding The character encoding to convert to
66 * @return StringWrapperInterface
68 public function setEncoding($encoding, $convertEncoding = null)
70 $supportedEncodings = static::getSupportedEncodings();
72 $encodingUpper = strtoupper($encoding);
73 if (! in_array($encodingUpper, $supportedEncodings)) {
74 throw new Exception\InvalidArgumentException(
75 'Wrapper doesn\'t support character encoding "' . $encoding . '"'
79 if ($convertEncoding !== null) {
80 $convertEncodingUpper = strtoupper($convertEncoding);
81 if (! in_array($convertEncodingUpper, $supportedEncodings)) {
82 throw new Exception\InvalidArgumentException(
83 'Wrapper doesn\'t support character encoding "' . $convertEncoding . '"'
87 $this->convertEncoding = $convertEncodingUpper;
89 $this->convertEncoding = null;
91 $this->encoding = $encodingUpper;
97 * Get the defined character encoding to work with
100 * @throws Exception\LogicException If no encoding was defined.
102 public function getEncoding()
104 return $this->encoding;
108 * Get the defined character encoding to convert to
110 * @return string|null
112 public function getConvertEncoding()
114 return $this->convertEncoding;
118 * Convert a string from defined character encoding to the defined convert encoding
121 * @param bool $reverse
122 * @return string|false
124 public function convert($str, $reverse = false)
126 $encoding = $this->getEncoding();
127 $convertEncoding = $this->getConvertEncoding();
128 if ($convertEncoding === null) {
129 throw new Exception\LogicException(
130 'No convert encoding defined'
134 if ($encoding === $convertEncoding) {
138 $from = $reverse ? $convertEncoding : $encoding;
139 $to = $reverse ? $encoding : $convertEncoding;
140 throw new Exception\RuntimeException(sprintf(
141 'Converting from "%s" to "%s" isn\'t supported by this string wrapper',
148 * Wraps a string to a given number of characters
150 * @param string $string
152 * @param string $break
154 * @return string|false
156 public function wordWrap($string, $width = 75, $break = "\n", $cut = false)
158 $string = (string) $string;
159 if ($string === '') {
163 $break = (string) $break;
165 throw new Exception\InvalidArgumentException('Break string cannot be empty');
168 $width = (int) $width;
169 if ($width === 0 && $cut) {
170 throw new Exception\InvalidArgumentException('Cannot force cut when width is zero');
173 if (null === $this->getEncoding() || StringUtils::isSingleByteEncoding($this->getEncoding())) {
174 return wordwrap($string, $width, $break, $cut);
177 $stringWidth = $this->strlen($string);
178 $breakWidth = $this->strlen($break);
181 $lastStart = $lastSpace = 0;
183 for ($current = 0; $current < $stringWidth; $current++) {
184 $char = $this->substr($string, $current, 1);
186 $possibleBreak = $char;
187 if ($breakWidth !== 1) {
188 $possibleBreak = $this->substr($string, $current, $breakWidth);
191 if ($possibleBreak === $break) {
192 $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth);
193 $current += $breakWidth - 1;
194 $lastStart = $lastSpace = $current + 1;
199 if ($current - $lastStart >= $width) {
200 $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
201 $lastStart = $current + 1;
204 $lastSpace = $current;
208 if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
209 $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
210 $lastStart = $lastSpace = $current;
214 if ($current - $lastStart >= $width && $lastStart < $lastSpace) {
215 $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart) . $break;
216 $lastStart = $lastSpace += 1;
221 if ($lastStart !== $current) {
222 $result .= $this->substr($string, $lastStart, $current - $lastStart);
229 * Pad a string to a certain length with another string
231 * @param string $input
232 * @param int $padLength
233 * @param string $padString
234 * @param int $padType
237 public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT)
239 if (null === $this->getEncoding() || StringUtils::isSingleByteEncoding($this->getEncoding())) {
240 return str_pad($input, $padLength, $padString, $padType);
243 $lengthOfPadding = $padLength - $this->strlen($input);
244 if ($lengthOfPadding <= 0) {
248 $padStringLength = $this->strlen($padString);
249 if ($padStringLength === 0) {
253 $repeatCount = (int) floor($lengthOfPadding / $padStringLength);
255 if ($padType === STR_PAD_BOTH) {
256 $repeatCountLeft = $repeatCountRight = (int) ($repeatCount - $repeatCount % 2) / 2;
258 $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
259 $lastStringLeftLength = $lastStringRightLength = (int) floor($lastStringLength / 2);
260 $lastStringRightLength += $lastStringLength % 2;
262 $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength);
263 $lastStringRight = $this->substr($padString, 0, $lastStringRightLength);
265 return str_repeat($padString, $repeatCountLeft) . $lastStringLeft
267 . str_repeat($padString, $repeatCountRight) . $lastStringRight;
270 $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength);
272 if ($padType === STR_PAD_LEFT) {
273 return str_repeat($padString, $repeatCount) . $lastString . $input;
276 return $input . str_repeat($padString, $repeatCount) . $lastString;