1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Lichtenwallner (https://lichtenwallner.at) |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/jolicht/markdown-cms for the canonical source repository |
6
|
|
|
* @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT |
7
|
|
|
* @copyright Copyright (c) Johannes Lichtenwallner |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types = 1); |
10
|
|
|
namespace Jolicht\MarkdownCms\Markdown\Renderer; |
11
|
|
|
|
12
|
|
|
use League\CommonMark\HtmlElement; |
13
|
|
|
|
14
|
|
|
class ImageCaptionRenderer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Render figcaption |
18
|
|
|
* |
19
|
|
|
* @param array $config |
20
|
|
|
* @return HtmlElement |
21
|
|
|
*/ |
22
|
|
|
public function __invoke(array $config) : HtmlElement |
23
|
|
|
{ |
24
|
|
|
if (isset($config['attribution'])) { |
25
|
|
|
$attribution = new HtmlElement( |
26
|
|
|
'span', |
27
|
|
|
['class' => 'attribution'], |
28
|
|
|
$this->getAttributionContents($config['attribution'])); |
29
|
|
|
$contents = [ |
30
|
|
|
$config['caption'], |
31
|
|
|
$attribution |
32
|
|
|
]; |
33
|
|
|
} else { |
34
|
|
|
$contents = $config['caption']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
$figcaption = new HtmlElement('figcaption', [], $contents); |
39
|
|
|
|
40
|
|
|
return $figcaption; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function getAttributionContents(array $params) |
44
|
|
|
{ |
45
|
|
|
$contents = []; |
46
|
|
|
if (isset($params['title'])) { |
47
|
|
|
$contents[] = $this->getTitle($params); |
48
|
|
|
} |
49
|
|
|
if (isset($params['author'])) { |
50
|
|
|
$contents[] = $this->getAuthor($params); |
51
|
|
|
} |
52
|
|
|
if (isset($params['license'])) { |
53
|
|
|
$contents[] = $this->getLicense($params); |
54
|
|
|
} |
55
|
|
|
return $contents; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
private function getTitle(array $params) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (isset($params['url'])) { |
61
|
|
|
return new HtmlElement('a', ['class' => 'source', 'href' => $params['url']], $params['title']); |
62
|
|
|
} else { |
63
|
|
|
return new HtmlElement('span', ['class' => 'source'], $params['title']); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
private function getAuthor(array $params) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (isset($params['author_url'])) { |
70
|
|
|
return new HtmlElement('a', ['class' => 'author', 'href' => $params['author_url']], $params['author']); |
71
|
|
|
} else { |
72
|
|
|
return new HtmlElement('span', ['class' => 'author'], $params['author']); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
private function getLicense(array $params) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (isset($params['license_url'])) { |
79
|
|
|
return new HtmlElement('a', ['class' => 'license', 'href' => $params['license_url']], $params['license']); |
80
|
|
|
} else { |
81
|
|
|
return new HtmlElement('span', ['class' => 'license'], $params['license']); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
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.