diff options
-rw-r--r-- | Test/Unit/Ustring.php | 13 | ||||
-rw-r--r-- | Ustring.php | 30 |
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' + ); +} |