|
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 AbstractConnection |
|
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
|
|
|
* @param array $options send options |
|
41
|
|
|
* @throws ResponseErrorException |
|
42
|
|
|
* @return mixed response data |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function search($options = []) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$this->request->getQuery()->addAction('search'); |
|
47
|
|
|
|
|
48
|
2 |
|
return $this->send($options); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sends a request to create/insert data. |
|
53
|
|
|
* @param mixed $table entity to create |
|
54
|
|
|
* @param mixed $columns attributes of object to create |
|
55
|
|
|
* @param array $params request parameters |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function insert($table, $columns, array $params = []) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$request = $this->db->getQueryBuilder()->insert($table, $columns, $params); |
|
61
|
|
|
|
|
62
|
1 |
|
return $this->setRequest($request); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Sends a request to update data. |
|
67
|
|
|
* @param mixed $table entity to update |
|
68
|
|
|
* @param mixed $columns attributes of object to update |
|
69
|
|
|
* @param array $condition |
|
70
|
|
|
* @param array $params request parameters |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function update($table, $columns, $condition = [], array $params = []) |
|
74
|
|
|
{ |
|
75
|
|
|
$request = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params); |
|
76
|
|
|
|
|
77
|
|
|
return $this->setRequest($request); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function delete($table, $condition, array $params = []) |
|
81
|
|
|
{ |
|
82
|
|
|
$request = $this->db->getQueryBuilder()->delete($table, $condition, $params); |
|
83
|
|
|
|
|
84
|
|
|
return $this->setRequest($request); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Creates and executes request with given data. |
|
89
|
|
|
* @param string $action |
|
90
|
|
|
* @param string $table |
|
91
|
|
|
* @param mixed $body |
|
92
|
|
|
* @param array $params request parameters |
|
93
|
|
|
* @return mixed response data |
|
94
|
|
|
*/ |
|
95
|
|
|
public function perform($action, $table, $body = [], array $params = []) |
|
96
|
|
|
{ |
|
97
|
|
|
$request = $this->db->getQueryBuilder()->perform($action, $table, $body, $params); |
|
98
|
|
|
$this->setRequest($request); |
|
99
|
|
|
|
|
100
|
|
|
return $this->send(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Executes the request. |
|
105
|
|
|
* @param array $options send options |
|
106
|
|
|
* @return mixed response data |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function send($options = []) |
|
109
|
|
|
{ |
|
110
|
2 |
|
$profile = serialize($this->request); |
|
111
|
2 |
|
$category = static::getProfileCategory(); |
|
112
|
2 |
|
Yii::beginProfile($profile, $category); |
|
113
|
2 |
|
$response = $this->request->send($options); |
|
114
|
2 |
|
Yii::endProfile($profile, $category); |
|
115
|
2 |
|
$this->db->checkResponse($response); |
|
116
|
|
|
|
|
117
|
|
|
return $response->getData(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
public static function getProfileCategory() |
|
121
|
|
|
{ |
|
122
|
2 |
|
return __METHOD__; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|