1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pageon\Lib\Markdown; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use League\CommonMark\ElementRendererInterface; |
7
|
|
|
use League\CommonMark\HtmlElement; |
8
|
|
|
use League\CommonMark\Inline\Element\AbstractInline; |
9
|
|
|
use League\CommonMark\Inline\Element\Image; |
10
|
|
|
use League\CommonMark\Inline\Element\Text; |
11
|
|
|
use League\CommonMark\Inline\Renderer\InlineRendererInterface; |
12
|
|
|
use Pageon\Html\Image\ImageFactory; |
13
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
14
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
15
|
|
|
|
16
|
|
|
class ImageRenderer implements InlineRendererInterface |
17
|
|
|
{ |
18
|
|
|
/** @var \Pageon\Html\Image\ImageFactory */ |
19
|
|
|
private $imageFactory; |
20
|
|
|
|
21
|
|
|
public function __construct(ImageFactory $imageFactory) |
22
|
|
|
{ |
23
|
|
|
$this->imageFactory = $imageFactory; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) |
27
|
|
|
{ |
28
|
|
|
if (! $inline instanceof Image) { |
29
|
|
|
throw new InvalidArgumentException('Inline must be instance of ' . Image::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$attributes = []; |
33
|
|
|
|
34
|
|
|
$src = $inline->getUrl(); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$responsiveImage = $this->imageFactory->create($src); |
38
|
|
|
} catch (FileNotFoundException $e) { |
39
|
|
|
throw InvalidConfiguration::fileNotFound($src); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$alt = $inline->firstChild(); |
43
|
|
|
|
44
|
|
|
$attributes['src'] = $src; |
45
|
|
|
$attributes['srcset'] = $responsiveImage->srcset() ?? null; |
46
|
|
|
$attributes['sizes'] = $responsiveImage->sizes() ?? null; |
47
|
|
|
$attributes['alt'] = $alt instanceof Text |
48
|
|
|
? $alt->getContent() |
49
|
|
|
: ''; |
50
|
|
|
|
51
|
|
|
return new HtmlElement( |
52
|
|
|
'img', |
53
|
|
|
$attributes, |
54
|
|
|
$htmlRenderer->renderInlines($inline->children()) |
|
|
|
|
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: