| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Orm |
| 12 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
* @version $Id: Split.php 220 2008-02-09 18:04:52Z doublecompile $ |
| 15 |
|
*/ |
| 16 |
|
/** |
| 17 |
|
* A string exploder that won't split inside paretheses or double-quotes |
| 18 |
|
* |
| 19 |
|
* @category Xyster |
| 20 |
|
* @package Xyster_Orm |
| 21 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 22 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 23 |
|
*/ |
| 24 |
|
class Xyster_Orm_Xsql_Split |
| 25 |
|
{ |
| 26 |
|
/** |
| 27 |
|
* @var string |
| 28 |
|
*/ |
| 29 |
|
protected $_needle; |
| 30 |
|
|
| 31 |
|
/** |
| 32 |
|
* @var int |
| 33 |
|
*/ |
| 34 |
|
protected $_needleLength; |
| 35 |
|
|
| 36 |
|
/** |
| 37 |
|
* @var array |
| 38 |
|
*/ |
| 39 |
|
protected static $_splits = array(); |
| 40 |
|
|
| 41 |
|
/** |
| 42 |
|
* Creates a new splitter |
| 43 |
|
* |
| 44 |
|
* @param string $needle |
| 45 |
|
*/ |
| 46 |
|
protected function __construct( $needle ) |
| 47 |
|
{ |
| 48 |
3 |
$this->_needle = $needle; |
| 49 |
3 |
$this->_needleLength = strlen($needle); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
/** |
| 53 |
|
* Like explode(), but won't split inside of parentheses or double-quotes |
| 54 |
|
* |
| 55 |
|
* This method will split a string into an array using a string as a |
| 56 |
|
* seperator. If the seperator is contained within a pair of double-quotes |
| 57 |
|
* or between matching parentheses, it will be ignored. If the seperator is |
| 58 |
|
* not found, the method will return an array with one element containing |
| 59 |
|
* the entire string. |
| 60 |
|
* |
| 61 |
|
* Example: |
| 62 |
|
* <code> |
| 63 |
|
* $haystack = 'A (great (test)) of "this method"'; |
| 64 |
|
* $split = Xyster_Orm_Xsql_Split::Space(); |
| 65 |
|
* print_r($split->split(' ', $haystack)); |
| 66 |
|
* </code> |
| 67 |
|
* |
| 68 |
|
* Would print: |
| 69 |
|
* <pre> |
| 70 |
|
* array ( |
| 71 |
|
* [0]=>A |
| 72 |
|
* [1]=>(great (test)) |
| 73 |
|
* [2]=>of |
| 74 |
|
* [3]=>"this method" |
| 75 |
|
* ) |
| 76 |
|
* </pre> |
| 77 |
|
* |
| 78 |
|
* @param string $haystack String to split |
| 79 |
|
* @param boolean $caseInsensitive Whether to ignore case |
| 80 |
|
* @return array Split parts of $haystack |
| 81 |
|
*/ |
| 82 |
|
public function split( $haystack, $caseInsensitive = true ) |
| 83 |
|
{ |
| 84 |
156 |
$needle = $this->_needle; |
| 85 |
156 |
$nlength = $this->_needleLength; |
| 86 |
|
|
| 87 |
156 |
$split = array(); |
| 88 |
156 |
$buff = ""; |
| 89 |
156 |
$inPar = 0; |
| 90 |
156 |
$inStr = false; |
| 91 |
|
|
| 92 |
156 |
for( $i=0; $i<strlen($haystack); $i++ ) { |
| 93 |
156 |
$currNeed = substr($haystack, $i, $nlength); |
| 94 |
156 |
$curr = $currNeed[0]; |
| 95 |
156 |
$last = ( $i ) ? $haystack[$i-1] : ""; |
| 96 |
156 |
if ( $curr == '"' && ( ( $inStr && $last != "\\") || !$inStr ) ) { |
| 97 |
30 |
$inStr = !$inStr; |
| 98 |
30 |
} |
| 99 |
156 |
if ( !$inStr ) { |
| 100 |
156 |
if ( $curr == "(" ) { |
| 101 |
40 |
$inPar++; |
| 102 |
156 |
} else if ( $curr == ")" ) { |
| 103 |
40 |
$inPar--; |
| 104 |
40 |
} |
| 105 |
156 |
} |
| 106 |
156 |
if ( !$inPar && !$inStr && |
| 107 |
156 |
( $currNeed == $needle || |
| 108 |
156 |
( $caseInsensitive && strcasecmp($currNeed, $needle) == 0 ) ) ) { |
| 109 |
135 |
$split[] = $buff; |
| 110 |
135 |
$buff = ""; |
| 111 |
135 |
$i = $i+($nlength-1); |
| 112 |
135 |
} else { |
| 113 |
156 |
$buff .= $curr; |
| 114 |
|
} |
| 115 |
156 |
} |
| 116 |
156 |
$split[] = $buff; |
| 117 |
|
|
| 118 |
156 |
return $split; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
/** |
| 122 |
|
* Gets a splitter for the arrow ('->') |
| 123 |
|
* |
| 124 |
|
* @return Xyster_Orm_Xsql_Split |
| 125 |
|
*/ |
| 126 |
|
public static function Arrow() |
| 127 |
|
{ |
| 128 |
59 |
return self::_factory('->'); |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
/** |
| 132 |
|
* Gets a splitter for a comma (',') |
| 133 |
|
* |
| 134 |
|
* @return Xyster_Orm_Xsql_Split |
| 135 |
|
*/ |
| 136 |
|
public static function Comma() |
| 137 |
|
{ |
| 138 |
17 |
return self::_factory(','); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
/** |
| 142 |
|
* Gets a splitter for any string |
| 143 |
|
* |
| 144 |
|
* @return Xyster_Orm_Xsql_Split |
| 145 |
|
*/ |
| 146 |
|
public static function Custom( $needle ) |
| 147 |
|
{ |
| 148 |
120 |
return self::_factory($needle); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
/** |
| 152 |
|
* Gets a splitter for a space (' ') |
| 153 |
|
* |
| 154 |
|
* @return Xyster_Orm_Xsql_Split |
| 155 |
|
*/ |
| 156 |
|
public static function Space() |
| 157 |
|
{ |
| 158 |
127 |
return self::_factory(' '); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
/** |
| 162 |
|
* Creates a new splitter |
| 163 |
|
* |
| 164 |
|
* @param string $needle |
| 165 |
|
* @return Xyster_Orm_Xsql_Split |
| 166 |
|
*/ |
| 167 |
|
protected static function _factory( $needle ) |
| 168 |
|
{ |
| 169 |
156 |
if ( !isset(self::$_splits[$needle]) ) { |
| 170 |
3 |
self::$_splits[$needle] = new self($needle); |
| 171 |
3 |
} |
| 172 |
156 |
return self::$_splits[$needle]; |
| 173 |
|
} |
| 174 |
|
} |