-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgolia.php
executable file
·70 lines (62 loc) · 1.74 KB
/
algolia.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace PMVC\PlugIn\algolia;
use IdOfThings\GetModel;
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\algolia';
\PMVC\initPlugin(['guid'=>null]);
\PMVC\l(__DIR__.'/src/BaseAlgolia.php');
/**
* @parameters string app
* @parameters string key
*/
class algolia extends GetModel
{
public function init()
{
if (!isset($this['app'])) {
return !trigger_error('Not set algolia app.');
}
if (!isset($this['key'])) {
return !trigger_error('Not set algolia key.');
}
$this->setConnected(true);
}
public function getBaseModel()
{
return __NAMESPACE__.'\BaseAlgolia';
}
public function getNameSpace()
{
return __NAMESPACE__;
}
public function getBaseUrl()
{
return 'https://'.$this['app'].'.algolia.net/1';
}
public function request($url, $params=[])
{
$respond = null;
$url->set($this->getBaseUrl());
$header = [
'X-Algolia-Application-Id: '.$this['app'],
'X-Algolia-API-Key: '.$this['key']
];
$params[CURLOPT_HTTPHEADER] = array_merge(
\PMVC\value($params,[CURLOPT_HTTPHEADER],[]),
$header
);
$curl = \PMVC\plug('curl');
$curl->get((string)$url,function($r) use (&$respond, $url){
if (500 > $r->code) {
$respond = (object)[
'header' => $r->header,
'body' => \PMVC\fromJson($r->body),
'code' => $r->code
];
} else {
return !trigger_error('Get result error. Error Code:'.$r->code. ' url: '.$url);
}
})->set($params);
$curl->process();
return $respond;
}
}