Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / zendframework / zend-stdlib / src / StringUtils.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;
11
12 use Zend\Stdlib\StringWrapper\StringWrapperInterface;
13
14 /**
15  * Utility class for handling strings of different character encodings
16  * using available PHP extensions.
17  *
18  * Declared abstract, as we have no need for instantiation.
19  */
20 abstract class StringUtils
21 {
22     /**
23      * Ordered list of registered string wrapper instances
24      *
25      * @var StringWrapperInterface[]
26      */
27     protected static $wrapperRegistry = null;
28
29     /**
30      * A list of known single-byte character encodings (upper-case)
31      *
32      * @var string[]
33      */
34     protected static $singleByteEncodings = [
35         'ASCII', '7BIT', '8BIT',
36         'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5',
37         'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10',
38         'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
39         'CP-1251', 'CP-1252',
40         // TODO
41     ];
42
43     /**
44      * Is PCRE compiled with Unicode support?
45      *
46      * @var bool
47      **/
48     protected static $hasPcreUnicodeSupport = null;
49
50     /**
51      * Get registered wrapper classes
52      *
53      * @return string[]
54      */
55     public static function getRegisteredWrappers()
56     {
57         if (static::$wrapperRegistry === null) {
58             static::$wrapperRegistry = [];
59
60             if (extension_loaded('intl')) {
61                 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Intl';
62             }
63
64             if (extension_loaded('mbstring')) {
65                 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\MbString';
66             }
67
68             if (extension_loaded('iconv')) {
69                 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Iconv';
70             }
71
72             static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Native';
73         }
74
75         return static::$wrapperRegistry;
76     }
77
78     /**
79      * Register a string wrapper class
80      *
81      * @param string $wrapper
82      * @return void
83      */
84     public static function registerWrapper($wrapper)
85     {
86         $wrapper = (string) $wrapper;
87         if (! in_array($wrapper, static::$wrapperRegistry, true)) {
88             static::$wrapperRegistry[] = $wrapper;
89         }
90     }
91
92     /**
93      * Unregister a string wrapper class
94      *
95      * @param string $wrapper
96      * @return void
97      */
98     public static function unregisterWrapper($wrapper)
99     {
100         $index = array_search((string) $wrapper, static::$wrapperRegistry, true);
101         if ($index !== false) {
102             unset(static::$wrapperRegistry[$index]);
103         }
104     }
105
106     /**
107      * Reset all registered wrappers so the default wrappers will be used
108      *
109      * @return void
110      */
111     public static function resetRegisteredWrappers()
112     {
113         static::$wrapperRegistry = null;
114     }
115
116     /**
117      * Get the first string wrapper supporting the given character encoding
118      * and supports to convert into the given convert encoding.
119      *
120      * @param string      $encoding        Character encoding to support
121      * @param string|null $convertEncoding OPTIONAL character encoding to convert in
122      * @return StringWrapperInterface
123      * @throws Exception\RuntimeException If no wrapper supports given character encodings
124      */
125     public static function getWrapper($encoding = 'UTF-8', $convertEncoding = null)
126     {
127         foreach (static::getRegisteredWrappers() as $wrapperClass) {
128             if ($wrapperClass::isSupported($encoding, $convertEncoding)) {
129                 $wrapper = new $wrapperClass($encoding, $convertEncoding);
130                 $wrapper->setEncoding($encoding, $convertEncoding);
131                 return $wrapper;
132             }
133         }
134
135         throw new Exception\RuntimeException(
136             'No wrapper found supporting "' . $encoding . '"'
137             . (($convertEncoding !== null) ? ' and "' . $convertEncoding . '"' : '')
138         );
139     }
140
141     /**
142      * Get a list of all known single-byte character encodings
143      *
144      * @return string[]
145      */
146     public static function getSingleByteEncodings()
147     {
148         return static::$singleByteEncodings;
149     }
150
151     /**
152      * Check if a given encoding is a known single-byte character encoding
153      *
154      * @param string $encoding
155      * @return bool
156      */
157     public static function isSingleByteEncoding($encoding)
158     {
159         return in_array(strtoupper($encoding), static::$singleByteEncodings);
160     }
161
162     /**
163      * Check if a given string is valid UTF-8 encoded
164      *
165      * @param string $str
166      * @return bool
167      */
168     public static function isValidUtf8($str)
169     {
170         return is_string($str) && ($str === '' || preg_match('/^./su', $str) == 1);
171     }
172
173     /**
174      * Is PCRE compiled with Unicode support?
175      *
176      * @return bool
177      */
178     public static function hasPcreUnicodeSupport()
179     {
180         if (static::$hasPcreUnicodeSupport === null) {
181             ErrorHandler::start();
182             static::$hasPcreUnicodeSupport = defined('PREG_BAD_UTF8_OFFSET_ERROR') && preg_match('/\pL/u', 'a') == 1;
183             ErrorHandler::stop();
184         }
185         return static::$hasPcreUnicodeSupport;
186     }
187 }