zend-blog-3-backend
152 строки · 3.7 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 10.04.16
6* Time: 17:29
7*/
8
9namespace App\Service;10
11use App\Entity\Post;12use App\Repository\MediaFileRepository;13use App\Repository\PygmentsCodeRepository;14
15class TextProcessor16{
17/**18* @var MediaFileRepository
19*/
20private $mediaFileRepository;21
22/**23* @var PygmentsCodeRepository
24*/
25private $codeRepository;26
27private ImageManager $im;28
29private PictureTagBuilder $ptb;30
31/**32* @param MediaFileRepository $mediaFileRepository
33* @param PygmentsCodeRepository $codeRepository
34* @param PictureTagBuilder $ptb
35* @param ImageManager $im
36*/
37public function __construct(38MediaFileRepository $mediaFileRepository,39PygmentsCodeRepository $codeRepository,40PictureTagBuilder $ptb,41ImageManager $im42) {43$this->mediaFileRepository = $mediaFileRepository;44$this->codeRepository = $codeRepository;45$this->ptb = $ptb;46$this->im = $im;47}48
49/**50* @param Post $entity
51*/
52public function processing(Post $entity)53{54$text = $this->codeSnippetProcessing($entity->getRawText());55
56$entity->setText($this->imageProcessing($text));57$entity->setPreview($this->preview($text));58}59
60/**61* @param string $text
62*
63* @return string
64*/
65private function imageProcessing(string $text): string66{67return preg_replace_callback(68'/!(?<id>\d+)(?:\((?<alt>[^\)]+)\))?!/m',69[$this, 'replaceImagesForArticle'],70$text71);72}73
74/**75* @param string $text
76*
77* @return string
78*/
79private function preview(string $text): string80{81$preview = explode('<!-- cut -->', $text);82
83return preg_replace_callback(84'/(<p>)?!(?<id>\d+)(\((?<alt>[^)]+)\))?!(<\/p>)?/m',85[$this, 'replaceImagesForPreview'],86$preview[0]87);88}89
90/**91* @param string $text
92*
93* @return string
94*/
95private function codeSnippetProcessing(string $text): string96{97$func = function (array $matches) {98$code = $this->codeRepository->find((int)$matches['id']);99if ($code) {100$replace = $code->getSourceHtml();101} else {102$replace = '<b>UNDEFINED CODE SNIPPET</b>';103}104
105return $replace;106};107
108return preg_replace_callback(109'/!<code>(?<id>\d+)!/m',110$func,111$text112);113}114
115public function replaceImagesForArticle(array $matches)116{117$media = $this->mediaFileRepository->find((int)$matches['id']);118if ($media) {119$alt = $matches['alt'] ?? $media->getDescription();120$replace = $this->ptb->articlePictureTag($media, $alt);121
122if ($media->getWidth() > 864) {123$replace = sprintf(124'<a class="anima-image-popup" href="%s">%s</a>',125$this->im->cdnImagePath() . $media->getPath(),126$replace127);128}129} else {130$replace = '<b>UNDEFINED</b>';131}132
133return $replace;134}135
136public function replaceImagesForPreview(array $matches)137{138$media = $this->mediaFileRepository->find((int)$matches['id']);139if ($media) {140if (!$media->isDefaultImage()) {141$alt = $matches['alt'] ?? $media->getDescription();142$replace = $this->ptb->previewPictureTag($media, $alt);143} else {144$replace = '';145}146} else {147$replace = '<b>UNDEFINED</b>';148}149
150return $replace;151}152}
153