1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Habrahabr\Api; |
4
|
|
|
|
5
|
|
|
use Habrahabr\Api\Exception\ResourceNotExistsException; |
6
|
|
|
use Habrahabr\Api\HttpAdapter\HttpAdapterInterface; |
7
|
|
|
use Habrahabr\Api\Resources\ResourceInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Client |
11
|
|
|
* |
12
|
|
|
* Основной класс для получения доступа к классам Habrahabr Api ресурсов |
13
|
|
|
* |
14
|
|
|
* @package Habrahabr\Api |
15
|
|
|
* @version 0.1.0 |
16
|
|
|
* @author thematicmedia <[email protected]> |
17
|
|
|
* @link https://tmtm.ru/ |
18
|
|
|
* @link https://habrahabr.ru/ |
19
|
|
|
* @link https://github.com/thematicmedia/habrahabr_api |
20
|
|
|
* @method Resources\UserResource getUserResource() |
21
|
|
|
* @method Resources\SearchResource getSearchResource() |
22
|
|
|
* @method Resources\PostResource getPostResource() |
23
|
|
|
* @method Resources\HubResource getHubResource() |
24
|
|
|
* @method Resources\FeedResource getFeedResource() |
25
|
|
|
* @method Resources\FlowResource getFlowResource() |
26
|
|
|
* @method Resources\CompanyResource getCompanyResource() |
27
|
|
|
* @method Resources\CommentsResource getCommentsResource() |
28
|
|
|
* @method Resources\TrackerResource getTrackerResource() |
29
|
|
|
* |
30
|
|
|
* For the full copyright and license information, please view the LICENSE |
31
|
|
|
* file that was distributed with this source code. |
32
|
|
|
*/ |
33
|
|
|
class Client |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @type HttpAdapterInterface|null Экземпляр Habrahabr Api HTTP адаптера |
37
|
|
|
*/ |
38
|
|
|
protected $adapter = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @type array Контейнер для хранения экземпляров классов ресурсов |
42
|
|
|
*/ |
43
|
|
|
protected $resources = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Client constructor. |
47
|
|
|
* |
48
|
|
|
* @param HttpAdapterInterface $adapter Экземпляр Habrahabr Api HTTP адаптера |
49
|
|
|
*/ |
50
|
12 |
|
public function __construct(HttpAdapterInterface $adapter) |
51
|
|
|
{ |
52
|
12 |
|
$this->adapter = $adapter; |
53
|
12 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Возращает экземпляр ресурса для работы с Habrahabr Api |
57
|
|
|
* |
58
|
|
|
* @param string $name Название метода по шаблону get[Ресурс]Resource |
59
|
|
|
* @param array $arguments Список передаваемых экземпляру аргументов |
60
|
|
|
* @return ResourceInterface |
61
|
|
|
* @throws ResourceNotExistsException |
62
|
|
|
*/ |
63
|
11 |
|
public function __call($name, $arguments) |
64
|
|
|
{ |
65
|
11 |
|
if (preg_match('#^get([\w]+)Resource$#i', $name, $m)) { |
66
|
10 |
|
$name = ucfirst($m[1]) . 'Resource'; |
67
|
|
|
|
68
|
10 |
|
if (!isset($this->resources[$name])) { |
69
|
10 |
|
$this->resources[$name] = $this->createResourceInstance($name); |
70
|
9 |
|
} |
71
|
|
|
|
72
|
9 |
|
return $this->resources[$name]; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
throw new ResourceNotExistsException('Method ' . $name . ' not implemented'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Возращает URL для OAuth авторизации Habrahabr Api |
80
|
|
|
* |
81
|
|
|
* @param string $redirect_uri OAuth Redirect URL |
82
|
|
|
* @param string $response_type OAuth Response type |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getAuthorizationUrl($redirect_uri, $response_type = 'code') |
86
|
|
|
{ |
87
|
1 |
|
return sprintf('https://%s/auth/o/login/?%s', |
88
|
1 |
|
str_replace('api.', '', parse_url($this->adapter->getEndpoint(), PHP_URL_HOST)), |
89
|
1 |
|
http_build_query([ |
90
|
1 |
|
'response_type' => $response_type, |
91
|
1 |
|
'client_id' => $this->adapter->getClient(), |
92
|
1 |
|
'redirect_uri' => $redirect_uri, |
93
|
1 |
|
]) |
94
|
1 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Создания экземпляра ресурса для работы с Habrahabr Api |
99
|
|
|
* |
100
|
|
|
* @param string $name Название класса для инициализации |
101
|
|
|
* @return ResourceInterface |
102
|
|
|
* @throws ResourceNotExistsException |
103
|
|
|
*/ |
104
|
10 |
|
protected function createResourceInstance($name) |
105
|
|
|
{ |
106
|
10 |
|
$classname = '\\Habrahabr\\Api\\Resources\\' . $name; |
107
|
|
|
|
108
|
10 |
|
if (!class_exists($classname)) { |
109
|
1 |
|
throw new ResourceNotExistsException($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
9 |
|
$resource = new $classname(); |
113
|
9 |
|
$resource->setAdapter($this->adapter); |
114
|
|
|
|
115
|
9 |
|
return $resource; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|