1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ActiveRecord for API |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, 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 ResponseInterface response object |
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
|
|
|
/** |
81
|
|
|
* Sends a request to delete data. |
82
|
|
|
* @param mixed $table entity to update |
83
|
|
|
* @param array $condition |
84
|
|
|
* @param array $params request parameters |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function delete($table, $condition, array $params = []) |
88
|
|
|
{ |
89
|
|
|
$request = $this->db->getQueryBuilder()->delete($table, $condition, $params); |
90
|
|
|
|
91
|
|
|
return $this->setRequest($request); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Creates and executes request with given data. |
96
|
|
|
* @param string $action |
97
|
|
|
* @param string $table |
98
|
|
|
* @param mixed $body |
99
|
|
|
* @param array $params request parameters |
100
|
|
|
* @return ResponseInterface response object |
101
|
|
|
*/ |
102
|
|
|
public function perform($action, $table, $body = [], array $params = []) |
103
|
|
|
{ |
104
|
|
|
$request = $this->db->getQueryBuilder()->perform($action, $table, $body, $params); |
105
|
|
|
$this->setRequest($request); |
106
|
|
|
|
107
|
|
|
return $this->send(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Executes the request. |
112
|
|
|
* @param array $options send options |
113
|
|
|
* @return ResponseInterface response object |
114
|
|
|
*/ |
115
|
2 |
|
public function send($options = []) |
116
|
|
|
{ |
117
|
2 |
|
$profile = serialize($this->request); |
118
|
2 |
|
$category = static::getProfileCategory(); |
119
|
2 |
|
Yii::beginProfile($profile, $category); |
120
|
2 |
|
$response = $this->request->send($options); |
121
|
2 |
|
Yii::endProfile($profile, $category); |
122
|
2 |
|
$this->db->checkResponse($response); |
123
|
|
|
|
124
|
2 |
|
return $response; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
public static function getProfileCategory() |
128
|
|
|
{ |
129
|
2 |
|
return __METHOD__; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|