aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-08-03 07:04:30 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-08-03 07:04:30 +0200
commit11c5be9916ee46690507cb004053a3773e699436 (patch)
tree566b4a3fe654f4d5a9b05943f0345309f389c79f
parentb03af96129159098c0b34b1950769f3f198dd9cd (diff)
downloadUstring-11c5be9916ee46690507cb004053a3773e699436.zip
Ustring-11c5be9916ee46690507cb004053a3773e699436.tar.gz
Ustring-11c5be9916ee46690507cb004053a3773e699436.tar.bz2
Test `ext/mbstring` availability globally.
First, in the constructor, we tested whether the `ext/mbstring` extension was available or not. This check was made each time the constructor run. This is useless and we can optimize it. Second, some static methods were using `ext/mbstring`, so this check was not made and this situation could lead to unexpected errors. This patch introduces and testes the `checkMbString` static method. As its name suggests, it checks whether the `ext/mbstring` extension is available or not. We finally use this method globally, after the class declaration, and throw an exception if the extension is not available.
-rw-r--r--Test/Unit/Ustring.php13
-rw-r--r--Ustring.php30
2 files changed, 31 insertions, 12 deletions
diff --git a/Test/Unit/Ustring.php b/Test/Unit/Ustring.php
index 0443892..a03db3c 100644
--- a/Test/Unit/Ustring.php
+++ b/Test/Unit/Ustring.php
@@ -36,8 +36,8 @@
namespace Hoa\Ustring\Test\Unit;
-use Hoa\Ustring as LUT;
use Hoa\Test;
+use Hoa\Ustring as LUT;
/**
* Class \Hoa\Ustring\Test\Unit\Ustring.
@@ -49,7 +49,16 @@ use Hoa\Test;
*/
class Ustring extends Test\Unit\Suite
{
- public function case_no_mbstring()
+ public function case_check_mbstring()
+ {
+ $this
+ ->given($this->function->function_exists = true)
+ ->then
+ ->boolean(LUT::checkMbString())
+ ->isTrue();
+ }
+
+ public function case_check_no_mbstring()
{
$this
->given(
diff --git a/Ustring.php b/Ustring.php
index 93c10a9..fe37f71 100644
--- a/Ustring.php
+++ b/Ustring.php
@@ -204,14 +204,6 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
*/
public function __construct($string = null)
{
- if (false === function_exists('mb_substr')) {
- throw new Exception(
- '%s needs the mbstring extension.',
- 0,
- get_class($this)
- );
- }
-
if (null !== $string) {
$this->append($string);
}
@@ -220,6 +212,16 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
}
/**
+ * Check if ext/mbstring is available.
+ *
+ * @return bool
+ */
+ public static function checkMbString()
+ {
+ return function_exists('mb_substr');
+ }
+
+ /**
* Append a substring to the current string, i.e. add to the end.
*
* @param string $substring Substring to append.
@@ -504,7 +506,7 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
throw new Exception(
'%s needs the class Normalizer to work properly, ' .
'or you can force a try by using %1$s(true).',
- 1,
+ 0,
__METHOD__
);
}
@@ -537,7 +539,7 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
if (null === $transliterator = static::getTransliterator($identifier)) {
throw new Exception(
'%s needs the class Transliterator to work properly.',
- 2,
+ 1,
__METHOD__
);
}
@@ -1035,3 +1037,11 @@ class Ustring implements \ArrayAccess, \Countable, \IteratorAggregate
* Flex entity.
*/
Core\Consistency::flexEntity('Hoa\Ustring\Ustring');
+
+if (false === Ustring::checkMbString()) {
+ throw new Exception(
+ '%s needs the mbstring extension.',
+ 0,
+ __NAMESPACE__ . '\Ustring'
+ );
+}