1 | <?php |
||
26 | class EmbeddedImageProcessor implements EmbeddedImageProcessorInterface |
||
27 | { |
||
28 | private const DEFAULT_ARTICLE_BODY_IMAGE_RENDITION = 'original'; |
||
29 | |||
30 | /** |
||
31 | * @var MediaManagerInterface |
||
32 | */ |
||
33 | private $mediaManager; |
||
34 | |||
35 | /** |
||
36 | * @var FileExtensionCheckerInterface |
||
37 | */ |
||
38 | private $fileExtensionChecker; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $defaultImageRendition; |
||
44 | |||
45 | public function __construct(MediaManagerInterface $mediaManager, FileExtensionCheckerInterface $fileExtensionChecker) |
||
46 | { |
||
47 | $this->mediaManager = $mediaManager; |
||
48 | $this->fileExtensionChecker = $fileExtensionChecker; |
||
49 | } |
||
50 | |||
51 | public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void |
||
52 | { |
||
53 | $body = $article->getBody(); |
||
54 | $mediaId = str_replace('/', '\\/', $articleMedia->getKey()); |
||
55 | |||
56 | preg_match( |
||
57 | "/(<!-- ?EMBED START Image {id: \"$mediaId\"} ?-->)(.+?)(<!-- ?EMBED END Image {id: \"$mediaId\"} ?-->)/im", |
||
58 | str_replace(PHP_EOL, '', $body), |
||
59 | $embeds |
||
60 | ); |
||
61 | |||
62 | if (empty($embeds)) { |
||
63 | return; |
||
64 | } |
||
65 | |||
66 | $figureString = $embeds[2]; |
||
67 | $crawler = new Crawler($figureString); |
||
68 | $images = $crawler->filter('figure img'); |
||
69 | |||
70 | /** @var \DOMElement $imageElement */ |
||
71 | foreach ($images as $imageElement) { |
||
72 | /** @var ImageRendition $rendition */ |
||
73 | foreach ($articleMedia->getRenditions() as $rendition) { |
||
74 | if ($this->getDefaultImageRendition() === $rendition->getName()) { |
||
75 | $this->processImageElement($imageElement, $rendition, $mediaId); |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | |||
80 | $figCaptionNode = $crawler->filter('figure figcaption')->getNode(0); |
||
81 | if (null !== $figCaptionNode) { |
||
82 | $this->appendImageByline($articleMedia, $figCaptionNode); |
||
83 | } |
||
84 | |||
85 | $article->setBody(str_replace($figureString, $crawler->filter('body')->html(), $body)); |
||
86 | } |
||
87 | |||
88 | public function setDefaultImageRendition(string $renditionName): void |
||
89 | { |
||
90 | $this->defaultImageRendition = $renditionName; |
||
91 | } |
||
92 | |||
93 | public function supports(string $type): bool |
||
97 | |||
98 | private function appendImageByline(ArticleMediaInterface $articleMedia, \DOMElement $figCaptionNode): void |
||
99 | { |
||
100 | $element = new \DOMElement('span'); |
||
106 | |||
107 | public function applyByline(ArticleMediaInterface $articleMedia): string |
||
111 | |||
112 | protected function processImageElement(\DOMElement $imageElement, ImageRendition $rendition, string $mediaId): void |
||
135 | |||
136 | protected function getDefaultImageRendition(): string |
||
144 | } |
||
145 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.