aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Test/Unit/Ustring.php14
-rw-r--r--Ustring.php19
-rw-r--r--composer.json6
3 files changed, 36 insertions, 3 deletions
diff --git a/Test/Unit/Ustring.php b/Test/Unit/Ustring.php
index a03db3c..36095d4 100644
--- a/Test/Unit/Ustring.php
+++ b/Test/Unit/Ustring.php
@@ -901,6 +901,20 @@ class Ustring extends Test\Unit\Suite
->isEqualTo('11110000100111111001001010101001');
}
+ public function case_transcode_no_iconv()
+ {
+ $this
+ ->given(
+ $this->function->function_exists = function ($name) {
+ return 'iconv' !== $name;
+ }
+ )
+ ->exception(function () {
+ LUT::transcode('foo', 'UTF-8');
+ })
+ ->isInstanceOf('Hoa\Ustring\Exception');
+ }
+
public function case_transcode_and_isUtf8()
{
$this
diff --git a/Ustring.php b/Ustring.php
index fe37f71..8558847 100644
--- a/Ustring.php
+++ b/Ustring.php
@@ -222,6 +222,16 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
}
/**
+ * Check if ext/iconv is available.
+ *
+ * @return bool
+ */
+ public static function checkIconv()
+ {
+ return function_exists('iconv');
+ }
+
+ /**
* Append a substring to the current string, i.e. add to the end.
*
* @param string $substring Substring to append.
@@ -995,9 +1005,18 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
* @param string $from Original encoding.
* @param string $to Final encoding.
* @return string
+ * @throws \Hoa\Ustring\Exception
*/
public static function transcode($string, $from, $to = 'UTF-8')
{
+ if (false === static::checkIconv()) {
+ throw new Exception(
+ '%s needs the iconv extension.',
+ 2,
+ __CLASS__
+ );
+ }
+
return iconv($from, $to, $string);
}
diff --git a/composer.json b/composer.json
index 1474a13..081f596 100644
--- a/composer.json
+++ b/composer.json
@@ -21,8 +21,7 @@
"source": "http://git.hoa-project.net/"
},
"require": {
- "hoa/core" : "~2.0",
- "ext-iconv" : "*"
+ "hoa/core": "~2.0"
},
"require-dev": {
"hoa/test": "~1.0"
@@ -33,7 +32,8 @@
}
},
"suggest": {
- "ext-intl": "To get a better Hoa\\Ustring::toAscii() and Hoa\\Ustring::compareTo()."
+ "ext-iconv": "ext/iconv must be present (or a third implementation) to use Hoa\\Ustring::transcode().",
+ "ext-intl" : "To get a better Hoa\\Ustring::toAscii() and Hoa\\Ustring::compareTo()."
},
"extra" : {
"branch-alias": {