summaryrefslogtreecommitdiff
path: root/framework/DataAccess/SQLMap/Configuration/TSqlMapConditionalTag.php
blob: dfb98223b1bed2f03c65372aa481291262a5cf31 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

/**
 * TSqlMapConditionalTag class.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package System.DataAccess.SQLMap.Configuration
 * @since 3.0
 */
class TSqlMapConditionalTag extends TComponent
{
	public static $ConditionalChecks = array
	(
		'isEqual', 'isNotEqual', 
		'isGreaterThan', 'isGreaterEqual', 
		'isLessThan', 'isLessEqual'
	);
	
	public static $UnaryChecks = array
	(
		'isNull', 'isNotNull', 
		'isEmpty', 'isNotEmpty'
	);
	
	public static $ParameterChecks = array
	(
		'isPropertyAvailable', 'isPropertyNotAvailable',
		'isParameterPresent', 'isParameterNotPresent'
	);
	
	private $_tagName;
	private $_prepend='';
	private $_property;
	private $_propertyType = 'string';
	private $_compareProperty;
	private $_compareValue;

	public function getTagName(){ return $this->_tagName; }
	public function setTagName($value){ $this->_tagName = $value; }

	public function getPrepend(){ return $this->_prepend; }
	public function setPrepend($value){ $this->_prepend = $value; }

	public function getProperty(){ return $this->_property; }
	public function setProperty($value){ $this->_property = $value; }

	public function getCompareProperty(){ return $this->_compareProperty; }
	public function setCompareProperty($value){ $this->_compareProperty = $value; }

	public function getCompareValue(){ return $this->_compareValue; }
	public function setCompareValue($value){ $this->_compareValue = $value; }
	
	public function getPropertyType(){ return $this->_propertyType; }
	public function setPropertyType($value){ $this->_propertyType = $value; }
	
	/**
	 * Evaluates the conditional tag, return true if satisfied, false otherwise.
	 * @param object current conditional tag context.
	 * @param object query parameter object.
	 * @return boolean true if conditions are met, false otherwise.
	 */
	public function evaluate($context, $parameter)
	{
		$value = $this->getPropertyValue($parameter, $this->getProperty());
		if(in_array($this->getTagName(), self::$UnaryChecks))
			return $this->evaluateUnary($context, $value);
		else if(in_array($this->getTagName(), self::$ConditionalChecks))
		{
			$comparee = $this->getComparisonValue($parameter);
			return $this->evaluateConditional($context, $value, $comparee);
		}
		else
			return $this->evaluateParameterCheck($context, $parameter);
	}
	
	protected function evaluateUnary($context, $value)
	{
		switch($this->getTagName())
		{
			case 'isNull':
				return is_null($value);
			case 'isNotNull':
				return !is_null($value);
			case 'isEmpty':
				return empty($value);
			case 'isNotEmpty':
				return !empty($value);
		}
	}
	
	protected function evaluateConditional($context, $valueA, $valueB)
	{
		switch($this->getTagName())
		{
			case 'isEqual': return $valueA == $valueB;
			case 'isNotEqual': return $valueA != $valueB;
			case 'isGreaterThan': return $valueA > $valueB;
			case 'isGreaterEqual': return $valueA >= $valueB;
			case 'isLessThan': return $valueA < $valueB;
			case 'isLessEqual': return $valueA <= $valueB;
		}
	}
	
	protected function evaluateParameterCheck($context, $parameter)
	{
		switch($this->getTagName())
		{
			case 'isPropertyAvailable': return TPropertyAccess::has(
					$parameter, $this->getProperty());
			case 'isPropertyAvailable': return TPropertyAccess::has(
							$parameter, $this->getProperty()) == false;
			case 'isParameterPresent': return !is_null($parameter);
			case 'isParameterNotPresent': return is_null($parameter);
		}
	}
	
	/** 
	 * @param object query parameter object.
	 * @return mixed value for comparison.
	 */
	protected function getComparisonValue($parameter)
	{
		if(strlen($property = $this->getCompareProperty()) > 0
			&& (is_array($parameter) || is_object($parameter)))
		{
			$value = TPropertyAccess::get($parameter, $property);
		}
		else
			$value = $this->getCompareValue();
		return $this->ensureType($value);
	}
		
	/**
	 * @param object query parameter object
	 * @param string name of the property in the parameter object used for comparison.
	 * @return mixed value for comparison.
	 */
	protected function getPropertyValue($parameter, $property)
	{
		if(!is_null($property) && !is_null($parameter))
		{
			if(is_array($parameter) || is_object($parameter))
				return $this->ensureType(TPropertyAccess::get($parameter, $property));
			else
				return $this->ensureType($parameter);
		}
		else	
			return null;
	}

	/** 
	 * @param string property value
	 * @return mixed enforces property value to type given by {@link getPropertyType PropertyType}.
	 */
	protected function ensureType($value)
	{
		switch(strtolower($this->getPropertyType()))
		{
			case 'boolean':
				return TPropertyValue::ensureBoolean($value);
			case 'integer': case 'int':
				return TPropertyValue::ensureInteger($value);
			case 'float': case 'double':
				return TPropertyValue::ensureFloat($value);
			case 'array':
				return TPropertyValue::ensureArray($value);
			case 'object':
				return TPropertyValue::ensureObject($value);
			default:
				return TPropertyValue::ensureString($value);
		}		
	}	
}

?>