-
Notifications
You must be signed in to change notification settings - Fork 0
/
delicious.php
155 lines (129 loc) · 3.01 KB
/
delicious.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
<?php
/**
* Laravel Delicious Bundle
*
* @category Bundle
* @package Laravel
* @author Brian Drelling <http://briandrelling.com>
* @copyright 2013 Brian Drelling
* @license MIT License http://www.opensource.org/licenses/mit-license.php
* @version Release: 1.0
* @link https://github.com/bdrelling/Laravel-Delicious
*/
namespace Delicious;
class Delicious {
public $username;
protected $sort = NULL;
protected $tags = NULL;
protected $take = NULL;
function __construct($username)
{
$this->username = $username;
}
/**
* initialize and set the username
*
* @param string username
* @return object
*/
public static function init($username)
{
return new static($username);
}
/**
* turns on the alphanumeric sort
*
* @return object
*/
public function sort()
{
$this->sort = true;
return $this;
}
/**
* sets the tags to return
*
* @param string tags
* @return object
*/
public function tags($tags)
{
$this->tags = $tags;
return $this;
}
/**
* set the count of results to take
*
* @param integer count
* @return object
*/
public function take($count)
{
$count = ($count > 100) ? 100 : (($count < 0) ? 0 : $count);
$this->take = $count;
return $this;
}
/**
* resets all variables
*
*/
function reset()
{
$this->sort = NULL;
$this->tags = NULL;
$this->take = NULL;
}
/**
* get the bookmarks
*
* @return array bookmarks
*/
function get()
{
$url = 'https://api.del.icio.us/v2/json/'.$this->username;
if(isset($this->tags)) {
$url .= '/'.$this->tags;
}
if(isset($this->take)) {
$url .= '?count='.$this->take;
}
$result = $this->_get_data($url);
if (isset($this->sort)) {
$result = $this->_sort_data($result);
}
return $result;
}
/**
* retrieve data from API
*
* @param string url
* @return array api results
*/
protected function _get_data($url)
{
if(function_exists("curl_version"))
{
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($c);
}
return json_decode($result);
}
/**
* sorts data retrieved from the API by name
*
* @param string param
* @return array bookmarks
*/
protected function _sort_data($bookmarks)
{
usort($bookmarks, function ($a, $b) {
$s = strcasecmp($a->d, $b->d);
if ($s == 0) { return 0; }
if ($s > 0) { return 1; }
if ($s < 0) { return -1; }
});
return $bookmarks;
}
}