Completed
Push — master ( 54991b...612227 )
by Dev
13:09
created

MainContentManager::__construct()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 17
rs 9.6111
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use PiedWeb\CMSBundle\Entity\PageInterface;
6
use Twig\Environment as Twig;
7
8
class MainContentManager
9
{
10
    protected $page;
11
    protected $chapeau;
12
    protected $mainContent;
13
14
    public function __construct(Twig $twig, PageInterface $page)
15
    {
16
        $this->page = $page;
17
        $this->twig = $twig;
0 ignored issues
show
Bug Best Practice introduced by
The property twig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
19
        $parsedContent = explode('<!--break-->', (string) $this->page->getMainContent());
20
21
        $this->chapeau = isset($parsedContent[1]) ? $parsedContent[0] : null;
22
        $this->mainContent = $parsedContent[1] ?? $parsedContent[0];
23
24
        if ($this->page->mainContentIsMarkdown()) {
25
            if ($this->chapeau) {
26
                $this->chapeau = '{% filter markdown %}'.$this->chapeau.'{% endfilter %}';
27
            }
28
            if ($this->mainContent) {
29
                $this->mainContent = self::convertMarkdownImage($this->mainContent);
30
                $this->mainContent = '{% filter markdown %}'.$this->mainContent.'{% endfilter %}';
31
            }
32
        }
33
    }
34
35
    public static function removeHtmlComments(string $content)
36
    {
37
        return preg_replace('/<!--(.|\s)*?-->/', '', $content);
38
    }
39
40
    public static function punctuationBeautifer($text)
41
    {
42
        return str_replace(
43
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
44
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
45
            $text
46
        );
47
    }
48
49
    public static function convertMarkdownImage(string $body)
50
    {
51
        return preg_replace(
52
            '/(?:!\[(.*?)\]\((.*?)\))/',
53
            '{%'
54
            .PHP_EOL.'    include "@PiedWebCMS/component/_inline_image.html.twig" with {'
55
            .PHP_EOL.'        "image_wrapper_class" : "mimg",'
56
            .PHP_EOL.'        "image_src" : "$2",'
57
            .PHP_EOL.'        "image_alt" : "$1"'
58
            .PHP_EOL.'    } only'
59
            .PHP_EOL.'%}'.PHP_EOL,
60
            $body
61
        );
62
    }
63
64
    protected function render($string)
65
    {
66
        $string = $this->twig->createTemplate($string)->render(['page' => $this->page]); // convert to html
67
        $string = self::punctuationBeautifer($string); // small fixes on punctuation to avoid linebreak
68
        $string = self::removeHtmlComments($string);
69
70
        return $string;
71
    }
72
73
    public function getFull()
74
    {
75
        return $this->render($this->chapeau.chr(10).chr(10).$this->mainContent);
76
    }
77
78
    public function getChapeau()
79
    {
80
        return $this->render($this->chapeau);
81
    }
82
83
    public function getMainContent()
84
    {
85
        return $this->render($this->mainContent);
86
    }
87
88
    public function getContent()
89
    {
90
        return $this->render($this->mainContent);
91
    }
92
93
    public function getIntro()
94
    {
95
        // return text without chapeau before first <h
96
    }
97
98
    public function getContentWithoutIntro()
99
    {
100
    }
101
102
    public function getToc()
103
    {
104
    }
105
}
106