| 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: Xsql.php 220 2008-02-09 18:04:52Z doublecompile $ |
| 15 |
|
*/ |
| 16 |
|
/** |
| 17 |
|
* @see Xyster_Orm_Xsql_Split |
| 18 |
|
*/ |
| 19 |
1 |
require_once 'Xyster/Orm/Xsql/Split.php'; |
| 20 |
|
/** |
| 21 |
|
* A helper for XSQL syntax |
| 22 |
|
* |
| 23 |
|
* @category Xyster |
| 24 |
|
* @package Xyster_Orm |
| 25 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 26 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 27 |
|
*/ |
| 28 |
|
class Xyster_Orm_Xsql |
| 29 |
|
{ |
| 30 |
|
/** |
| 31 |
|
* Used for matching parenthesis groups not inside quotes |
| 32 |
|
* |
| 33 |
|
* It works, but this still matches parentheses inside quotes that aren't in |
| 34 |
|
* parentheses. |
| 35 |
|
*/ |
| 36 |
|
const PARENTH_QUOTE_REGEX = '/(?<![\w\d])(\(((?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^()])|(?1))+\))/m'; |
| 37 |
|
|
| 38 |
|
/** |
| 39 |
|
* Checks if a literal is a method call |
| 40 |
|
* |
| 41 |
|
* @param string $field |
| 42 |
|
* @return boolean |
| 43 |
|
*/ |
| 44 |
|
public static function isMethodCall( $field, array &$matches = array() ) |
| 45 |
|
{ |
| 46 |
48 |
return (bool) preg_match("/^[a-z_][a-z0-9_]*\([\w\W]*\)$/i", $field, $matches); |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
/** |
| 50 |
|
* Checks a literal for syntactical correctness |
| 51 |
|
* |
| 52 |
|
* @param string $lit |
| 53 |
|
* @return boolean |
| 54 |
|
*/ |
| 55 |
|
public static function isLiteral( $lit ) |
| 56 |
|
{ |
| 57 |
|
return ( |
| 58 |
|
// either a string or a number or the word "null" |
| 59 |
123 |
preg_match("/^(\"[^\"]*\"|[\d]+(.[\d]+)?|null)$/i", trim($lit)) |
| 60 |
|
// a string with escapes in it |
| 61 |
8 |
|| preg_match('/^"[^"\\\\]*(\\\\.[^"\\\\]*)*"$/', trim($lit)) |
| 62 |
|
// check to see if it's a field |
| 63 |
8 |
|| self::isValidField($lit) |
| 64 |
123 |
); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
* Checks a reference for syntactical correctness |
| 69 |
|
* |
| 70 |
|
* @param string $field |
| 71 |
|
* @return boolean |
| 72 |
|
*/ |
| 73 |
|
public static function isValidField( $field ) |
| 74 |
|
{ |
| 75 |
17 |
$field = trim($field); |
| 76 |
|
|
| 77 |
17 |
$ok = true; |
| 78 |
17 |
if ( !preg_match("/^[a-z][a-z0-9_]*(->[a-z0-9_]+(\([\s]*\))?)*$/i", $field) ) { |
| 79 |
17 |
$mcs = Xyster_Orm_Xsql_Split::Arrow()->split($field); |
| 80 |
17 |
foreach( $mcs as $mc ) { |
| 81 |
17 |
$matches = array(); |
| 82 |
17 |
$match = preg_match( "/^[a-z][a-z0-9_]*(\((?P<params>[\w\W]*)\))?$/i", $mc, $matches ); |
| 83 |
17 |
if ( ( $match && array_key_exists("params", $matches) && strlen(trim($matches['params'])) |
| 84 |
17 |
&& !self::_checkMethodParameters($matches['params']) ) || !$match ) { |
| 85 |
17 |
$ok = false; |
| 86 |
17 |
break; |
| 87 |
0 |
} |
| 88 |
2 |
} |
| 89 |
17 |
} |
| 90 |
17 |
return $ok; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
/** |
| 94 |
|
* Match nested parentheses groups not inside of double-quotes |
| 95 |
|
* |
| 96 |
|
* This method will find all balanced parentheses not inside of double |
| 97 |
|
* quotes. It also respects escaped double quotes (i.e. with a preceding |
| 98 |
|
* backslash). |
| 99 |
|
* |
| 100 |
|
* @param string $string |
| 101 |
|
* @return array |
| 102 |
|
* @todo make this work with top-level strings with parenths in them |
| 103 |
|
*/ |
| 104 |
|
public static function matchGroups( $string ) |
| 105 |
|
{ |
| 106 |
120 |
$groups = array(); |
| 107 |
|
|
| 108 |
120 |
if ( strpos($string, '(') !== false ) { |
| 109 |
58 |
$matches = array(); |
| 110 |
58 |
preg_match_all(self::PARENTH_QUOTE_REGEX, $string, $matches, PREG_SET_ORDER); |
| 111 |
58 |
foreach( $matches as $group ) { |
| 112 |
56 |
$groups[] = $group[0]; |
| 113 |
56 |
} |
| 114 |
58 |
} |
| 115 |
|
|
| 116 |
120 |
return $groups; |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
/** |
| 120 |
|
* Splits a string on the arrow ('->') |
| 121 |
|
* |
| 122 |
|
* @param string $haystack |
| 123 |
|
* @return array |
| 124 |
|
*/ |
| 125 |
|
public static function splitArrow( $haystack ) |
| 126 |
|
{ |
| 127 |
52 |
return Xyster_Orm_Xsql_Split::Arrow()->split($haystack); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
/** |
| 131 |
|
* Splits a string on the comma (',') |
| 132 |
|
* |
| 133 |
|
* @param string $haystack |
| 134 |
|
* @return array |
| 135 |
|
*/ |
| 136 |
|
public static function splitComma( $haystack ) |
| 137 |
|
{ |
| 138 |
14 |
return Xyster_Orm_Xsql_Split::Comma()->split($haystack); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
/** |
| 142 |
|
* Splits a string on the space (' ') |
| 143 |
|
* |
| 144 |
|
* @param string $haystack |
| 145 |
|
* @return array |
| 146 |
|
*/ |
| 147 |
|
public static function splitSpace( $haystack ) |
| 148 |
|
{ |
| 149 |
126 |
return Xyster_Orm_Xsql_Split::Space()->split($haystack); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
/** |
| 153 |
|
* Checks method parameters for syntactical correctness |
| 154 |
|
* |
| 155 |
|
* @param array $params |
| 156 |
|
* @return boolean |
| 157 |
|
*/ |
| 158 |
|
protected static function _checkMethodParameters( $params ) |
| 159 |
|
{ |
| 160 |
2 |
$ok = true; |
| 161 |
2 |
foreach( Xyster_Orm_Xsql_Split::Comma()->split(trim($params)) as $p ) { |
| 162 |
2 |
if ( !self::isLiteral($p) ) { |
| 163 |
2 |
$ok = false; |
| 164 |
2 |
break; |
| 165 |
0 |
} |
| 166 |
2 |
} |
| 167 |
2 |
return $ok; |
| 168 |
|
} |
| 169 |
|
} |