ce9dbfc358cb463210e4b4a987eb5a1f906dcc1a
[yaffs-website] / vendor / zendframework / zend-stdlib / src / StringWrapper / Native.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
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
8  */
9
10 namespace Zend\Stdlib\StringWrapper;
11
12 use Zend\Stdlib\Exception;
13 use Zend\Stdlib\StringUtils;
14
15 class Native extends AbstractStringWrapper
16 {
17     /**
18      * The character encoding working on
19      * (overwritten to change defaut encoding)
20      *
21      * @var string
22      */
23     protected $encoding = 'ASCII';
24
25     /**
26      * Check if the given character encoding is supported by this wrapper
27      * and the character encoding to convert to is also supported.
28      *
29      * @param  string      $encoding
30      * @param  string|null $convertEncoding
31      * @return bool
32      */
33     public static function isSupported($encoding, $convertEncoding = null)
34     {
35         $encodingUpper      = strtoupper($encoding);
36         $supportedEncodings = static::getSupportedEncodings();
37
38         if (! in_array($encodingUpper, $supportedEncodings)) {
39             return false;
40         }
41
42         // This adapter doesn't support to convert between encodings
43         if ($convertEncoding !== null && $encodingUpper !== strtoupper($convertEncoding)) {
44             return false;
45         }
46
47         return true;
48     }
49
50     /**
51      * Get a list of supported character encodings
52      *
53      * @return string[]
54      */
55     public static function getSupportedEncodings()
56     {
57         return StringUtils::getSingleByteEncodings();
58     }
59
60     /**
61      * Set character encoding working with and convert to
62      *
63      * @param string      $encoding         The character encoding to work with
64      * @param string|null $convertEncoding  The character encoding to convert to
65      * @return StringWrapperInterface
66      */
67     public function setEncoding($encoding, $convertEncoding = null)
68     {
69         $supportedEncodings = static::getSupportedEncodings();
70
71         $encodingUpper = strtoupper($encoding);
72         if (! in_array($encodingUpper, $supportedEncodings)) {
73             throw new Exception\InvalidArgumentException(
74                 'Wrapper doesn\'t support character encoding "' . $encoding . '"'
75             );
76         }
77
78         if ($encodingUpper !== strtoupper($convertEncoding)) {
79             $this->convertEncoding = $encodingUpper;
80         }
81
82         if ($convertEncoding !== null) {
83             if ($encodingUpper !== strtoupper($convertEncoding)) {
84                 throw new Exception\InvalidArgumentException(
85                     'Wrapper doesn\'t support to convert between character encodings'
86                 );
87             }
88
89             $this->convertEncoding = $encodingUpper;
90         } else {
91             $this->convertEncoding = null;
92         }
93         $this->encoding = $encodingUpper;
94
95         return $this;
96     }
97
98     /**
99      * Returns the length of the given string
100      *
101      * @param string $str
102      * @return int|false
103      */
104     public function strlen($str)
105     {
106         return strlen($str);
107     }
108
109     /**
110      * Returns the portion of string specified by the start and length parameters
111      *
112      * @param string   $str
113      * @param int      $offset
114      * @param int|null $length
115      * @return string|false
116      */
117     public function substr($str, $offset = 0, $length = null)
118     {
119         return substr($str, $offset, $length);
120     }
121
122     /**
123      * Find the position of the first occurrence of a substring in a string
124      *
125      * @param string $haystack
126      * @param string $needle
127      * @param int    $offset
128      * @return int|false
129      */
130     public function strpos($haystack, $needle, $offset = 0)
131     {
132         return strpos($haystack, $needle, $offset);
133     }
134 }