]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/SqlMap/Configuration/TSimpleDynamicParser.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Data / SqlMap / Configuration / TSimpleDynamicParser.php
1 <?php
2 /**
3  * TSimpleDynamicParser class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Data.SqlMap.Configuration
10  */
11
12 /**
13  * TSimpleDynamicParser finds place holders $name$ in the sql text and replaces
14  * it with a TSimpleDynamicParser::DYNAMIC_TOKEN.
15  *
16  * @author Wei Zhuo <weizho[at]gmail[dot]com>
17  * @package System.Data.SqlMap.Configuration
18  * @since 3.1
19  */
20 class TSimpleDynamicParser
21 {
22         const PARAMETER_TOKEN_REGEXP = '/\$([^\$]+)\$/';
23         const DYNAMIC_TOKEN = '`!`';
24
25         /**
26          * Parse the sql text for dynamic place holders of the form $name$.
27          * @param string Sql text.
28          * @return array name value pairs 'sql' and 'parameters'.
29          */
30         public function parse($sqlText)
31         {
32                 $matches = array();
33                 $mappings = array();
34                 preg_match_all(self::PARAMETER_TOKEN_REGEXP, $sqlText, $matches);
35                 for($i = 0, $k=count($matches[1]); $i<$k; $i++)
36                 {
37                         $mappings[] = $matches[1][$i];
38                         $sqlText = str_replace($matches[0][$i], self::DYNAMIC_TOKEN, $sqlText);
39                 }
40                 return array('sql'=>$sqlText, 'parameters'=>$mappings);
41         }
42 }
43