1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 Dirk Groenen |
4
|
|
|
* |
5
|
|
|
* (c) Dirk Groenen <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DirkGroenen\Pinterest; |
12
|
|
|
|
13
|
|
|
use DirkGroenen\Pinterest\Auth\PinterestOAuth; |
14
|
|
|
use DirkGroenen\Pinterest\Utils\CurlBuilder; |
15
|
|
|
use DirkGroenen\Pinterest\Transport\Request; |
16
|
|
|
use DirkGroenen\Pinterest\Exceptions\InvalidEndpointException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Boards boards |
20
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Following following |
21
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Pins pins |
22
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Users users |
23
|
|
|
*/ |
24
|
|
|
class Pinterest { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Reference to authentication class instance |
28
|
|
|
* |
29
|
|
|
* @var Auth\PinterestOAuth |
30
|
|
|
*/ |
31
|
|
|
public $auth; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A reference to the request class which travels |
35
|
|
|
* through the application |
36
|
|
|
* |
37
|
|
|
* @var Transport\Request |
38
|
|
|
*/ |
39
|
|
|
public $request; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* A array containing the cached endpoints |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $cachedEndpoints = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor |
50
|
|
|
* |
51
|
|
|
* @param string $client_id |
52
|
|
|
* @param string $client_secret |
53
|
|
|
* @param CurlBuilder $curlbuilder |
54
|
|
|
*/ |
55
|
37 |
|
public function __construct($client_id, $client_secret, $curlbuilder = null) |
56
|
|
|
{ |
57
|
37 |
|
if ($curlbuilder == null) { |
58
|
|
|
$curlbuilder = new CurlBuilder(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Create new instance of Transport\Request |
62
|
37 |
|
$this->request = new Request($curlbuilder); |
63
|
|
|
|
64
|
|
|
// Create and set new instance of the OAuth class |
65
|
37 |
|
$this->auth = new PinterestOAuth($client_id, $client_secret, $this->request); |
66
|
37 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get an Instagram API endpoint |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @param string $endpoint |
73
|
|
|
* @return mixed |
74
|
|
|
* @throws Exceptions\InvalidEndpointException |
75
|
|
|
*/ |
76
|
33 |
|
public function __get($endpoint) |
77
|
|
|
{ |
78
|
33 |
|
$endpoint = strtolower($endpoint); |
79
|
33 |
|
$class = "\\DirkGroenen\\Pinterest\\Endpoints\\" . ucfirst($endpoint); |
80
|
|
|
|
81
|
|
|
// Check if an instance has already been initiated |
82
|
33 |
|
if (!isset($this->cachedEndpoints[$endpoint])) { |
83
|
|
|
// Check endpoint existence |
84
|
33 |
|
if (!class_exists($class)) { |
85
|
|
|
throw new InvalidEndpointException; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Create a reflection of the called class and initialize it |
89
|
|
|
// with a reference to the request class |
90
|
33 |
|
$ref = new \ReflectionClass($class); |
91
|
33 |
|
$obj = $ref->newInstanceArgs([$this->request, $this]); |
92
|
|
|
|
93
|
33 |
|
$this->cachedEndpoints[$endpoint] = $obj; |
94
|
33 |
|
} |
95
|
|
|
|
96
|
33 |
|
return $this->cachedEndpoints[$endpoint]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get rate limit from the headers |
101
|
|
|
* |
102
|
|
|
* @access public |
103
|
|
|
* @return integer |
104
|
|
|
*/ |
105
|
1 |
|
public function getRateLimit() |
106
|
|
|
{ |
107
|
1 |
|
$header = $this->request->getHeaders(); |
108
|
1 |
|
return (isset($header['X-Ratelimit-Limit']) ? $header['X-Ratelimit-Limit'] : 1000); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get rate limit remaining from the headers |
113
|
|
|
* |
114
|
|
|
* @access public |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
1 |
|
public function getRateLimitRemaining() |
118
|
|
|
{ |
119
|
1 |
|
$header = $this->request->getHeaders(); |
120
|
1 |
|
return (isset($header['X-Ratelimit-Remaining']) ? $header['X-Ratelimit-Remaining'] : 'unknown'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|