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\ElementRendererInterface; |
13
|
|
|
use League\CommonMark\HtmlElement; |
14
|
|
|
use League\CommonMark\Inline\Renderer\InlineRendererInterface; |
15
|
|
|
use League\CommonMark\Inline\Element\AbstractInline; |
16
|
|
|
|
17
|
|
|
class ImageRenderer implements InlineRendererInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Url Translation |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $urlTranslation; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* @param array $urlTranslation |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $urlTranslation) |
32
|
|
|
{ |
33
|
|
|
$this->urlTranslation = $urlTranslation; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get url translation |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function getUrlTranslation() : array |
42
|
|
|
{ |
43
|
|
|
return $this->urlTranslation; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Render image |
48
|
|
|
* |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
* @see \League\CommonMark\Inline\Renderer\InlineRendererInterface::render() |
51
|
|
|
*/ |
52
|
|
|
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) |
53
|
|
|
{ |
54
|
|
|
$altContent = $inline->firstChild()->getContent(); |
|
|
|
|
55
|
|
|
$attributes = [ |
56
|
|
|
'src' => strtr($inline->getUrl(), $this->getUrlTranslation()), |
|
|
|
|
57
|
|
|
'alt' => $altContent, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$posJsonOpen = strpos($altContent, '{'); |
61
|
|
|
$posJsonClose = strpos($altContent, '}'); |
62
|
|
|
if ((false !== $posJsonOpen) and (false !== $posJsonClose)) { |
|
|
|
|
63
|
|
|
$json = substr($altContent, $posJsonOpen, $posJsonClose); |
64
|
|
|
$data = json_decode($json, true); |
65
|
|
|
if (is_array($data)) { |
66
|
|
|
$attributes = array_merge($attributes, $data); |
67
|
|
|
} |
68
|
|
|
$attributes['alt'] = substr($altContent, 0, $posJsonOpen); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!isset($attributes['layout'])) { |
72
|
|
|
$attributes['layout'] = 'responsive'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return new HtmlElement('amp-img', $attributes); |
76
|
|
|
} |
77
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: