1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The Command class implements execution of request. |
17
|
|
|
*/ |
18
|
|
|
class Command extends \yii\base\Component |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Connection |
22
|
|
|
*/ |
23
|
|
|
public $db; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var RequestInterface request object |
27
|
|
|
*/ |
28
|
|
|
protected $request; |
29
|
|
|
|
30
|
3 |
|
public function setRequest(RequestInterface $request) |
31
|
|
|
{ |
32
|
3 |
|
$this->request = $request; |
33
|
|
|
|
34
|
3 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sends a request to retrieve data. |
39
|
|
|
* In API this could be get, search or list request. |
40
|
|
|
* @throws ErrorResponseException |
41
|
|
|
* @return mixed response data |
42
|
|
|
*/ |
43
|
2 |
|
public function search() |
44
|
|
|
{ |
45
|
2 |
|
$this->request->getQuery()->addAction('search'); |
46
|
|
|
|
47
|
2 |
|
return $this->send(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sends a request to create/insert data. |
52
|
|
|
* @param mixed $table entity to create |
53
|
|
|
* @param mixed $columns attributes of object to create |
54
|
|
|
* @param array $params request parameters |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
1 |
|
public function insert($table, $columns, array $params = []) |
58
|
|
|
{ |
59
|
1 |
|
$request = $this->db->getQueryBuilder()->insert($table, $columns, $params); |
60
|
|
|
|
61
|
1 |
|
return $this->setRequest($request); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sends a request to update data. |
66
|
|
|
* @param mixed $table entity to update |
67
|
|
|
* @param mixed $columns attributes of object to update |
68
|
|
|
* @param array $condition |
69
|
|
|
* @param array $params request parameters |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function update($table, $columns, $condition = [], array $params = []) |
73
|
|
|
{ |
74
|
|
|
$request = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params); |
75
|
|
|
|
76
|
|
|
return $this->setRequest($request); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function delete($table, $condition, array $params = []) |
80
|
|
|
{ |
81
|
|
|
$request = $this->db->getQueryBuilder()->delete($table, $condition, $params); |
82
|
|
|
|
83
|
|
|
return $this->setRequest($request); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Creates and executes request with given data. |
88
|
|
|
* @param string $action |
89
|
|
|
* @param string $table |
90
|
|
|
* @param mixed $body |
91
|
|
|
* @param array $params request parameters |
92
|
|
|
* @return mixed response data |
93
|
|
|
*/ |
94
|
|
|
public function perform($action, $table, $body = [], array $params = []) |
95
|
|
|
{ |
96
|
|
|
$request = $this->db->getQueryBuilder()->perform($action, $table, $body, $params); |
97
|
|
|
$this->setRequest($request); |
98
|
|
|
|
99
|
|
|
return $this->send(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Executes the request. |
104
|
|
|
* @param array $options send options |
105
|
|
|
* @return mixed response data |
106
|
|
|
*/ |
107
|
2 |
|
public function send($options = []) |
108
|
|
|
{ |
109
|
2 |
|
$profile = serialize($this->request); |
110
|
2 |
|
$category = static::getProfileCategory(); |
111
|
2 |
|
Yii::beginProfile($profile, $category); |
112
|
2 |
|
$response = $this->request->send($options); |
113
|
2 |
|
Yii::endProfile($profile, $category); |
114
|
2 |
|
$this->db->checkResponse($response); |
115
|
|
|
|
116
|
2 |
|
return $response->getData(); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
public static function getProfileCategory() |
120
|
|
|
{ |
121
|
2 |
|
return __METHOD__; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|