Mercurial > hg > isophonics-drupal-site
comparison vendor/zendframework/zend-stdlib/src/StringWrapper/MbString.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 | |
14 class MbString extends AbstractStringWrapper | |
15 { | |
16 /** | |
17 * List of supported character sets (upper case) | |
18 * | |
19 * @var null|string[] | |
20 * @link http://php.net/manual/mbstring.supported-encodings.php | |
21 */ | |
22 protected static $encodings = null; | |
23 | |
24 /** | |
25 * Get a list of supported character encodings | |
26 * | |
27 * @return string[] | |
28 */ | |
29 public static function getSupportedEncodings() | |
30 { | |
31 if (static::$encodings === null) { | |
32 static::$encodings = array_map('strtoupper', mb_list_encodings()); | |
33 | |
34 // FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result | |
35 $indexIso885916 = array_search('ISO-8859-16', static::$encodings, true); | |
36 if ($indexIso885916 !== false) { | |
37 unset(static::$encodings[$indexIso885916]); | |
38 } | |
39 } | |
40 | |
41 return static::$encodings; | |
42 } | |
43 | |
44 /** | |
45 * Constructor | |
46 * | |
47 * @throws Exception\ExtensionNotLoadedException | |
48 */ | |
49 public function __construct() | |
50 { | |
51 if (!extension_loaded('mbstring')) { | |
52 throw new Exception\ExtensionNotLoadedException( | |
53 'PHP extension "mbstring" is required for this wrapper' | |
54 ); | |
55 } | |
56 } | |
57 | |
58 /** | |
59 * Returns the length of the given string | |
60 * | |
61 * @param string $str | |
62 * @return int|false | |
63 */ | |
64 public function strlen($str) | |
65 { | |
66 return mb_strlen($str, $this->getEncoding()); | |
67 } | |
68 | |
69 /** | |
70 * Returns the portion of string specified by the start and length parameters | |
71 * | |
72 * @param string $str | |
73 * @param int $offset | |
74 * @param int|null $length | |
75 * @return string|false | |
76 */ | |
77 public function substr($str, $offset = 0, $length = null) | |
78 { | |
79 return mb_substr($str, $offset, $length, $this->getEncoding()); | |
80 } | |
81 | |
82 /** | |
83 * Find the position of the first occurrence of a substring in a string | |
84 * | |
85 * @param string $haystack | |
86 * @param string $needle | |
87 * @param int $offset | |
88 * @return int|false | |
89 */ | |
90 public function strpos($haystack, $needle, $offset = 0) | |
91 { | |
92 return mb_strpos($haystack, $needle, $offset, $this->getEncoding()); | |
93 } | |
94 | |
95 /** | |
96 * Convert a string from defined encoding to the defined convert encoding | |
97 * | |
98 * @param string $str | |
99 * @param bool $reverse | |
100 * @return string|false | |
101 */ | |
102 public function convert($str, $reverse = false) | |
103 { | |
104 $encoding = $this->getEncoding(); | |
105 $convertEncoding = $this->getConvertEncoding(); | |
106 | |
107 if ($convertEncoding === null) { | |
108 throw new Exception\LogicException( | |
109 'No convert encoding defined' | |
110 ); | |
111 } | |
112 | |
113 if ($encoding === $convertEncoding) { | |
114 return $str; | |
115 } | |
116 | |
117 $fromEncoding = $reverse ? $convertEncoding : $encoding; | |
118 $toEncoding = $reverse ? $encoding : $convertEncoding; | |
119 return mb_convert_encoding($str, $toEncoding, $fromEncoding); | |
120 } | |
121 } |