summaryrefslogtreecommitdiffstats
path: root/src/Components/ArrayObj.php
diff options
context:
space:
mode:
authorDan Ungureanu <udan1107@gmail.com>2016-02-02 23:17:12 +0200
committerDan Ungureanu <udan1107@gmail.com>2016-02-02 23:17:12 +0200
commit466b59cbb242361d0fe6c9adc4cbac688a333a68 (patch)
tree99190501983344ea5442b8ee648d6e4c51fa4f32 /src/Components/ArrayObj.php
parent0a603aa9a3363cd1d2f93dc3bfd373a7bf017f7a (diff)
downloadsql-parser-466b59cbb242361d0fe6c9adc4cbac688a333a68.zip
sql-parser-466b59cbb242361d0fe6c9adc4cbac688a333a68.tar.gz
sql-parser-466b59cbb242361d0fe6c9adc4cbac688a333a68.tar.bz2
ArrayObj: Handle more complex expressions in arrays.
Diffstat (limited to 'src/Components/ArrayObj.php')
-rw-r--r--src/Components/ArrayObj.php129
1 files changed, 82 insertions, 47 deletions
diff --git a/src/Components/ArrayObj.php b/src/Components/ArrayObj.php
index fb199ea..56c7247 100644
--- a/src/Components/ArrayObj.php
+++ b/src/Components/ArrayObj.php
@@ -63,20 +63,32 @@ class ArrayObj extends Component
$ret = empty($options['type']) ? new ArrayObj() : array();
/**
- * The state of the parser.
+ * The last raw expression.
*
- * Below are the states of the parser.
- *
- * 0 -----------------------[ ( ]------------------------> 1
+ * @var string $lastRaw
+ */
+ $lastRaw = '';
+
+ /**
+ * The last value.
*
- * 1 ------------------[ array element ]-----------------> 2
+ * @var string $lastValue
+ */
+ $lastValue = '';
+
+ /**
+ * Counts brackets.
*
- * 2 ------------------------[ , ]-----------------------> 1
- * 2 ------------------------[ ) ]-----------------------> (END)
+ * @var int $brackets
+ */
+ $brackets = 0;
+
+ /**
+ * Last separator (bracket or comma).
*
- * @var int $state
+ * @var boolean $isCommaLast
*/
- $state = 0;
+ $isCommaLast = false;
for (; $list->idx < $list->count; ++$list->idx) {
/**
@@ -92,49 +104,72 @@ class ArrayObj extends Component
}
// Skipping whitespaces and comments.
- if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
+ if (($token->type === Token::TYPE_WHITESPACE)
+ || ($token->type === Token::TYPE_COMMENT)
+ ) {
+ $lastRaw .= $token->token;
+ $lastValue = trim($lastValue) .' ';
continue;
}
- if ($state === 0) {
- if (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '(')) {
- $parser->error(
- __('An opening bracket was expected.'),
- $token
- );
- break;
- }
- $state = 1;
- } elseif ($state === 1) {
- if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ')')) {
- // Empty array.
- break;
- }
- if (empty($options['type'])) {
- $ret->values[] = $token->value;
- $ret->raw[] = $token->token;
- } else {
- $ret[] = $options['type']::parse(
- $parser,
- $list,
- empty($options['typeOptions']) ? array() : $options['typeOptions']
- );
- }
- $state = 2;
- } elseif ($state === 2) {
- if (($token->type !== Token::TYPE_OPERATOR) || (($token->value !== ',') && ($token->value !== ')'))) {
- $parser->error(
- __('A comma or a closing bracket was expected'),
- $token
- );
- break;
- }
- if ($token->value === ',') {
- $state = 1;
- } else { // )
- break;
+ if (($brackets === 0)
+ && (($token->type !== Token::TYPE_OPERATOR)
+ || ($token->value !== '('))
+ ) {
+ $parser->error(__('An opening bracket was expected.'), $token);
+ break;
+ }
+
+ if ($token->type === Token::TYPE_OPERATOR) {
+ if ($token->value === '(') {
+ if (++$brackets === 1) { // 1 is the base level.
+ continue;
+ }
+ } elseif ($token->value === ')') {
+ if (--$brackets === 0) { // Array ended.
+ break;
+ }
+ } elseif ($token->value === ',') {
+ if ($brackets === 1) {
+ $isCommaLast = true;
+ if (empty($options['type'])) {
+ $ret->raw[] = trim($lastRaw);
+ $ret->values[] = trim($lastValue);
+ $lastRaw = $lastValue = '';
+ }
+ }
+ continue;
}
}
+
+ if (empty($options['type'])) {
+ $lastRaw .= $token->token;
+ $lastValue .= $token->value;
+ } else {
+ $ret[] = $options['type']::parse(
+ $parser,
+ $list,
+ empty($options['typeOptions']) ? array() : $options['typeOptions']
+ );
+ }
+ }
+
+ // Handling last element.
+ //
+ // This is treated differently to treat the following cases:
+ //
+ // => array()
+ // (,) => array('', '')
+ // () => array()
+ // (a,) => array('a', '')
+ // (a) => array('a')
+ //
+ $lastRaw = trim($lastRaw);
+ if ((empty($options['type']))
+ && ((strlen($lastRaw) > 0) || ($isCommaLast))
+ ) {
+ $ret->raw[] = $lastRaw;
+ $ret->values[] = trim($lastValue);
}
return $ret;