Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
parseFamilyFunction.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
8 {
9 
10  public $functionName = '';
11  public $appName = '';
12  public $funcCall = '';
13  public $inputString = '';
14  public $outputString = '';
15  /**
16  * @var inputArgument[]
17  */
18  public $inputs = array();
19  public $outputs = array();
20  protected $error = '';
21  protected $firstParenthesis;
22  protected $lastParenthesis;
23  protected $lastSemiColumn;
24 
25  public function getError()
26  {
27  return $this->error;
28  }
29 
30  protected function setError($error)
31  {
32  return $this->error = $error;
33  }
34 
35  protected function initParse($funcCall)
36  {
37  $this->funcCall = $funcCall;
38  $this->firstParenthesis = strpos($funcCall, '(');
39  $this->lastParenthesis = strrpos($funcCall, ')');
40  $this->lastSemiColumn = strrpos($funcCall, ':');
41  $this->inputs = array();
42  $this->outputs = array();
43  $this->outputString = '';
44  $this->inputString = '';
45  }
46 
47  protected function checkParenthesis()
48  {
49  if (($this->firstParenthesis === false) || ($this->lastParenthesis === false) || ($this->firstParenthesis >= $this->lastParenthesis)) {
50  $this->setError(ErrorCode::getError('ATTR1201', $this->funcCall));
51  return false;
52  }
53 
54  if ($this->lastSemiColumn > $this->lastParenthesis) {
55  $spaceUntil = $this->lastSemiColumn;
56  } else {
57  $spaceUntil = strlen($this->funcCall);
58  }
59 
60  for ($i = $this->lastParenthesis + 1; $i < $spaceUntil; $i++) {
61  $c = $this->funcCall[$i];
62  if ($c != ' ') {
63  $this->setError(ErrorCode::getError('ATTR1201', $this->funcCall));
64  return false;
65  }
66  }
67 
68  return true;
69  }
70  /**
71  * @static
72  * @param $methCall
73  * @return parseFamilyFunction
74  */
75  public function parse($methCall, $noOut = false)
76  {
77 
78  $this->initParse($methCall);
79  $funcName = trim(substr($methCall, 0, $this->firstParenthesis));
80  if (strpos($funcName, ':')) {
81  list($appName, $funcName) = explode(':', $funcName, 2);
82  } else $appName = '';
83 
84  if ($this->checkParenthesis()) {
85  if ((!$noOut) && ($this->lastSemiColumn < $this->lastParenthesis)) {
86  $this->setError(ErrorCode::getError('ATTR1206', $methCall));
87  } else {
88 
89  if (!$this->isPHPName($funcName)) {
90  $this->setError(ErrorCode::getError('ATTR1202', $funcName));
91  } elseif (!preg_match('/^[a-z0-9_]*$/i', $appName)) {
92  $this->setError(ErrorCode::getError('ATTR1202', $funcName));
93  } else {
94  $this->functionName = $funcName;
95  $this->appName = $appName;
96  $inputString = substr($methCall, $this->firstParenthesis + 1, ($this->lastParenthesis - $this->firstParenthesis - 1));
97  $this->inputString = $inputString;
98 
99  $this->parseArguments();
100  $this->parseOutput();
101  }
102  }
103  }
104 
105  return $this;
106  }
107 
108  protected function parseArguments()
109  {
110  $args = array();
111  $types = array();
112  $ak = 0;
113  $bq = '';
114  for ($i = 0; $i < strlen($this->inputString); $i++) {
115  $c = $this->inputString[$i];
116 
117  if ($c == '"') {
118  $this->parseDoubleQuote($i);
119  } elseif ($c == "'") {
120  $this->parseSimpleQuote($i);
121  } elseif ($c == ',') {
122  } elseif ($c == ' ') {
123  // skip
124 
125  } else {
126  $this->parseArgument($i);
127  }
128  }
129  }
130 
131  protected function parseOutput()
132  {
133  if ($this->lastSemiColumn > $this->lastParenthesis) {
134  $this->outputString = trim(substr($this->funcCall, $this->lastSemiColumn + 1));
135  }
136  if ($this->outputString) {
137  $this->outputs = explode(',', $this->outputString);
138  foreach ($this->outputs as & $output) {
139  $output = trim($output);
140  if (!$this->isAlphaNumOutAttribute($output)) {
141  $this->setError(ErrorCode::getError('ATTR1207', $this->funcCall));
142  }
143  }
144  }
145  }
146 
147  protected function isAlphaNum($s)
148  {
149  return preg_match('/^[a-z_][a-z0-9_]*$/i', $s);
150  }
151 
152  protected function isAlphaNumOutAttribute($s)
153  {
154  return preg_match('/^[a-z_\?][a-z0-9_\[\]]*$/i', $s);
155  }
156  protected function isPHPName($s)
157  {
158  return preg_match('/^[a-z_][a-z0-9_]*$/i', $s);
159  }
160  protected function isPHPClassName($s)
161  {
162  return preg_match('/^([a-z_][a-z0-9_]*\\\\)*[a-z_][a-z0-9_]*$/i', $s);
163  }
164 
165  private function gotoNextArgument(&$index)
166  {
167  for ($i = $index; $i < strlen($this->inputString); $i++) {
168 
169  $c = $this->inputString[$i];
170 
171  if ($c == ',') {
172  break;
173  } elseif ($c == " ") {
174  //skip
175 
176  } else {
177  $this->setError($this->setError(ErrorCode::getError('ATTR1204', strlen($this->functionName) + 1 + $i, $this->funcCall)));
178  }
179  }
180  $index = $i;
181  }
182  /**
183  * analyze single misc argument
184  * @param int $index index to start analysis string
185  * @return void
186  */
187  protected function parseArgument(&$index)
188  {
189  $arg = '';
190  for ($i = $index; $i < strlen($this->inputString); $i++) {
191 
192  $c = $this->inputString[$i];
193 
194  if ($c == ',') {
195  break;
196  } else {
197  $arg.= $c;
198  }
199  }
200  $index = $i;
201  $arg = trim($arg);
202 
203  if (preg_match('/^[a-z_][a-z0-9_]*$/i', $arg)) {
204  $type = "any";
205  } else {
206  $type = "string";
207  }
208 
209  $this->inputs[] = new inputArgument($arg, $type);
210  }
211  /**
212  * analyze single double quoted text argument
213  * @param int $index index to start analysis string
214  * @return void
215  */
216  protected function parseDoubleQuote(&$index)
217  {
218  $arg = '';
219  $doubleQuoteDetected = false;
220  $c = $this->inputString[$index];
221  if ($c != '"') {
222  $this->setError($this->setError(ErrorCode::getError('ATTR1204', strlen($this->functionName) + 1 + $index, $this->funcCall)));
223  }
224  for ($i = $index + 1; $i < strlen($this->inputString); $i++) {
225  $cp = $c;
226  $c = $this->inputString[$i];
227 
228  if ($c == '"') {
229  if ($cp == '\\') {
230  $arg = substr($arg, 0, -1);
231  $arg.= $c;
232  } else {
233  $doubleQuoteDetected = true;
234  break;
235  }
236  } else {
237  $arg.= $c;
238  }
239  }
240  $index = $i;
241 
242  if (!$doubleQuoteDetected) $this->setError($this->setError(ErrorCode::getError('ATTR1204', strlen($this->functionName) + 1 + $index, $this->funcCall)));
243  else {
244  $index++;
245  $this->gotoNextArgument($index);
246  }
247  $this->inputs[] = new inputArgument($arg, "string");;
248  }
249  /**
250  * analyze single simple quoted text argument
251  * @param int $index index to start analysis string
252  * @return void
253  */
254  protected function parseSimpleQuote(&$index)
255  {
256  $arg = '';
257 
258  $c = $this->inputString[$index];
259  if ($c != "'") {
260  $this->setError($this->setError(ErrorCode::getError('ATTR1205', strlen($this->functionName) + 1 + $index, $this->funcCall)));
261  }
262 
263  for ($i = $index + 1; $i < strlen($this->inputString); $i++) {
264  $cp = $c;
265  $c = $this->inputString[$i];
266 
267  if ($c == "'") {
268  if ($cp == '\\') {
269  $arg = substr($arg, 0, -1);
270  $arg.= $c;
271  } else {
272  $arg.= $c;
273  }
274  } elseif ($c == ',') {
275  break;
276  } else {
277  $arg.= $c;
278  }
279  }
280  $r = strlen($arg) - 1;
281  $c = ($r >= 0) ? $arg[$r] : '';
282  while ($c == ' ' && $r > 0) {
283  $r--;
284  $c = $arg[$r];
285  }
286  if ($c == "'") {
287  $arg = substr($arg, 0, $r);
288  }
289  $index = $i;
290 
291  $this->inputs[] = new inputArgument($arg, "string");
292  }
293 }
294 
296 {
297  public $name = '';
298  public $type = 'any';
299 
300  function __construct($name = '', $type = 'any')
301  {
302  $this->name = $name;
303  $this->type = $type;
304  }
305 }
306 
if(substr($wsh, 0, 1)!= '/') $args
$s type
Definition: dav.php:73
print $fam getTitle() $fam name
if($famId) $s
static getError($code, $args=null)
Definition: ErrorCode.php:27
__construct($name= '', $type= 'any')
parse($methCall, $noOut=false)
← centre documentaire © anakeen