summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/Configuration/TSimpleDynamicParser.php
blob: e9fd5ad5b8939d9878dcfbb1672402804b4c2932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
 * TSimpleDynamicParser class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package Prado\Data\SqlMap\Configuration
 */

namespace Prado\Data\SqlMap\Configuration;

/**
 * TSimpleDynamicParser finds place holders $name$ in the sql text and replaces
 * it with a TSimpleDynamicParser::DYNAMIC_TOKEN.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @package Prado\Data\SqlMap\Configuration
 * @since 3.1
 */
class TSimpleDynamicParser
{
	const PARAMETER_TOKEN_REGEXP = '/\$([^\$]+)\$/';
	const DYNAMIC_TOKEN = '`!`';

	/**
	 * Parse the sql text for dynamic place holders of the form $name$.
	 * @param string Sql text.
	 * @return array name value pairs 'sql' and 'parameters'.
	 */
	public function parse($sqlText)
	{
		$matches = array();
		$mappings = array();
		preg_match_all(self::PARAMETER_TOKEN_REGEXP, $sqlText, $matches);
		for($i = 0, $k=count($matches[1]); $i<$k; $i++)
		{
			$mappings[] = $matches[1][$i];
			$sqlText = str_replace($matches[0][$i], self::DYNAMIC_TOKEN, $sqlText);
		}
		return array('sql'=>$sqlText, 'parameters'=>$mappings);
	}
}