-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAjaxApi.php
45 lines (36 loc) · 1.11 KB
/
AjaxApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace CTApi\Requests\Traits;
use CTApi\CTClient;
use Psr\Http\Message\ResponseInterface;
trait AjaxApi
{
private array $keyTranslation = [];
protected function setAjaxKeyTranslation($modelKey, $ajaxKey)
{
$this->keyTranslation[$modelKey] = $ajaxKey;
}
/**
* @param string $ajaxQuery e.q. "churchservice/ajax"
* @param string $ajaxFunction e.q. "editArrangement"
* @param array $data model-data
* @return ResponseInterface
*/
protected function requestAjax(string $ajaxQuery, string $ajaxFunction, array $data): ResponseInterface
{
$translatedData = [];
foreach ($data as $key => $value) {
if (array_key_exists($key, $this->keyTranslation)) {
$key = $this->keyTranslation[$key];
}
$translatedData[$key] = $value;
}
$translatedData["func"] = $ajaxFunction;
$client = CTClient::getClient();
return $client->post('/index.php', [
"query" => [
"q" => $ajaxQuery
],
"json" => $translatedData
]);
}
}