|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of the Joomla Framework Github Package |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
|
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
|
12
|
|
|
use Joomla\Http\Exception\UnexpectedResponseException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* GitHub API Markdown class. |
|
16
|
|
|
* |
|
17
|
|
|
* @documentation http://developer.github.com/v3/markdown |
|
18
|
|
|
* |
|
19
|
|
|
* @since 1.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class Markdown extends AbstractPackage |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Render an arbitrary Markdown document. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $text The text object being parsed. |
|
27
|
|
|
* @param string $mode The parsing mode; valid options are 'markdown' or 'gfm'. |
|
28
|
|
|
* @param string $context An optional repository context, only used in 'gfm' mode. |
|
29
|
|
|
* |
|
30
|
|
|
* @return string Formatted HTML |
|
31
|
|
|
* |
|
32
|
|
|
* @since 1.0 |
|
33
|
|
|
* @throws UnexpectedResponseException |
|
34
|
|
|
* @throws \InvalidArgumentException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function render($text, $mode = 'gfm', $context = null) |
|
37
|
|
|
{ |
|
38
|
|
|
// The valid modes |
|
39
|
|
|
$validModes = array('gfm', 'markdown'); |
|
40
|
|
|
|
|
41
|
|
|
// Make sure the scope is valid |
|
42
|
|
|
if (!in_array($mode, $validModes)) |
|
43
|
|
|
{ |
|
44
|
|
|
throw new \InvalidArgumentException(sprintf('The %s mode is not valid. Valid modes are "gfm" or "markdown".', $mode)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Build the request path. |
|
48
|
|
|
$path = '/markdown'; |
|
49
|
|
|
|
|
50
|
|
|
// Build the request data. |
|
51
|
|
|
$data = str_replace('\\/', '/', json_encode( |
|
52
|
|
|
array( |
|
53
|
|
|
'text' => $text, |
|
54
|
|
|
'mode' => $mode, |
|
55
|
|
|
'context' => $context |
|
56
|
|
|
) |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
// Send the request. |
|
61
|
|
|
$response = $this->client->post($this->fetchUrl($path), $data); |
|
62
|
|
|
|
|
63
|
|
|
// Validate the response code. |
|
64
|
|
View Code Duplication |
if ($response->code != 200) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
// Decode the error response and throw an exception. |
|
67
|
|
|
$error = json_decode($response->body); |
|
68
|
|
|
$message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; |
|
69
|
|
|
throw new UnexpectedResponseException($response, $message, $response->code); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $response->body; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.