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 class Native
extends AbstractStringWrapper
18 * The character encoding working on
19 * (overwritten to change defaut encoding)
23 protected $encoding = 'ASCII';
26 * Check if the given character encoding is supported by this wrapper
27 * and the character encoding to convert to is also supported.
29 * @param string $encoding
30 * @param string|null $convertEncoding
33 public static function isSupported($encoding, $convertEncoding = null)
35 $encodingUpper = strtoupper($encoding);
36 $supportedEncodings = static::getSupportedEncodings();
38 if (!in_array($encodingUpper, $supportedEncodings)) {
42 // This adapter doesn't support to convert between encodings
43 if ($convertEncoding !== null && $encodingUpper !== strtoupper($convertEncoding)) {
51 * Get a list of supported character encodings
55 public static function getSupportedEncodings()
57 return StringUtils
::getSingleByteEncodings();
61 * Set character encoding working with and convert to
63 * @param string $encoding The character encoding to work with
64 * @param string|null $convertEncoding The character encoding to convert to
65 * @return StringWrapperInterface
67 public function setEncoding($encoding, $convertEncoding = null)
69 $supportedEncodings = static::getSupportedEncodings();
71 $encodingUpper = strtoupper($encoding);
72 if (!in_array($encodingUpper, $supportedEncodings)) {
73 throw new Exception\
InvalidArgumentException(
74 'Wrapper doesn\'t support character encoding "' . $encoding . '"'
78 if ($encodingUpper !== strtoupper($convertEncoding)) {
79 $this->convertEncoding
= $encodingUpper;
82 if ($convertEncoding !== null) {
83 if ($encodingUpper !== strtoupper($convertEncoding)) {
84 throw new Exception\
InvalidArgumentException(
85 'Wrapper doesn\'t support to convert between character encodings'
89 $this->convertEncoding
= $encodingUpper;
91 $this->convertEncoding
= null;
93 $this->encoding
= $encodingUpper;
99 * Returns the length of the given string
104 public function strlen($str)
110 * Returns the portion of string specified by the start and length parameters
114 * @param int|null $length
115 * @return string|false
117 public function substr($str, $offset = 0, $length = null)
119 return substr($str, $offset, $length);
123 * Find the position of the first occurrence of a substring in a string
125 * @param string $haystack
126 * @param string $needle
130 public function strpos($haystack, $needle, $offset = 0)
132 return strpos($haystack, $needle, $offset);