|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel API client made with HiART. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hipanel-hiart |
|
6
|
|
|
* @package hipanel-hiart |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\hiart; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\hiart\Query; |
|
14
|
|
|
use yii\base\NotSupportedException; |
|
15
|
|
|
use yii\helpers\ArrayHelper; |
|
16
|
|
|
use yii\helpers\Inflector; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* QueryBuilder for HiPanel API. |
|
20
|
|
|
*/ |
|
21
|
|
|
class QueryBuilder extends \hiqdev\hiart\rest\QueryBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
public function buildMethod(Query $query) |
|
24
|
|
|
{ |
|
25
|
|
|
return 'POST'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function buildUri(Query $query) |
|
29
|
|
|
{ |
|
30
|
|
|
$action = $query->action; |
|
31
|
|
|
if (is_array($action)) { |
|
32
|
|
|
$from = reset($action); |
|
33
|
|
|
} else { |
|
34
|
|
|
if (ctype_upper($action[0])) { |
|
35
|
|
|
return $action; |
|
36
|
|
|
} |
|
37
|
|
|
$from = is_array($query->from) ? reset($query->from) : $query->from; |
|
38
|
|
|
} |
|
39
|
|
|
$from = Inflector::id2camel($from); |
|
40
|
|
|
|
|
41
|
|
|
return lcfirst($from . ($query->getOption('batch') ? 's' : '') . $this->buildCommand($query)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function buildCommand(Query $query) |
|
45
|
|
|
{ |
|
46
|
|
|
$action = $query->action; |
|
47
|
|
|
if (is_array($action)) { |
|
48
|
|
|
$action = end($action); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$prefix = ''; |
|
52
|
|
|
if ($action === 'search' && empty($query->getOption('batch'))) { |
|
53
|
|
|
$prefix = 's'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($action === 'insert') { |
|
57
|
|
|
$action = 'create'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $prefix . Inflector::id2camel($action); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function buildFormParams(Query $query) |
|
64
|
|
|
{ |
|
65
|
|
|
return $query->params; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function buildAuth(Query $query) |
|
69
|
|
|
{ |
|
70
|
|
|
$query->addParams($this->db->getAuth()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Query $query |
|
75
|
|
|
* @throws NotSupportedException |
|
76
|
|
|
* @return Query |
|
77
|
|
|
*/ |
|
78
|
|
|
public function prepare(Query $query) |
|
79
|
|
|
{ |
|
80
|
|
|
$parts = []; |
|
81
|
|
|
$query->prepare($this); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$this->buildSelect($query->select, $parts); |
|
84
|
|
|
$this->buildCount($query->count, $parts); |
|
85
|
|
|
$this->buildLimit($query->limit, $parts); |
|
86
|
|
|
$this->buildPage($query->offset, $query->limit, $parts); |
|
87
|
|
|
$this->buildOrderBy($query->orderBy, $parts); |
|
88
|
|
|
|
|
89
|
|
|
$parts = ArrayHelper::merge($parts, $this->buildCondition($query->where), $query->body); |
|
90
|
|
|
|
|
91
|
|
|
$query->addParams($parts); |
|
92
|
|
|
|
|
93
|
|
|
return $query; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function buildCount($count, &$parts) |
|
97
|
|
|
{ |
|
98
|
|
|
if (!empty($count)) { |
|
99
|
|
|
$parts['count'] = 1; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function buildLimit($limit, &$parts) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!empty($limit)) { |
|
106
|
|
|
if ($limit === -1) { |
|
107
|
|
|
$limit = 'ALL'; |
|
108
|
|
|
} |
|
109
|
|
|
$parts['limit'] = $limit; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function buildPage($offset, $limit, &$parts) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($offset > 0) { |
|
116
|
|
|
$parts['page'] = ceil($offset / $limit) + 1; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private $_sort = [ |
|
121
|
|
|
SORT_ASC => '_asc', |
|
122
|
|
|
SORT_DESC => '_desc', |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
public function buildOrderBy($orderBy, &$parts) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!empty($orderBy)) { |
|
128
|
|
|
$parts['orderby'] = key($orderBy) . $this->_sort[reset($orderBy)]; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function buildSelect($select, &$parts) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!empty($select)) { |
|
135
|
|
|
foreach ($select as $attribute) { |
|
136
|
|
|
$parts['select'][$attribute] = $attribute; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: