summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Statement.php6
-rw-r--r--src/UtfString.php5
-rw-r--r--src/Utils/Formatter.php57
-rw-r--r--tests/Components/Array2dTest.php34
-rw-r--r--tests/Utils/FormatterTest.php6
5 files changed, 69 insertions, 39 deletions
diff --git a/src/Statement.php b/src/Statement.php
index a0a2b17..523683a 100644
--- a/src/Statement.php
+++ b/src/Statement.php
@@ -479,11 +479,11 @@ abstract class Statement
// Handle ordering of Multiple Joins in a query
if ($clauseStartIdx != -1) {
- if ($joinStart == 0 && stripos($clauseType, 'JOIN')) {
+ if ($joinStart == 0 && stripos($clauseType, 'JOIN') !== false) {
$joinStart = 1;
- } elseif ($joinStart == 1 && ! stripos($clauseType, 'JOIN')) {
+ } elseif ($joinStart == 1 && stripos($clauseType, 'JOIN') === false) {
$joinStart = 2;
- } elseif ($joinStart == 2 && stripos($clauseType, 'JOIN')) {
+ } elseif ($joinStart == 2 && stripos($clauseType, 'JOIN') !== false) {
$error = 1;
}
}
diff --git a/src/UtfString.php b/src/UtfString.php
index 8b53a6d..66ec4e4 100644
--- a/src/UtfString.php
+++ b/src/UtfString.php
@@ -76,10 +76,7 @@ class UtfString implements \ArrayAccess
$this->str = $str;
$this->byteIdx = 0;
$this->charIdx = 0;
- // TODO: `strlen($str)` might return a wrong length when function
- // overloading is enabled.
- // https://php.net/manual/ro/mbstring.overload.php
- $this->byteLen = strlen($str);
+ $this->byteLen = mb_strlen($str, '8bit');
$this->charLen = mb_strlen($str, 'UTF-8');
}
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php
index 51f8633..e459b60 100644
--- a/src/Utils/Formatter.php
+++ b/src/Utils/Formatter.php
@@ -71,7 +71,11 @@ class Formatter
$options
);
- $options['formats'] = self::mergeFormats($this->getDefaultFormats(), @$options['formats'] ?: array());
+ if (isset($options['formats'])) {
+ $options['formats'] = self::mergeFormats($this->getDefaultFormats(), $options['formats']);
+ } else {
+ $options['formats'] = $this->getDefaultFormats();
+ }
if (is_null($options['line_ending'])) {
$options['line_ending'] = $options['type'] === 'html' ? '<br/>' : "\n";
@@ -208,44 +212,39 @@ class Formatter
private static function mergeFormats(array $formats, array $newFormats)
{
$added = array();
+ $integers = array('flags', 'type');
+ $strings = array('html', 'cli', 'function');
+ /* Sanitize the array so that we do not have to care later */
+ foreach ($newFormats as $j => $new) {
+ foreach ($integers as $name) {
+ if (! isset($new[$name])) {
+ $newFormats[$j][$name] = 0;
+ }
+ }
+ foreach ($strings as $name) {
+ if (! isset($new[$name])) {
+ $newFormats[$j][$name] = '';
+ }
+ }
+ }
+
+ /* Process changes to existing formats */
foreach ($formats as $i => $original) {
foreach ($newFormats as $j => $new) {
- if (isset($new['type'])
- && $new['type'] === $original['type']
- && (
- (
- isset($new['flags'])
- && $original['flags'] === $new['flags']
- )
- || (
- !isset($new['flags'])
- && $original['flags'] == 0
- )
- )
+ if ($new['type'] === $original['type']
+ && $original['flags'] === $new['flags']
) {
- $formats[$i] = array(
- 'type' => $original['type'],
- 'flags' => isset($new['flags']) ? $new['flags'] : 0,
- 'html' => isset($new['html']) ? $new['html'] : '',
- 'cli' => isset($new['cli']) ? $new['cli'] : '',
- 'function' => isset($new['function']) ? $new['function'] : '',
- );
-
+ $formats[$i] = $new;
$added[] = $j;
}
}
}
+ /* Add not already handled formats */
foreach ($newFormats as $j => $new) {
- if (!in_array($j, $added) && isset($new['type'])) {
- $formats[] = array(
- 'type' => $new['type'],
- 'flags' => isset($new['flags']) ? $new['flags'] : 0,
- 'html' => isset($new['html']) ? $new['html'] : '',
- 'cli' => isset($new['cli']) ? $new['cli'] : '',
- 'function' => isset($new['function']) ? $new['function'] : '',
- );
+ if (! in_array($j, $added)) {
+ $formats[] = $new;
}
}
diff --git a/tests/Components/Array2dTest.php b/tests/Components/Array2dTest.php
index a1c9786..ee9d679 100644
--- a/tests/Components/Array2dTest.php
+++ b/tests/Components/Array2dTest.php
@@ -46,25 +46,53 @@ class Array2dTest extends TestCase
{
$parser = new Parser();
Array2d::parse($parser, $this->getTokensList(')'));
- Array2d::parse($parser, $this->getTokensList('TABLE'));
- // TODO: Assert errors.
+ $this->assertEquals(
+ 1,
+ count($parser->errors)
+ );
+ $this->assertEquals(
+ 'An opening bracket followed by a set of values was expected.',
+ $parser->errors[0]->getMessage()
+ );
}
public function testParseErr4()
{
$parser = new Parser();
+ Array2d::parse($parser, $this->getTokensList('TABLE'));
+ $this->assertEquals(
+ 1,
+ count($parser->errors)
+ );
+ $this->assertEquals(
+ 'An opening bracket followed by a set of values was expected.',
+ $parser->errors[0]->getMessage()
+ );
+ }
+
+ public function testParseErr5()
+ {
+ $parser = new Parser();
Array2d::parse($parser, $this->getTokensList('(1, 2),'));
$this->assertEquals(
+ 1,
+ count($parser->errors)
+ );
+ $this->assertEquals(
"An opening bracket followed by a set of values was expected.",
$parser->errors[0]->getMessage()
);
}
- public function testParseErr5()
+ public function testParseErr6()
{
$parser = new Parser();
Array2d::parse($parser, $this->getTokensList('(1, 2),(3)'));
$this->assertEquals(
+ 1,
+ count($parser->errors)
+ );
+ $this->assertEquals(
"2 values were expected, but found 1.",
$parser->errors[0]->getMessage()
);
diff --git a/tests/Utils/FormatterTest.php b/tests/Utils/FormatterTest.php
index 1cc3b06..f8a911f 100644
--- a/tests/Utils/FormatterTest.php
+++ b/tests/Utils/FormatterTest.php
@@ -59,6 +59,9 @@ class FormatTest extends TestCase
array(
'type' => 0,
'flags' => 0,
+ 'html' => '',
+ 'cli' => '',
+ 'function' => '',
),
),
array( // overriding
@@ -69,6 +72,9 @@ class FormatTest extends TestCase
array(
'type' => 0,
'flags' => 0,
+ 'html' => '',
+ 'cli' => '',
+ 'function' => '',
),
),
),