-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLimelightResource.php
183 lines (156 loc) · 4.58 KB
/
LimelightResource.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
require_once 'lib/restPHP/Resource.php';
require_once 'LimelightServer.php';
class LimelightResource extends restPHP_Resource {
/** The title of this resource. */
public $title = NULL;
/** An array of custom data. */
public $custom_property = NULL;
/**
* Create the server object.
*/
public function createServer($config = array()) {
$this->server = new LimelightServer($config);
}
/**
* Returns the default filter for creating the list of resources.
*/
protected function getIndexDefaults() {
return array(
'page_id' => 0,
'page_size' => 25,
'sort_by' => 'update_date',
'sort_order' => 'asc'
);
}
/**
* Parse function to parse out resources returned by list functions.
*
* @param type $resources
*/
protected function parse($resources, $className) {
// If media_list exists, use it instead.
if (isset($resources->media_list)) {
$resources = $resources->media_list;
}
// If channel_list exists, use it instead.
if (isset($resources->channel_list)) {
$resources = $resources->channel_list;
}
// Parse the resources.
return parent::parse($resources, $className);
}
/**
* Override the getParams to unset the custom_property.
*
* @param type $params
*/
protected function getParams($params = array()) {
$params = parent::getParams($params);
unset($params['custom_property']);
return $params;
}
/**
* Returns the list of resources.
*/
protected function getIndex($endpoint, $filter, $className) {
if (isset($filter['published'])) {
if (!$filter['published']) {
$endpoint .= '/all';
$this->server->setConfig('authenticate', TRUE);
}
unset($filter['published']);
}
$index = parent::getIndex($endpoint, $filter, $className);
$this->server->setConfig('authenticate', FALSE);
return $index;
}
/**
* Returns the endpoint for this resource for both get and set operations.
*/
protected function endpoint($type) {
// Get the endpoint from the parent.
$endpoint = parent::endpoint($type);
// For get and set calls, we append "properties" to the endpoint.
if ($this->id && in_array($type, array('get', 'set'))) {
$endpoint .= '/properties';
}
// Return the endpoint.
return $endpoint;
}
/**
* Perform a set along with custom data.
*/
public function set($params = array()) {
/**
* HACK: Limelight API has a bug where it expects a string 'true' or 'false'
* instead of a boolean value. Because of this, we need to iterate through
* each of the parameters and turn it into the string representation of a
* boolean.
*/
foreach ($params as &$value) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
}
// Set the parent first.
parent::set($params);
// Now set the custom data.
$endpoint = $this->endpoint('set') . '/custom';
$custom = isset($params['custom']) ? $params['custom'] : array();
$this->setProperties('custom_property', $endpoint, $custom, array(
'deleteeach' => TRUE
));
// Return the this pointer.
return $this;
}
/**
* Adds a custom property to this resource.
*/
public function addCustom($name, $type = 'text', $default_values = array()) {
$endpoint = $this->endpoint('set') . '/custom/' . rawurlencode($name);
$params = array();
$params['type'] = $type;
if ($default_values) {
$params['default_values'] = $default_values;
}
$this->server->post($endpoint, $params, FALSE);
return $this;
}
/**
* Removes a custom property of this resource.
*/
public function deleteCustom($name) {
$endpoint = $this->endpoint('set') . '/custom/' . rawurlencode($name);
$this->server->delete($endpoint);
return $this;
}
/**
* Updates a custom property.
*/
public function updateCustom($name, $new_name = '', $type = 'text', $default_values = array()) {
$endpoint = $this->endpoint('set') . '/custom/' . rawurlencode($name);
$params = array();
$params['new_property_name'] = $new_name ? $new_name : $name;
$params['type'] = $type;
if ($default_values) {
$params['default_values'] = $default_values;
}
$this->server->put($endpoint, $params, FALSE);
return $this;
}
/**
* Returns the object sent to the server.
*/
public function getObject() {
// If there isn't any title, then return null to skip the call.
if (!$this->title) {
return false;
}
// Return the object.
return array(
'title' => $this->title
);
}
}
?>