1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) NAVER <http://www.navercorp.com> */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* QueryArgument class |
6
|
|
|
* @author NAVER ([email protected]) |
7
|
|
|
* @package /classes/xml/xmlquery/queryargument |
8
|
|
|
* @version 0.1 |
9
|
|
|
*/ |
10
|
|
|
class QueryArgument |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Argument name |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
var $argument_name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Variable name |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
var $variable_name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Argument validator |
27
|
|
|
* @var QueryArgumentValidator |
28
|
|
|
*/ |
29
|
|
|
var $argument_validator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Column name |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
var $column_name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Table name |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
var $table_name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Operation |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
var $operation; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Ignore value |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
var $ignore_value; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* constructor |
57
|
|
|
* @param object $tag tag object |
58
|
|
|
* @param bool $ignore_value |
59
|
|
|
* @return void |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
function __construct($tag, $ignore_value = FALSE) |
62
|
|
|
{ |
63
|
|
|
static $number_of_arguments = 0; |
64
|
|
|
|
65
|
|
|
$this->argument_name = $tag->attrs->var; |
66
|
|
|
if(!$this->argument_name) |
67
|
|
|
{ |
68
|
|
|
$this->argument_name = str_replace('.', '_', $tag->attrs->name); |
69
|
|
|
} |
70
|
|
|
if(!$this->argument_name) |
71
|
|
|
{ |
72
|
|
|
$this->argument_name = str_replace('.', '_', $tag->attrs->column); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->variable_name = $this->argument_name; |
76
|
|
|
|
77
|
|
|
$number_of_arguments++; |
78
|
|
|
$this->argument_name .= $number_of_arguments; |
79
|
|
|
|
80
|
|
|
$name = $tag->attrs->name; |
81
|
|
|
if(!$name) |
82
|
|
|
{ |
83
|
|
|
$name = $tag->attrs->column; |
84
|
|
|
} |
85
|
|
|
if(strpos($name, '.') === FALSE) |
86
|
|
|
{ |
87
|
|
|
$this->column_name = $name; |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
list($prefix, $name) = explode('.', $name); |
92
|
|
|
$this->column_name = $name; |
93
|
|
|
$this->table_name = $prefix; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if($tag->attrs->operation) |
97
|
|
|
{ |
98
|
|
|
$this->operation = $tag->attrs->operation; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->argument_validator = new QueryArgumentValidator($tag, $this); |
102
|
|
|
$this->ignore_value = $ignore_value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
function getArgumentName() |
106
|
|
|
{ |
107
|
|
|
return $this->argument_name; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
function getColumnName() |
111
|
|
|
{ |
112
|
|
|
return $this->column_name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
function getTableName() |
116
|
|
|
{ |
117
|
|
|
return $this->table_name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function getValidatorString() |
121
|
|
|
{ |
122
|
|
|
return $this->argument_validator->toString(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function isConditionArgument() |
126
|
|
|
{ |
127
|
|
|
if($this->operation) |
128
|
|
|
{ |
129
|
|
|
return TRUE; |
130
|
|
|
} |
131
|
|
|
return FALSE; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Change QueryArgument object to string |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
function toString() |
139
|
|
|
{ |
140
|
|
|
if($this->isConditionArgument()) |
141
|
|
|
{ |
142
|
|
|
// Instantiation |
143
|
|
|
$arg = sprintf("\n" . '${\'%s_argument\'} = new ConditionArgument(\'%s\', %s, \'%s\');' . "\n" |
144
|
|
|
, $this->argument_name |
145
|
|
|
, $this->variable_name |
146
|
|
|
, '$args->' . $this->variable_name |
147
|
|
|
, $this->operation |
148
|
|
|
); |
149
|
|
|
// Call methods to validate argument and ensure default value |
150
|
|
|
$arg .= $this->argument_validator->toString(); |
151
|
|
|
|
152
|
|
|
// Prepare condition string |
153
|
|
|
$arg .= sprintf('${\'%s_argument\'}->createConditionValue();' . "\n" |
154
|
|
|
, $this->argument_name |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
// Check that argument passed validation, else return |
158
|
|
|
$arg .= sprintf('if(!${\'%s_argument\'}->isValid()) return ${\'%s_argument\'}->getErrorMessage();' . "\n" |
159
|
|
|
, $this->argument_name |
160
|
|
|
, $this->argument_name |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
else |
164
|
|
|
{ |
165
|
|
|
$arg = sprintf("\n" . '${\'%s_argument\'} = new Argument(\'%s\', %s);' . "\n" |
166
|
|
|
, $this->argument_name |
167
|
|
|
, $this->variable_name |
168
|
|
|
, $this->ignore_value ? 'NULL' : '$args->{\'' . $this->variable_name . '\'}'); |
169
|
|
|
|
170
|
|
|
$arg .= $this->argument_validator->toString(); |
171
|
|
|
|
172
|
|
|
$arg .= sprintf('if(!${\'%s_argument\'}->isValid()) return ${\'%s_argument\'}->getErrorMessage();' . "\n" |
173
|
|
|
, $this->argument_name |
174
|
|
|
, $this->argument_name |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// If the argument is null, skip it |
179
|
|
|
if($this->argument_validator->isIgnorable()) |
180
|
|
|
{ |
181
|
|
|
$arg = sprintf("if(isset(%s)) {", '$args->' . $this->variable_name) |
182
|
|
|
. $arg |
183
|
|
|
. sprintf("} else\n" . '${\'%s_argument\'} = NULL;', $this->argument_name); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $arg; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
/* End of file QueryArgument.class.php */ |
191
|
|
|
/* Location: ./classes/xml/xmlquery/queryargument/QueryArgument.class.php */ |
192
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.