Multimag  0.2.992
multiquery.php
См. документацию.
1 <?php
2 // MultiMag v0.2 - Complex sales system
3 //
4 // Copyright (C) 2005-2018, BlackLight, TND Team, http://tndproject.org
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Affero General Public License as
8 // published by the Free Software Foundation, either version 3 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Affero General Public License for more details.
15 //
16 // You should have received a copy of the GNU Affero General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 namespace api;
20 
22 class multiquery {
23 
24  protected function run($data) {
25  if(!is_array($data)) {
26  throw new \InvalidArgumentException('Отсутствуют параметы запроса');
27  }
28  if(!isset($data['query']) || !is_array($data['query'])) {
29  throw new \InvalidArgumentException('Отсутствуют параметы мультизапроса');
30  }
31  if(count($data['query'])==0) {
32  return [];
33  }
34  $ret = array();
35  $count = 0;
36  foreach($data['query'] as $query) {
37  $sub_data = array();
38  if(isset($data[$query])) {
39  $sub_data = $data[$query];
40  }
41  list($sub_obj, $sub_action) = explode('.', $query);
42  if(!preg_match('/^\\w+$/', $sub_obj)) {
43  throw new \InvalidArgumentException('Некорректный подзапрос');
44  }
45  $class_name = '\\api\\' . $sub_obj;
46  if(!class_exists($class_name)) {
47  throw new \InvalidArgumentException('Отсутствует обработчик для мультизапроса '.$sub_obj);
48  }
49  $class = new $class_name;
50  $ret[$query] = $class->dispatch($sub_action, $sub_data);
51  $count++;
52  }
53  $ret['count'] = $count;
54  return $ret;
55  }
56 
57  public function dispatch($action, $data=null) {
58  switch($action) {
59  case 'run':
60  return $this->run($data);
61  default:
62  throw new \NotFoundException('Некорректное действие');
63  }
64  }
65 }
Definition: agent.php:19
$data
Definition: api.php:27
Обработчик API мультизапросов
Definition: multiquery.php:22
$action
Definition: api.php:26
if(!preg_match('/^\\w+$/', $object) $class_name)
Definition: api.php:44
dispatch($action, $data=null)
Definition: multiquery.php:57