|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
|
7
|
|
|
* @package yii2-hiart |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\hiart; |
|
13
|
|
|
|
|
14
|
|
|
use yii\base\Component; |
|
15
|
|
|
use yii\helpers\ArrayHelper; |
|
16
|
|
|
use yii\helpers\Inflector; |
|
17
|
|
|
use yii\helpers\Json; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The Command class implements the API for accessing REST API. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Command extends Component |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Connection |
|
26
|
|
|
*/ |
|
27
|
|
|
public $db; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string|array the indexes to execute the query on. Defaults to null meaning all indexes |
|
30
|
|
|
*/ |
|
31
|
|
|
public $index; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string|array the types to execute the query on. Defaults to null meaning all types |
|
34
|
|
|
*/ |
|
35
|
|
|
public $type; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array list of arrays or json strings that become parts of a query |
|
38
|
|
|
*/ |
|
39
|
|
|
public $queryParts = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sends a request to the _search API and returns the result. |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
* @throws ErrorResponseException |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function search($options = []) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$url = $this->index . Inflector::id2camel(ArrayHelper::remove($options, 'scenario', 'search')); |
|
50
|
1 |
|
$query = $this->queryParts; |
|
51
|
1 |
|
$options = array_merge($query, $options); |
|
52
|
|
|
|
|
53
|
1 |
|
return $this->db->post($url, $options); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getList($options = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$options = array_merge($this->queryParts, $options); |
|
59
|
|
|
$command = $this->index . 'GetList'; |
|
60
|
|
|
$result = $this->db->post($command, $options); |
|
61
|
|
|
|
|
62
|
|
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function insert($action, $data, $id = null, $options = []) |
|
66
|
|
|
{ |
|
67
|
|
|
$options = array_merge($data, $options); |
|
68
|
|
|
|
|
69
|
|
|
if ($id !== null) { |
|
70
|
|
|
return $this->db->put($action . 'Update', array_merge($options, ['id' => $id])); |
|
71
|
|
|
} else { |
|
72
|
|
|
return $this->db->post($action . 'Create', $options); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function get($modelName, $primaryKey, $options) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->db->post($modelName . 'GetInfo', ArrayHelper::merge(['id' => $primaryKey], $options)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function mget($index, $type, $ids, $options = []) |
|
82
|
|
|
{ |
|
83
|
|
|
$body = Json::encode(['ids' => array_values($ids)]); |
|
84
|
|
|
|
|
85
|
|
|
return $this->db->post([$index, $type, '_mget'], $options, $body); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function exists($index, $type, $id) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->db->head([$index, $type, $id]); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function delete($index, $id, $options = []) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->db->delete($index . 'Delete', array_merge($options, ['id' => $id])); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function update($index, $id, $data, $options = []) |
|
99
|
|
|
{ |
|
100
|
|
|
$options['id'] = $id; |
|
101
|
|
|
|
|
102
|
|
|
return $this->db->put($index . 'Update', array_merge($data, $options)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $action |
|
107
|
|
|
* @param mixed $body request parameters |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function perform($action, $body = []) |
|
111
|
|
|
{ |
|
112
|
1 |
|
return $this->db->post($action, [], $body); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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: