1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EdyWladson\VistaSoft; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class VistaSoft |
11
|
|
|
* @package EdyWladson\VistaSoft |
12
|
|
|
*/ |
13
|
|
|
class VistaSoft |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
private $apiUrl; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $apiKey; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $build; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $showtotal; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $immobileId; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $clientId; |
32
|
|
|
|
33
|
|
|
/** @var mixed */ |
34
|
|
|
private $callback; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* VistaSoft constructor. |
38
|
|
|
* @param string $apiUrl |
39
|
|
|
* @param string $apiKey |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $apiUrl, string $apiKey) |
42
|
|
|
{ |
43
|
|
|
$this->apiUrl = $apiUrl; |
44
|
|
|
$this->apiKey = $apiKey; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $fields |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function fields(array $fields): VistaSoft |
52
|
|
|
{ |
53
|
|
|
$this->build = [ |
54
|
|
|
"fields" => $fields |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $leads |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function leads(array $leads): VistaSoft |
65
|
|
|
{ |
66
|
|
|
$this->build = [ |
67
|
|
|
"lead" => $leads |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $filterBy |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function filter(array $filterBy): VistaSoft |
78
|
|
|
{ |
79
|
|
|
if(empty($this->build)){ |
80
|
|
|
$this->build = [ |
81
|
|
|
"filter" => $filterBy |
82
|
|
|
]; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->build += [ |
87
|
|
|
"filter" => $filterBy |
88
|
|
|
]; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $orderBy |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function order(array $orderBy): VistaSoft |
97
|
|
|
{ |
98
|
|
|
$this->build += [ |
99
|
|
|
"order" => $orderBy |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $page |
107
|
|
|
* @param int $quantity |
108
|
|
|
* @param bool $total |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function paginator(int $page = 1, int $quantity = 20, bool $total = false): VistaSoft |
112
|
|
|
{ |
113
|
|
|
$this->showtotal = ($total) ? "&showtotal=1" : ''; |
114
|
|
|
$this->build += [ |
115
|
|
|
"paginacao" => [ |
116
|
|
|
"pagina" => $page, |
117
|
|
|
"quantidade" => $quantity |
118
|
|
|
] |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param int $id |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function clientId(int $id): VistaSoft |
129
|
|
|
{ |
130
|
|
|
$this->clientId = "&cliente={$id}"; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $id |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function immobileId(int $id): VistaSoft |
139
|
|
|
{ |
140
|
|
|
$this->immobileId = "&imovel={$id}"; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
public function callback() |
148
|
|
|
{ |
149
|
|
|
return $this->callback; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* GET |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function get($endpoint): VistaSoft |
157
|
|
|
{ |
158
|
|
|
if ($endpoint[0] != "/"){ |
159
|
|
|
$endpoint = substr_replace($endpoint, "/", 0, 0); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$url = "{$this->apiUrl}{$endpoint}?key={$this->apiKey}"; |
163
|
|
|
$fields = json_encode($this->build); |
164
|
|
|
$url .= "&pesquisa={$fields}{$this->showtotal}{$this->clientId}{$this->immobileId}"; |
165
|
|
|
|
166
|
|
|
$ch = curl_init($url); |
167
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
168
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']); |
169
|
|
|
$this->callback = json_decode(curl_exec($ch)); |
|
|
|
|
170
|
|
|
curl_close($ch); |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* POST |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function post($endpoint): VistaSoft |
179
|
|
|
{ |
180
|
|
|
if ($endpoint[0] != "/"){ |
181
|
|
|
$endpoint = substr_replace($endpoint, "/", 0, 0); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$url = "{$this->apiUrl}{$endpoint}?key={$this->apiKey}"; |
185
|
|
|
$fields = "cadastro=" . json_encode($this->build); |
186
|
|
|
$url .= "{$this->clientId}{$this->immobileId}"; |
187
|
|
|
|
188
|
|
|
$ch = curl_init($url); |
189
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
190
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
191
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); |
192
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
193
|
|
|
'Content-type: application/x-www-form-urlencoded', |
194
|
|
|
'Accept: application/json', |
195
|
|
|
'Content-Length: ' . strlen($fields) |
196
|
|
|
]); |
197
|
|
|
$this->callback = json_decode(curl_exec($ch)); |
|
|
|
|
198
|
|
|
curl_close($ch); |
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* PUT |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function put($endpoint): VistaSoft |
207
|
|
|
{ |
208
|
|
|
if ($endpoint[0] != "/"){ |
209
|
|
|
$endpoint = substr_replace($endpoint, "/", 0, 0); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$url = "{$this->apiUrl}{$endpoint}?key={$this->apiKey}"; |
213
|
|
|
$fields = "cadastro=" . json_encode($this->build); |
214
|
|
|
$url .= "{$this->clientId}{$this->immobileId}"; |
215
|
|
|
|
216
|
|
|
$ch = curl_init($url); |
217
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
218
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
219
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); |
220
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
221
|
|
|
'Content-type: application/x-www-form-urlencoded', |
222
|
|
|
'Accept: application/json', |
223
|
|
|
'Content-Length: ' . strlen($fields) |
224
|
|
|
]); |
225
|
|
|
$this->callback = json_decode(curl_exec($ch)); |
|
|
|
|
226
|
|
|
curl_close($ch); |
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
} |