-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGitHub.php
84 lines (74 loc) · 2.32 KB
/
GitHub.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
<?php
/**
* Created by PhpStorm.
* User: Exodus 4D
* Date: 28.01.2019
* Time: 22:38
*/
namespace Exodus4D\ESI\Client\GitHub;
use Exodus4D\ESI\Client;
use Exodus4D\ESI\Config\ConfigInterface;
use Exodus4D\ESI\Config\GitHub\Config;
use Exodus4D\ESI\Lib\RequestConfig;
use Exodus4D\ESI\Lib\WebClient;
use Exodus4D\ESI\Mapper;
class GitHub extends Client\AbstractApi implements GitHubInterface {
/**
* @param string $projectName e.g. "exodus4d/pathfinder"
* @param int $count
* @return RequestConfig
*/
protected function getProjectReleasesRequest(string $projectName, int $count = 1) : RequestConfig {
$requestOptions = [
'query' => [
'page' => 1,
'per_page' => $count
]
];
return new RequestConfig(
WebClient::newRequest('GET', $this->getConfig()->getEndpoint(['releases', 'GET'], [$projectName])),
$requestOptions,
function($body) : array {
$releasesData = [];
if(!$body->error){
foreach((array)$body as $data){
$releasesData[] = (new Mapper\GitHub\Release($data))->getData();
}
}
return $releasesData;
}
);
}
/**
* @param string $context
* @param string $markdown
* @return RequestConfig
*/
protected function markdownToHtmlRequest(string $context, string $markdown) : RequestConfig {
$requestOptions = [
'json_enabled' => false, // disable JSON Middleware
'json' => [
'text' => $markdown,
'mode' => 'gfm',
'context' => $context
]
];
return new RequestConfig(
WebClient::newRequest('POST', $this->getConfig()->getEndpoint(['markdown', 'POST'])),
$requestOptions,
function($body) : string {
$html = '';
if(!$body->error){
$html = (string)$body;
}
return $html;
}
);
}
/**
* @return ConfigInterface
*/
protected function getConfig() : ConfigInterface {
return ($this->config instanceof ConfigInterface) ? $this->config : $this->config = new Config();
}
}