-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLuzmo.php
111 lines (95 loc) · 3.27 KB
/
Luzmo.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Luzmo;
class Luzmo {
private $host;
private $port = '443';
private $apiVersion = '0.1.0';
private $apiKey;
private $apiToken;
private $format;
public function __construct() {
}
public static function initialize($apiKey, $apiToken, $host = 'https://api.luzmo.com', $format = 'array') {
$instance = new self();
$instance->apiKey = $apiKey;
$instance->apiToken = $apiToken;
$instance->host = $host ? $host : 'https://api.luzmo.com';
$instance->format = ($format === 'object' || $format === 'array') ? $format : 'array';
return $instance;
}
public function create($resource, $properties, $associations = array()) {
return $this->_emit($resource, 'POST', array(
'action' => 'create',
'properties' => $properties,
'associations' => $associations
));
}
public function get($resource, $filter) {
return $this->_emit($resource, 'SEARCH', array(
'action' => 'get',
'find' => $filter
));
}
public function delete($resource, $id, $properties = array()) {
return $this->_emit($resource, 'DELETE', array(
'action' => 'delete',
'id' => $id,
'properties' => $properties
));
}
public function update($resource, $id, $properties) {
return $this->_emit($resource, 'PATCH', array(
'action' => 'update',
'id' => $id,
'properties' => $properties
));
}
public function associate($resource, $id, $associationRole, $associationId, $properties = array()) {
return $this->_emit($resource, 'LINK', array(
'action' => 'associate',
'id' => $id,
'resource' => array('role' => $associationRole, 'id' => $associationId),
'properties' => $properties
));
}
public function dissociate($resource, $id, $associationRole, $associationId) {
return $this->_emit($resource, 'UNLINK', array(
'action' => 'dissociate',
'id' => $id,
'resource' => array('role' => $associationRole, 'id' => $associationId)
));
}
public function query($filter) {
return $this->get('data', $filter);
}
public function _emit($resource, $action, $query) {
$url = $this->host . ':' . $this->port . '/' . $this->apiVersion . '/' . $resource;
$query['key'] = $this->apiKey;
$query['token'] = $this->apiToken;
$query['version'] = $this->apiVersion;
$payload = json_encode($query);
$curl = curl_init();
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => $action,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
),
CURLOPT_POSTFIELDS => $payload
);
curl_setopt_array($curl, $curl_options);
$response = curl_exec($curl);
$content_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
// Check content type and handle accordingly
if (strpos($content_type, 'application/json') === false) {
// Non-JSON response (e.g. image or PDF export) -> return as such
return $response;
}
else {
// JSON response from Luzmo API -> decode
return json_decode($response, $this->format === 'array');
}
}
}