| 1 | <?php |
| 2 | /* |
| 3 | * $Id: From.php 1080 2007-02-10 18:17:08Z romanb $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Tokenizer |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Tokenizer |
| 27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 28 | * @link www.phpdoctrine.org |
| 29 | * @since 1.0 |
| 30 | * @version $Revision: 1080 $ |
| 31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 32 | */ |
| 33 | class Doctrine_Tokenizer |
| 34 | { |
| 35 | /** |
| 36 | * trims brackets |
| 37 | * |
| 38 | * @param string $str |
| 39 | * @param string $e1 the first bracket, usually '(' |
| 40 | * @param string $e2 the second bracket, usually ')' |
| 41 | */ |
| 42 | public static function bracketTrim($str, $e1 = '(', $e2 = ')') |
| 43 | { |
| 44 | if (substr($str, 0, 1) === $e1 && substr($str, -1) === $e2) { |
| 45 | return substr($str, 1, -1); |
| 46 | } else { |
| 47 | return $str; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * bracketExplode |
| 53 | * |
| 54 | * example: |
| 55 | * |
| 56 | * parameters: |
| 57 | * $str = (age < 20 AND age > 18) AND email LIKE 'John@example.com' |
| 58 | * $d = ' AND ' |
| 59 | * $e1 = '(' |
| 60 | * $e2 = ')' |
| 61 | * |
| 62 | * would return an array: |
| 63 | * array("(age < 20 AND age > 18)", |
| 64 | * "email LIKE 'John@example.com'") |
| 65 | * |
| 66 | * @param string $str |
| 67 | * @param string $d the delimeter which explodes the string |
| 68 | * @param string $e1 the first bracket, usually '(' |
| 69 | * @param string $e2 the second bracket, usually ')' |
| 70 | * |
| 71 | */ |
| 72 | public static function bracketExplode($str, $d = ' ', $e1 = '(', $e2 = ')') |
| 73 | { |
| 74 | if (is_array($d)) { |
| 75 | $a = preg_split('/('.implode('|', $d).')/', $str); |
| 76 | $d = stripslashes($d[0]); |
| 77 | } else { |
| 78 | $a = explode($d, $str); |
| 79 | } |
| 80 | |
| 81 | $i = 0; |
| 82 | $term = array(); |
| 83 | foreach($a as $key=>$val) { |
| 84 | if (empty($term[$i])) { |
| 85 | $term[$i] = trim($val); |
| 86 | $s1 = substr_count($term[$i], $e1); |
| 87 | $s2 = substr_count($term[$i], $e2); |
| 88 | |
| 89 | if ($s1 == $s2) { |
| 90 | $i++; |
| 91 | } |
| 92 | } else { |
| 93 | $term[$i] .= $d . trim($val); |
| 94 | $c1 = substr_count($term[$i], $e1); |
| 95 | $c2 = substr_count($term[$i], $e2); |
| 96 | |
| 97 | if ($c1 == $c2) { |
| 98 | $i++; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return $term; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * quoteExplode |
| 107 | * |
| 108 | * example: |
| 109 | * |
| 110 | * parameters: |
| 111 | * $str = email LIKE 'John@example.com' |
| 112 | * $d = ' AND ' |
| 113 | * |
| 114 | * would return an array: |
| 115 | * array("email", "LIKE", "'John@example.com'") |
| 116 | * |
| 117 | * @param string $str |
| 118 | * @param string $d the delimeter which explodes the string |
| 119 | */ |
| 120 | public static function quoteExplode($str, $d = ' ') |
| 121 | { |
| 122 | if (is_array($d)) { |
| 123 | $a = preg_split('/('.implode('|', $d).')/', $str); |
| 124 | $d = stripslashes($d[0]); |
| 125 | } else { |
| 126 | $a = explode($d, $str); |
| 127 | } |
| 128 | |
| 129 | $i = 0; |
| 130 | $term = array(); |
| 131 | foreach ($a as $key => $val) { |
| 132 | if (empty($term[$i])) { |
| 133 | $term[$i] = trim($val); |
| 134 | |
| 135 | if ( ! (substr_count($term[$i], "'") & 1)) { |
| 136 | $i++; |
| 137 | } |
| 138 | } else { |
| 139 | $term[$i] .= $d . trim($val); |
| 140 | |
| 141 | if ( ! (substr_count($term[$i], "'") & 1)) { |
| 142 | $i++; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return $term; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * sqlExplode |
| 151 | * |
| 152 | * explodes a string into array using custom brackets and |
| 153 | * quote delimeters |
| 154 | * |
| 155 | * |
| 156 | * example: |
| 157 | * |
| 158 | * parameters: |
| 159 | * $str = "(age < 20 AND age > 18) AND name LIKE 'John Doe'" |
| 160 | * $d = ' ' |
| 161 | * $e1 = '(' |
| 162 | * $e2 = ')' |
| 163 | * |
| 164 | * would return an array: |
| 165 | * array('(age < 20 AND age > 18)', |
| 166 | * 'name', |
| 167 | * 'LIKE', |
| 168 | * 'John Doe') |
| 169 | * |
| 170 | * @param string $str |
| 171 | * @param string $d the delimeter which explodes the string |
| 172 | * @param string $e1 the first bracket, usually '(' |
| 173 | * @param string $e2 the second bracket, usually ')' |
| 174 | * |
| 175 | * @return array |
| 176 | */ |
| 177 | public static function sqlExplode($str, $d = ' ', $e1 = '(', $e2 = ')') |
| 178 | { |
| 179 | if ($d == ' ') { |
| 180 | $d = array(' ', '\s'); |
| 181 | } |
| 182 | if (is_array($d)) { |
| 183 | $d = array_map('preg_quote', $d); |
| 184 | |
| 185 | if (in_array(' ', $d)) { |
| 186 | $d[] = '\s'; |
| 187 | } |
| 188 | |
| 189 | $split = '§(' . implode('|', $d) . ')§'; |
| 190 | |
| 191 | $str = preg_split($split, $str); |
| 192 | $d = stripslashes($d[0]); |
| 193 | } else { |
| 194 | $str = explode($d, $str); |
| 195 | } |
| 196 | |
| 197 | $i = 0; |
| 198 | $term = array(); |
| 199 | |
| 200 | foreach ($str as $key => $val) { |
| 201 | if (empty($term[$i])) { |
| 202 | $term[$i] = trim($val); |
| 203 | |
| 204 | $s1 = substr_count($term[$i], $e1); |
| 205 | $s2 = substr_count($term[$i], $e2); |
| 206 | |
| 207 | if (strpos($term[$i], '(') !== false) { |
| 208 | if ($s1 == $s2) { |
| 209 | $i++; |
| 210 | } |
| 211 | } else { |
| 212 | if ( ! (substr_count($term[$i], "'") & 1) && |
| 213 | ! (substr_count($term[$i], "\"") & 1)) { |
| 214 | $i++; |
| 215 | } |
| 216 | } |
| 217 | } else { |
| 218 | $term[$i] .= $d . trim($val); |
| 219 | $c1 = substr_count($term[$i], $e1); |
| 220 | $c2 = substr_count($term[$i], $e2); |
| 221 | |
| 222 | if (strpos($term[$i], '(') !== false) { |
| 223 | if ($c1 == $c2) { |
| 224 | $i++; |
| 225 | } |
| 226 | } else { |
| 227 | if ( ! (substr_count($term[$i], "'") & 1) && |
| 228 | ! (substr_count($term[$i], "\"") & 1)) { |
| 229 | $i++; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | return $term; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * clauseExplode |
| 239 | * |
| 240 | * explodes a string into array using custom brackets and |
| 241 | * quote delimeters |
| 242 | * |
| 243 | * |
| 244 | * example: |
| 245 | * |
| 246 | * parameters: |
| 247 | * $str = "(age < 20 AND age > 18) AND name LIKE 'John Doe'" |
| 248 | * $d = ' ' |
| 249 | * $e1 = '(' |
| 250 | * $e2 = ')' |
| 251 | * |
| 252 | * would return an array: |
| 253 | * array('(age < 20 AND age > 18)', |
| 254 | * 'name', |
| 255 | * 'LIKE', |
| 256 | * 'John Doe') |
| 257 | * |
| 258 | * @param string $str |
| 259 | * @param string $d the delimeter which explodes the string |
| 260 | * @param string $e1 the first bracket, usually '(' |
| 261 | * @param string $e2 the second bracket, usually ')' |
| 262 | * |
| 263 | * @return array |
| 264 | */ |
| 265 | public static function clauseExplode($str, array $d, $e1 = '(', $e2 = ')') |
| 266 | { |
| 267 | if (is_array($d)) { |
| 268 | $d = array_map('preg_quote', $d); |
| 269 | |
| 270 | if (in_array(' ', $d)) { |
| 271 | $d[] = '\s'; |
| 272 | } |
| 273 | |
| 274 | $split = '§(' . implode('|', $d) . ')§'; |
| 275 | |
| 276 | $str = preg_split($split, $str, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 277 | } |
| 278 | |
| 279 | $i = 0; |
| 280 | $term = array(); |
| 281 | |
| 282 | foreach ($str as $key => $val) { |
| 283 | if ($key & 1) { |
| 284 | if (isset($term[($i - 1)]) && ! is_array($term[($i - 1)])) { |
| 285 | $term[($i - 1)] = array($term[($i - 1)], $val); |
| 286 | } |
| 287 | continue; |
| 288 | } |
| 289 | if (empty($term[$i])) { |
| 290 | $term[$i] = $val; |
| 291 | } else { |
| 292 | $term[$i] .= $str[($key - 1)] . $val; |
| 293 | } |
| 294 | |
| 295 | $c1 = substr_count($term[$i], $e1); |
| 296 | $c2 = substr_count($term[$i], $e2); |
| 297 | |
| 298 | if (strpos($term[$i], '(') !== false) { |
| 299 | if ($c1 == $c2) { |
| 300 | $i++; |
| 301 | } |
| 302 | } else { |
| 303 | if ( ! (substr_count($term[$i], "'") & 1) && |
| 304 | ! (substr_count($term[$i], "\"") & 1)) { |
| 305 | $i++; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | $term[$i - 1] = array($term[$i - 1], ''); |
| 310 | |
| 311 | return $term; |
| 312 | } |
| 313 | } |