Completed
Push — master ( 074cf1...332d1b )
by Dev
12:33
created

PageExtendedMainContentTrait::getSubtitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
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 getOtherProperties()
87
    {
88
        return $this->otherProperties;
89
    }
90
91
    public function setOtherProperties($otherProperties)
92
    {
93
        $this->otherProperties = $otherProperties;
94
        $this->otherPropertiesParsed = null;
95
96
        return $otherProperties;
97
    }
98
99
    /**
100
     * @Assert\Callback
101
     */
102
    public function validateOtherProperties(ExecutionContextInterface $context)
103
    {
104
        if (!empty($this->otherProperties)) {
105
            // ou utiliser yaml_parse
106
            try {
107
                Yaml::parse($this->otherProperties);
108
            } catch (ParseException $exception) {
109
                $context->buildViolation('page.otherProperties.malformed') //'$exception->getMessage())
110
                    ->atPath('otherProperties')
111
                    ->addViolation();
112
            }
113
        }
114
    }
115
116
    protected function getOtherPropertiesParsed($name)
117
    {
118
        if (null === $this->otherPropertiesParsed) {
119
            $this->otherPropertiesParsed = $this->otherProperties ? Yaml::parse($this->otherProperties) : [];
120
        }
121
122
        return $this->otherPropertiesParsed[$name] ?? null;
123
        /*
124
        if (!isset($this->otherPropertiesParsed[$name])) {
125
            throw new \Exception('"'.$name.'" is not defined.');
126
        }
127
128
        return $this->otherPropertiesParsed[$name];
129
        /**/
130
    }
131
132
    /**
133
     * Magic getter for otherProperties.
134
     *
135
     * @param string $method
136
     * @param array  $arguments
137
     *
138
     * @return mixed
139
     */
140
    public function __call($method, $arguments)
141
    {
142
        if ('_action' == $method) {
143
            return; // avoid error with sonata
144
        }
145
        if (preg_match('/^get/', $method)) {
146
            $property = lcfirst(preg_replace('/^get/', '', $method));
147
            if (!property_exists(get_class($this), $property)) {
148
                return $this->getOtherPropertiesParsed($property) ?? $this->getEmc($name);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
149
                // todo remove the else next release
150
            }
151
152
            return $this->$property;
153
        } else {
154
            $vars = array_keys(get_object_vars($this));
155
            if (in_array($method, $vars)) {
156
                return call_user_func_array([$this, 'get'.ucfirst($method)], $arguments);
157
            }
158
159
            return $this->getOtherPropertiesParsed(lcfirst($method));
160
        }
161
    }
162
163
    // To remove next release
164
    public function getEmc($name)
165
    {
166
        if (preg_match('/<!--"'.$name.'"--(.*)--\/-->/sU', $this->getMainContent(), $match)) {
167
            return $match[1];
168
        }
169
    }
170
}
171