Passed
Push — master ( 3303f4...09741c )
by Dev
13:58
created

PageExtendedMainContentTrait::getReadableContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Symfony\Component\Validator\Constraints as Assert;
6
use Symfony\Component\Validator\Context\ExecutionContextInterface;
7
use Symfony\Component\Yaml\Exception\ParseException;
8
use Symfony\Component\Yaml\Yaml;
9
10
trait PageExtendedMainContentTrait
11
{
12
    protected $chapeau;
13
14
    protected $readableContent;
15
16
    /**
17
     * @ORM\Column(type="boolean", nullable=true)
18
     */
19
    protected $mainContentIsMarkdown;
20
21
    /**
22
     * @ORM\Column(type="text", nullable=true)
23
     */
24
    protected $otherProperties;
25
26
    protected $otherPropertiesParsed;
27
28
    abstract public function getMainContent(): ?string;
29
30
    public static function removeHtmlComments(string $content)
31
    {
32
        return preg_replace('/<!--(.|\s)*?-->/', '', $content);
33
    }
34
35
    protected function manageMainContent()
36
    {
37
        $content = (string) $this->getMainContent();
38
        $content = explode('<!--break-->', $content);
39
40
        $this->chapeau = isset($content[1]) ? self::removeHtmlComments($content[0]) : null;
41
        $this->readableContent = \PiedWeb\CMSBundle\Twig\AppExtension::convertMarkdownImage(
42
            self::removeHtmlComments(isset($content[1]) ? $content[1] : $content[0])
43
        );
44
45
        if ($this->mainContentIsMarkdown) {
46
            if ($this->chapeau) {
47
                $this->chapeau = '{% filter markdown %}' . $this->chapeau . '{% endfilter %}';
48
            }
49
            if ($this->readableContent) {
50
                $this->readableContent = '{% filter markdown %}' . $this->readableContent . '{% endfilter %}';
51
            }
52
        }
53
    }
54
55
    public function getReadableContent()
56
    {
57
        $this->manageMainContent();
58
        /* Disable cache because it's generate an error with StaticBundle
59
        if (null === $this->readableContent) {
60
            $this->manageMainContent();
61
        }
62
        /**/
63
64
        return $this->readableContent;
65
    }
66
67
    public function getChapeau()
68
    {
69
        $this->manageMainContent();
70
71
        return $this->chapeau;
72
    }
73
74
    public function mainContentIsMarkdown(): bool
75
    {
76
        return null === $this->mainContentIsMarkdown ? false : $this->mainContentIsMarkdown;
77
    }
78
79
    public function setMainContentIsMarkdown(bool $mainContentIsMarkdown): self
80
    {
81
        $this->mainContentIsMarkdown = $mainContentIsMarkdown;
82
83
        return $this;
84
    }
85
86
    public function getTemplate(): ?string
87
    {
88
        return $this->getOtherProperty('template');
89
    }
90
91
    public function getOtherProperties()
92
    {
93
        return $this->otherProperties;
94
    }
95
96
    public function setOtherProperties($otherProperties)
97
    {
98
        $this->otherProperties = $otherProperties;
99
        $this->otherPropertiesParsed = null;
100
101
        return $otherProperties;
102
    }
103
104
    /**
105
     * @Assert\Callback
106
     */
107
    public function validateOtherProperties(ExecutionContextInterface $context)
108
    {
109
        if (!empty($this->otherProperties)) {
110
            // ou utiliser yaml_parse
111
            try {
112
                Yaml::parse($this->otherProperties);
113
            } catch (ParseException $exception) {
114
                $context->buildViolation('page.otherProperties.malformed') //'$exception->getMessage())
115
                    ->atPath('otherProperties')
116
                    ->addViolation();
117
            }
118
        }
119
    }
120
121
    protected function getOtherProperty($name)
122
    {
123
        if (null === $this->otherPropertiesParsed) {
124
            $this->otherPropertiesParsed = $this->otherProperties ? Yaml::parse($this->otherProperties) : [];
125
        }
126
127
        return $this->otherPropertiesParsed[$name] ?? null;
128
        /*
129
        if (!isset($this->otherPropertiesParsed[$name])) {
130
            throw new \Exception('"'.$name.'" is not defined.');
131
        }
132
133
        return $this->otherPropertiesParsed[$name];
134
        /**/
135
    }
136
137
    /**
138
     * Magic getter for otherProperties.
139
     *
140
     * @param string $method
141
     * @param array  $arguments
142
     *
143
     * @return mixed
144
     */
145
    public function __call($method, $arguments)
146
    {
147
        if ('_action' == $method) {
148
            return; // avoid error with sonata
149
        }
150
151
        if (preg_match('/^get/', $method)) {
152
            $property = lcfirst(preg_replace('/^get/', '', $method));
153
            if (!property_exists(get_class($this), $property)) {
154
                return $this->getOtherProperty($property) ?? $this->getEmc($property);
155
                // todo remove the else next release
156
            }
157
158
            return $this->$property;
159
        } else {
160
            $vars = array_keys(get_object_vars($this));
161
            if (in_array($method, $vars)) {
162
                return call_user_func_array([$this, 'get' . ucfirst($method)], $arguments);
163
            }
164
165
            return $this->getOtherProperty(lcfirst($method)) ?? $this->getEmc($method);
166
        }
167
    }
168
169
    // To remove next release
170
    public function getEmc($name)
171
    {
172
        if (preg_match('/<!--"' . $name . '"--(.*)--\/-->/sU', $this->getMainContent(), $match)) {
173
            return $match[1];
174
        }
175
    }
176
}
177