1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
<?php
namespace Wcms;
use Exception;
class Element
{
protected $fullmatch;
protected $type;
protected $options;
protected $sources = [];
protected $params = [];
protected $autolink = 0;
protected $markdown = 1;
protected $content = '';
const OPTIONS = ['autolink', 'markdown'];
// __________________________________________________ F U N ____________________________________________________________
public function __construct($datas = [], $pageid)
{
$this->hydrate($datas);
$this->analyse($pageid);
}
public function hydrate($datas)
{
foreach ($datas as $key => $value) {
$method = 'set' . $key;
if (method_exists($this, $method)) {
$this->$method($value);
}
}
}
private function analyse(string $pageid)
{
if(!empty($this->options)) {
// Replace "!" by the real page name
$this->options = str_replace('!', $pageid, $this->options);
preg_match('~(:([a-z0-9-_+!]+))?(\/([a-z0-9-,_+=]+))?~', $this->options, $matches);
if(isset($matches[2]) && !empty($matches[2])) {
$this->sources = explode('+', $matches[2]);
} else {
$this->sources[] = $pageid;
}
if(isset($matches[4])) {
$this->params = explode(',', $matches[4]);
}
$this->readoptions();
} else {
$this->sources[] = $pageid;
}
}
private function readoptions()
{
if(!empty($this->params)) {
foreach ($this->params as $param ) {
preg_match('~([a-z0-9-_]+)(=(-?[0-9]+))?~', $param, $optionmatch);
if(isset($optionmatch[1])) {
$key = $optionmatch[1];
}
if(isset($optionmatch[3])) {
$value = $optionmatch[3];
} else {
$read = '<h2>Rendering error :</h2><p>Paramaters must have a value like : <strong><code>/' . $key . '=__value__</code></strong> for parameter : <strong><code>' . $key . '</code></strong></p>';
//throw new Exception($read);
}
$method = 'set' . $key;
if (in_array($key, self::OPTIONS)) {
if (!$this->$method($value)) {
$read = '<h2>Rendering error :</h2><p>Invalid value input : <strong><code>' . $value . '</code></strong> for parameter : <strong><code>' . $key . '</code></strong></p>';
//throw new Exception($read);
}
} else {
$read = '<h2>Rendering error :</h2><p>Parameter name : <strong><code>' . $optionmatch[1] . '</code></strong> does not exist<p>';
//throw new Exception($read);
}
}
}
}
public function addtags()
{
$this->content = PHP_EOL . '<' . $this->type() . '>' . PHP_EOL . $this->content() . PHP_EOL . '</' . $this->type() . '>' . PHP_EOL;
}
// __________________________________________________ G E T ____________________________________________________________
public function fullmatch()
{
return $this->fullmatch;
}
public function type()
{
return $this->type;
}
public function options()
{
return $this->options;
}
public function params()
{
return $this->params;
}
public function sources()
{
return $this->sources;
}
public function autolink()
{
return $this->autolink;
}
public function markdown()
{
return $this->markdown;
}
public function content()
{
return $this->content;
}
// __________________________________________________ S E T ____________________________________________________________
public function setfullmatch(string $fullmatch)
{
$this->fullmatch = $fullmatch;
}
public function settype(string $type)
{
$type = strtolower($type);
if(in_array($type, Model::TEXT_ELEMENTS)) {
$this->type = $type;
}
}
public function setoptions(string $options)
{
if(!empty($options)) {
$this->options = $options;
}
}
public function setautolink(int $level)
{
if($level >= 0 && $level <= 16) {
$this->autolink = $level;
return true;
} else {
return false;
}
}
public function setmarkdown(int $level)
{
if($level >= 0 && $level <= 1) {
$this->markdown = $level;
return true;
} else {
return false;
}
}
public function setcontent(string $content)
{
$this->content = $content;
}
}
?>
|