zend-blog-3-backend

Форк
0
/
TextProcessor.php 
152 строки · 3.7 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 10.04.16
6
 * Time: 17:29
7
 */
8

9
namespace App\Service;
10

11
use App\Entity\Post;
12
use App\Repository\MediaFileRepository;
13
use App\Repository\PygmentsCodeRepository;
14

15
class TextProcessor
16
{
17
    /**
18
     * @var MediaFileRepository
19
     */
20
    private $mediaFileRepository;
21

22
    /**
23
     * @var PygmentsCodeRepository
24
     */
25
    private $codeRepository;
26

27
    private ImageManager $im;
28

29
    private PictureTagBuilder $ptb;
30

31
    /**
32
     * @param MediaFileRepository $mediaFileRepository
33
     * @param PygmentsCodeRepository $codeRepository
34
     * @param PictureTagBuilder $ptb
35
     * @param ImageManager $im
36
     */
37
    public function __construct(
38
        MediaFileRepository $mediaFileRepository,
39
        PygmentsCodeRepository $codeRepository,
40
        PictureTagBuilder $ptb,
41
        ImageManager $im
42
    ) {
43
        $this->mediaFileRepository = $mediaFileRepository;
44
        $this->codeRepository = $codeRepository;
45
        $this->ptb = $ptb;
46
        $this->im = $im;
47
    }
48

49
    /**
50
     * @param Post $entity
51
     */
52
    public 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
     */
65
    private function imageProcessing(string $text): string
66
    {
67
        return preg_replace_callback(
68
            '/!(?<id>\d+)(?:\((?<alt>[^\)]+)\))?!/m',
69
            [$this, 'replaceImagesForArticle'],
70
            $text
71
        );
72
    }
73

74
    /**
75
     * @param string $text
76
     *
77
     * @return string
78
     */
79
    private function preview(string $text): string
80
    {
81
        $preview = explode('<!-- cut -->', $text);
82

83
        return 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
     */
95
    private function codeSnippetProcessing(string $text): string
96
    {
97
        $func = function (array $matches) {
98
            $code = $this->codeRepository->find((int)$matches['id']);
99
            if ($code) {
100
                $replace = $code->getSourceHtml();
101
            } else {
102
                $replace = '<b>UNDEFINED CODE SNIPPET</b>';
103
            }
104

105
            return $replace;
106
        };
107

108
        return preg_replace_callback(
109
            '/!<code>(?<id>\d+)!/m',
110
            $func,
111
            $text
112
        );
113
    }
114

115
    public function replaceImagesForArticle(array $matches)
116
    {
117
        $media = $this->mediaFileRepository->find((int)$matches['id']);
118
        if ($media) {
119
            $alt = $matches['alt'] ?? $media->getDescription();
120
            $replace = $this->ptb->articlePictureTag($media, $alt);
121

122
            if ($media->getWidth() > 864) {
123
                $replace = sprintf(
124
                    '<a class="anima-image-popup" href="%s">%s</a>',
125
                    $this->im->cdnImagePath() . $media->getPath(),
126
                    $replace
127
                );
128
            }
129
        } else {
130
            $replace = '<b>UNDEFINED</b>';
131
        }
132

133
        return $replace;
134
    }
135

136
    public function replaceImagesForPreview(array $matches)
137
    {
138
        $media = $this->mediaFileRepository->find((int)$matches['id']);
139
        if ($media) {
140
            if (!$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

150
        return $replace;
151
    }
152
}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.