From 29a6d2ef7f90d1779812bff3cb48dcb514fff14e Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 11 May 2007 19:07:06 +0000 Subject: [PATCH] added new methods --- lib/Doctrine/Tokenizer.php | 162 ++++++++++++++++++++++++++++++++++--- 1 file changed, 153 insertions(+), 9 deletions(-) diff --git a/lib/Doctrine/Tokenizer.php b/lib/Doctrine/Tokenizer.php index 30b28cf1b..1faf4cf5d 100644 --- a/lib/Doctrine/Tokenizer.php +++ b/lib/Doctrine/Tokenizer.php @@ -39,6 +39,21 @@ class Doctrine_Tokenizer public function tokenize() { + } + /** + * trims brackets + * + * @param string $str + * @param string $e1 the first bracket, usually '(' + * @param string $e2 the second bracket, usually ')' + */ + public static function bracketTrim($str,$e1 = '(',$e2 = ')') + { + if(substr($str,0,1) == $e1 && substr($str,-1) == $e2) { + return substr($str,1,-1); + } else { + return $str; + } } /** * bracketExplode @@ -63,25 +78,154 @@ class Doctrine_Tokenizer */ public static function bracketExplode($str, $d = ' ', $e1 = '(', $e2 = ')') { - if (is_array($d)) { - $a = preg_split('/(' . implode('|', $d) . ')/', $str); + if(is_array($d)) { + $a = preg_split('/('.implode('|', $d).')/', $str); $d = stripslashes($d[0]); - } else { + } else $a = explode($d, $str); - } + $i = 0; $term = array(); - foreach($a as $key => $val) { + foreach($a as $key=>$val) { if (empty($term[$i])) { $term[$i] = trim($val); + $s1 = substr_count($term[$i], $e1); + $s2 = substr_count($term[$i], $e2); + + if($s1 == $s2) { + $i++; + } } else { $term[$i] .= $d . trim($val); + $c1 = substr_count($term[$i], $e1); + $c2 = substr_count($term[$i], $e2); + + if($c1 == $c2) { + $i++; + } } - $c1 = substr_count($term[$i], $e1); - $c2 = substr_count($term[$i], $e2); + } + return $term; + } + /** + * quoteExplode + * + * example: + * + * parameters: + * $str = email LIKE 'John@example.com' + * $d = ' AND ' + * + * would return an array: + * array("email", "LIKE", "'John@example.com'") + * + * @param string $str + * @param string $d the delimeter which explodes the string + */ + public static function quoteExplode($str, $d = ' ') + { + if (is_array($d)) { + $a = preg_split('/('.implode('|', $d).')/', $str); + $d = stripslashes($d[0]); + } else + $a = explode($d, $str); - if($c1 == $c2) { - $i++; + $i = 0; + $term = array(); + foreach ($a as $key => $val) { + if (empty($term[$i])) { + $term[$i] = trim($val); + + if( ! (substr_count($term[$i], "'") & 1)) { + $i++; + } + } else { + $term[$i] .= $d . trim($val); + + if( ! (substr_count($term[$i], "'") & 1)) { + $i++; + } + } + } + return $term; + } + /** + * sqlExplode + * + * explodes a string into array using custom brackets and + * quote delimeters + * + * + * example: + * + * parameters: + * $str = "(age < 20 AND age > 18) AND name LIKE 'John Doe'" + * $d = ' ' + * $e1 = '(' + * $e2 = ')' + * + * would return an array: + * array('(age < 20 AND age > 18)', + * 'name', + * 'LIKE', + * 'John Doe') + * + * @param string $str + * @param string $d the delimeter which explodes the string + * @param string $e1 the first bracket, usually '(' + * @param string $e2 the second bracket, usually ')' + * + * @return array + */ + public static function sqlExplode($str, $d = ' ', $e1 = '(', $e2 = ')') + { + if ($d == ' ') { + $d = array(' ', '\s'); + } + if (is_array($d)) { + if (in_array(' ', $d)) { + $d[] = '\s'; + } + $str = preg_split('/(' . implode('|', $d) . ')/', $str); + $d = stripslashes($d[0]); + } else { + $str = explode("$d",$str); + } + + $i = 0; + $term = array(); + foreach ($str as $key => $val) { + if (empty($term[$i])) { + $term[$i] = trim($val); + + $s1 = substr_count($term[$i], $e1); + $s2 = substr_count($term[$i], $e2); + + if (substr($term[$i],0,1) == '(') { + if($s1 == $s2) { + $i++; + } + } else { + if ( ! (substr_count($term[$i], "'") & 1) && + ! (substr_count($term[$i], "\"") & 1)) { + $i++; + } + } + } else { + $term[$i] .= $d . trim($val); + $c1 = substr_count($term[$i], $e1); + $c2 = substr_count($term[$i], $e2); + + if (substr($term[$i],0,1) == '(') { + if($c1 == $c2) { + $i++; + } + } else { + if ( ! (substr_count($term[$i], "'") & 1) && + ! (substr_count($term[$i], "\"") & 1)) { + $i++; + } + } } } return $term;