aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-08-03 07:06:46 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-08-03 07:06:46 +0200
commit9c6e7c84880c457a836fea4c9f4668ef766765dd (patch)
tree4ed882b6fd8a118aa7bdaa19d92d5a0735a3372f
parent4cf922258a87eeb7edc592b45b21c6cf67b9bc5a (diff)
downloadUstring-9c6e7c84880c457a836fea4c9f4668ef766765dd.zip
Ustring-9c6e7c84880c457a836fea4c9f4668ef766765dd.tar.gz
Ustring-9c6e7c84880c457a836fea4c9f4668ef766765dd.tar.bz2
`ext/iconv` is suggested, no longer required.
First, this patch introduces and testes the `checkIconv` static method that checks whether the `ext/iconv` extension is available or not. Second, this method is used in the `Ustring::transcode` static method to test whether this extension is available. If not, then an exception is thrown. Finally, because this is only for one method **and** to allow third implementation to be used instead of the official `ext/iconv` extension, this extension is now suggested instead of required in the `composer.json` file. It removes one (strong) dependency to projects using `hoa/ustring` if they are sure to not use `Hoa\Ustring::transcode`.
-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": {