summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Lexer.php15
-rw-r--r--src/Parser.php15
-rw-r--r--tests/Lexer/LexerTest.php18
-rw-r--r--tests/Parser/ParserTest.php22
-rw-r--r--tests/bootstrap.php13
5 files changed, 67 insertions, 16 deletions
diff --git a/src/Lexer.php b/src/Lexer.php
index d6c3784..cb3c658 100644
--- a/src/Lexer.php
+++ b/src/Lexer.php
@@ -13,14 +13,14 @@ namespace SqlParser;
use SqlParser\Exceptions\LexerException;
-if (!defined('USE_GETTEXT')) {
+if (!defined('TRANSLATE')) {
/**
- * Whether `gettext` function should be used to translate error messages
- * before throwing exceptions.
- * @var bool
+ * The name of the function that translates error messages.
+ * By default `__` (if exists).
+ * @var string
*/
- define('USE_GETTEXT', function_exists('gettext'));
+ define('TRANSLATE', function_exists('__') ? '__' : '');
}
if (!defined('USE_UTF_STRINGS')) {
@@ -322,8 +322,9 @@ class Lexer
public function error(
$msg = '', $str = '', $pos = 0, $args = null, $code = 0
) {
- if (USE_GETTEXT) {
- $msg = gettext($msg);
+ if (!empty(TRANSLATE)) {
+ $func = TRANSLATE;
+ $msg = $func($msg);
}
if (!empty($args)) {
$msg = vsprintf($msg, $args);
diff --git a/src/Parser.php b/src/Parser.php
index 2fdd27c..f29bc7d 100644
--- a/src/Parser.php
+++ b/src/Parser.php
@@ -12,14 +12,14 @@ namespace SqlParser;
use SqlParser\Statements\SelectStatement;
use SqlParser\Exceptions\ParserException;
-if (!defined('USE_GETTEXT')) {
+if (!defined('TRANSLATE')) {
/**
- * Whether `gettext` function should be used to translate error messages
- * before throwing exceptions.
- * @var bool
+ * The name of the function that translates error messages.
+ * By default `__` (if exists).
+ * @var string
*/
- define('USE_GETTEXT', function_exists('gettext'));
+ define('TRANSLATE', function_exists('__') ? '__' : '');
}
/**
@@ -419,8 +419,9 @@ class Parser
public function error(
$msg = '', Token $token = null, $args = null, $code = 0
) {
- if (USE_GETTEXT) {
- $msg = gettext($msg);
+ if (!empty(TRANSLATE)) {
+ $func = TRANSLATE;
+ $msg = $func($msg);
}
if (!empty($args)) {
$msg = vsprintf($msg, $args);
diff --git a/tests/Lexer/LexerTest.php b/tests/Lexer/LexerTest.php
index 1a7c5f5..8a7798c 100644
--- a/tests/Lexer/LexerTest.php
+++ b/tests/Lexer/LexerTest.php
@@ -31,6 +31,24 @@ class LexerTest extends TestCase
}
/**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
+ public function testErrorTranslate()
+ {
+ define('TRANSLATE', '\\SqlParser\\Tests\\translate');
+
+ $lexer = new Lexer('');
+
+ $lexer->error('TO_TRANSLATE', null);
+
+ $this->assertEquals(
+ $lexer->errors,
+ array(new LexerException('***', null, 0))
+ );
+ }
+
+ /**
* @expectedException SqlParser\Exceptions\LexerException
* @expectedExceptionMessage strict error
* @expectedExceptionCode 4
diff --git a/tests/Parser/ParserTest.php b/tests/Parser/ParserTest.php
index 82052a2..1cd5e2d 100644
--- a/tests/Parser/ParserTest.php
+++ b/tests/Parser/ParserTest.php
@@ -49,13 +49,31 @@ class ParserTest extends TestCase
$this->assertEquals(
$parser->errors,
array(
- new ParserException('error #1', new Token('foo'), 1),
- new ParserException('error #2', new Token('bar'), 2),
+ new ParserException('error #1', new Token('foo'), 1),
+ new ParserException('error #2', new Token('bar'), 2),
)
);
}
/**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
+ public function testErrorTranslate()
+ {
+ define('TRANSLATE', '\\SqlParser\\Tests\\translate');
+
+ $parser = new Parser(new TokensList());
+
+ $parser->error('TO_TRANSLATE', null);
+
+ $this->assertEquals(
+ $parser->errors,
+ array(new ParserException('***', null, 0))
+ );
+ }
+
+ /**
* @expectedException SqlParser\Exceptions\ParserException
* @expectedExceptionMessage strict error
* @expectedExceptionCode 3
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 6cc3c63..d23d71f 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -9,6 +9,19 @@ use SqlParser\Parser;
use SqlParser\Token;
/**
+ * Dummy function used to test if errors are translated.
+ *
+ * It translates only "TO_TRANSLATE" to "***".
+ *
+ * @param string $msg
+ *
+ * @return string
+ */
+function translate($msg) {
+ return str_replace('TO_TRANSLATE', '***' , $msg);
+}
+
+/**
* Implements useful methods for testing.
*
* Each test consists of a string that represents the serialized Lexer or Parser