3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib\StringWrapper;
12 use Zend\Stdlib\Exception;
13 use Zend\Stdlib\StringUtils;
15 abstract class AbstractStringWrapper implements StringWrapperInterface
18 * The character encoding working on
21 protected $encoding = 'UTF-8';
24 * An optionally character encoding to convert to
27 protected $convertEncoding;
30 * Check if the given character encoding is supported by this wrapper
31 * and the character encoding to convert to is also supported.
33 * @param string $encoding
34 * @param string|null $convertEncoding
37 public static function isSupported($encoding, $convertEncoding = null)
39 $supportedEncodings = static::getSupportedEncodings();
41 if (! in_array(strtoupper($encoding), $supportedEncodings)) {
45 if ($convertEncoding !== null && ! in_array(strtoupper($convertEncoding), $supportedEncodings)) {
53 * Set character encoding working with and convert to
55 * @param string $encoding The character encoding to work with
56 * @param string|null $convertEncoding The character encoding to convert to
57 * @return StringWrapperInterface
59 public function setEncoding($encoding, $convertEncoding = null)
61 $supportedEncodings = static::getSupportedEncodings();
63 $encodingUpper = strtoupper($encoding);
64 if (! in_array($encodingUpper, $supportedEncodings)) {
65 throw new Exception\InvalidArgumentException(
66 'Wrapper doesn\'t support character encoding "' . $encoding . '"'
70 if ($convertEncoding !== null) {
71 $convertEncodingUpper = strtoupper($convertEncoding);
72 if (! in_array($convertEncodingUpper, $supportedEncodings)) {
73 throw new Exception\InvalidArgumentException(
74 'Wrapper doesn\'t support character encoding "' . $convertEncoding . '"'
78 $this->convertEncoding = $convertEncodingUpper;
80 $this->convertEncoding = null;
82 $this->encoding = $encodingUpper;
88 * Get the defined character encoding to work with
91 * @throws Exception\LogicException If no encoding was defined
93 public function getEncoding()
95 return $this->encoding;
99 * Get the defined character encoding to convert to
101 * @return string|null
103 public function getConvertEncoding()
105 return $this->convertEncoding;
109 * Convert a string from defined character encoding to the defined convert encoding
112 * @param bool $reverse
113 * @return string|false
115 public function convert($str, $reverse = false)
117 $encoding = $this->getEncoding();
118 $convertEncoding = $this->getConvertEncoding();
119 if ($convertEncoding === null) {
120 throw new Exception\LogicException(
121 'No convert encoding defined'
125 if ($encoding === $convertEncoding) {
129 $from = $reverse ? $convertEncoding : $encoding;
130 $to = $reverse ? $encoding : $convertEncoding;
131 throw new Exception\RuntimeException(sprintf(
132 'Converting from "%s" to "%s" isn\'t supported by this string wrapper',
139 * Wraps a string to a given number of characters
141 * @param string $string
143 * @param string $break
145 * @return string|false
147 public function wordWrap($string, $width = 75, $break = "\n", $cut = false)
149 $string = (string) $string;
150 if ($string === '') {
154 $break = (string) $break;
156 throw new Exception\InvalidArgumentException('Break string cannot be empty');
159 $width = (int) $width;
160 if ($width === 0 && $cut) {
161 throw new Exception\InvalidArgumentException('Cannot force cut when width is zero');
164 if (StringUtils::isSingleByteEncoding($this->getEncoding())) {
165 return wordwrap($string, $width, $break, $cut);
168 $stringWidth = $this->strlen($string);
169 $breakWidth = $this->strlen($break);
172 $lastStart = $lastSpace = 0;
174 for ($current = 0; $current < $stringWidth; $current++) {
175 $char = $this->substr($string, $current, 1);
177 $possibleBreak = $char;
178 if ($breakWidth !== 1) {
179 $possibleBreak = $this->substr($string, $current, $breakWidth);
182 if ($possibleBreak === $break) {
183 $result .= $this->substr($string, $lastStart, $current - $lastStart + $breakWidth);
184 $current += $breakWidth - 1;
185 $lastStart = $lastSpace = $current + 1;
190 if ($current - $lastStart >= $width) {
191 $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
192 $lastStart = $current + 1;
195 $lastSpace = $current;
199 if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
200 $result .= $this->substr($string, $lastStart, $current - $lastStart) . $break;
201 $lastStart = $lastSpace = $current;
205 if ($current - $lastStart >= $width && $lastStart < $lastSpace) {
206 $result .= $this->substr($string, $lastStart, $lastSpace - $lastStart) . $break;
207 $lastStart = $lastSpace = $lastSpace + 1;
212 if ($lastStart !== $current) {
213 $result .= $this->substr($string, $lastStart, $current - $lastStart);
220 * Pad a string to a certain length with another string
222 * @param string $input
223 * @param int $padLength
224 * @param string $padString
225 * @param int $padType
228 public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT)
230 if (StringUtils::isSingleByteEncoding($this->getEncoding())) {
231 return str_pad($input, $padLength, $padString, $padType);
234 $lengthOfPadding = $padLength - $this->strlen($input);
235 if ($lengthOfPadding <= 0) {
239 $padStringLength = $this->strlen($padString);
240 if ($padStringLength === 0) {
244 $repeatCount = floor($lengthOfPadding / $padStringLength);
246 if ($padType === STR_PAD_BOTH) {
247 $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
249 $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
250 $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
251 $lastStringRightLength += $lastStringLength % 2;
253 $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength);
254 $lastStringRight = $this->substr($padString, 0, $lastStringRightLength);
256 return str_repeat($padString, $repeatCountLeft) . $lastStringLeft
258 . str_repeat($padString, $repeatCountRight) . $lastStringRight;
261 $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength);
263 if ($padType === STR_PAD_LEFT) {
264 return str_repeat($padString, $repeatCount) . $lastString . $input;
267 return $input . str_repeat($padString, $repeatCount) . $lastString;