1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Shortcode; |
3
|
|
|
|
4
|
|
|
use Thunder\Shortcode\EventContainer\EventContainer; |
5
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
6
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainerInterface; |
7
|
|
|
use Thunder\Shortcode\Parser\ParserInterface; |
8
|
|
|
use Thunder\Shortcode\Parser\RegularParser; |
9
|
|
|
use Thunder\Shortcode\Processor\Processor; |
10
|
|
|
use Thunder\Shortcode\Processor\ProcessorInterface; |
11
|
|
|
use Thunder\Shortcode\Serializer\JsonSerializer; |
12
|
|
|
use Thunder\Shortcode\Serializer\SerializerInterface; |
13
|
|
|
use Thunder\Shortcode\Serializer\TextSerializer; |
14
|
|
|
use Thunder\Shortcode\Serializer\XmlSerializer; |
15
|
|
|
use Thunder\Shortcode\Serializer\YamlSerializer; |
16
|
|
|
use Thunder\Shortcode\Shortcode\ParsedShortcodeInterface; |
17
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
18
|
|
|
use Thunder\Shortcode\Syntax\CommonSyntax; |
19
|
|
|
use Thunder\Shortcode\Syntax\SyntaxInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ShortcodeFacade |
25
|
|
|
{ |
26
|
|
|
/** @var ProcessorInterface */ |
27
|
|
|
private $processor; |
28
|
|
|
/** @var ParserInterface */ |
29
|
|
|
private $parser; |
30
|
|
|
/** @var SyntaxInterface */ |
31
|
|
|
private $syntax; |
32
|
|
|
|
33
|
|
|
/** @var HandlerContainer */ |
34
|
|
|
private $handlers; |
35
|
|
|
/** @var EventContainer */ |
36
|
|
|
private $events; |
37
|
|
|
|
38
|
|
|
/** @var SerializerInterface */ |
39
|
|
|
private $textSerializer; |
40
|
|
|
/** @var SerializerInterface */ |
41
|
|
|
private $jsonSerializer; |
42
|
|
|
/** @var SerializerInterface */ |
43
|
|
|
private $xmlSerializer; |
44
|
|
|
/** @var SerializerInterface */ |
45
|
|
|
private $yamlSerializer; |
46
|
|
|
|
47
|
5 |
|
public function __construct() |
48
|
|
|
{ |
49
|
5 |
|
$this->syntax = new CommonSyntax(); |
50
|
5 |
|
$this->handlers = new HandlerContainer(); |
51
|
5 |
|
$this->events = new EventContainer(); |
52
|
|
|
|
53
|
5 |
|
$this->parser = new RegularParser($this->syntax); |
54
|
5 |
|
$this->rebuildProcessor(); |
55
|
|
|
|
56
|
5 |
|
$this->textSerializer = new TextSerializer(); |
57
|
5 |
|
$this->jsonSerializer = new JsonSerializer(); |
58
|
5 |
|
$this->yamlSerializer = new YamlSerializer(); |
59
|
5 |
|
$this->xmlSerializer = new XmlSerializer(); |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @deprecated use constructor and customize using exposed methods |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
1 |
|
public static function create(HandlerContainerInterface $handlers, SyntaxInterface $syntax) |
67
|
|
|
{ |
68
|
1 |
|
$self = new self(); |
69
|
|
|
|
70
|
|
|
/** @psalm-suppress PropertyTypeCoercion */ |
71
|
1 |
|
$self->handlers = $handlers; |
|
|
|
|
72
|
1 |
|
$self->syntax = $syntax; |
73
|
1 |
|
$self->rebuildProcessor(); |
74
|
|
|
|
75
|
1 |
|
return $self; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @return void */ |
79
|
5 |
|
private function rebuildProcessor() |
80
|
|
|
{ |
81
|
5 |
|
$this->processor = new Processor($this->parser, $this->handlers); |
82
|
5 |
|
$this->processor = $this->processor->withEventContainer($this->events); |
83
|
5 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $text |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function process($text) |
91
|
|
|
{ |
92
|
2 |
|
return $this->processor->process($text); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $text |
97
|
|
|
* |
98
|
|
|
* @return ParsedShortcodeInterface[] |
99
|
|
|
*/ |
100
|
1 |
|
public function parse($text) |
101
|
|
|
{ |
102
|
1 |
|
return $this->parser->parse($text); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @return $this */ |
106
|
1 |
|
public function setParser(ParserInterface $parser) |
107
|
|
|
{ |
108
|
1 |
|
$this->parser = $parser; |
109
|
1 |
|
$this->rebuildProcessor(); |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $name |
116
|
|
|
* @psalm-param callable(ShortcodeInterface):string $handler |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
2 |
|
public function addHandler($name, $handler) |
121
|
|
|
{ |
122
|
2 |
|
$this->handlers->add($name, $handler); |
123
|
|
|
|
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $alias |
129
|
|
|
* @param string $name |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
1 |
|
public function addHandlerAlias($alias, $name) |
134
|
|
|
{ |
135
|
1 |
|
$this->handlers->addAlias($alias, $name); |
136
|
|
|
|
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @param callable $handler |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
1 |
|
public function addEventHandler($name, $handler) |
147
|
|
|
{ |
148
|
1 |
|
$this->events->addListener($name, $handler); |
149
|
|
|
|
150
|
1 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/* --- SERIALIZATION --------------------------------------------------- */ |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $format |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
2 |
View Code Duplication |
public function serialize(ShortcodeInterface $shortcode, $format) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
switch($format) { |
163
|
2 |
|
case 'text': return $this->textSerializer->serialize($shortcode); |
164
|
2 |
|
case 'json': return $this->jsonSerializer->serialize($shortcode); |
165
|
2 |
|
case 'yaml': return $this->yamlSerializer->serialize($shortcode); |
166
|
2 |
|
case 'xml': return $this->xmlSerializer->serialize($shortcode); |
167
|
1 |
|
default: throw new \InvalidArgumentException(sprintf('Invalid serialization format %s!', $format)); |
168
|
1 |
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $text |
173
|
|
|
* @param string $format |
174
|
|
|
* |
175
|
|
|
* @return ShortcodeInterface |
176
|
|
|
*/ |
177
|
2 |
View Code Duplication |
public function unserialize($text, $format) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
switch($format) { |
180
|
2 |
|
case 'text': return $this->textSerializer->unserialize($text); |
181
|
2 |
|
case 'json': return $this->jsonSerializer->unserialize($text); |
182
|
2 |
|
case 'yaml': return $this->yamlSerializer->unserialize($text); |
183
|
2 |
|
case 'xml': return $this->xmlSerializer->unserialize($text); |
184
|
1 |
|
default: throw new \InvalidArgumentException(sprintf('Invalid unserialization format %s!', $format)); |
185
|
1 |
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @deprecated use serialize($shortcode, $format) |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function serializeToText(ShortcodeInterface $s) { return $this->serialize($s, 'text'); } |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @deprecated use serialize($shortcode, $format) |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function serializeToJson(ShortcodeInterface $s) { return $this->serialize($s, 'json'); } |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @deprecated use serialize($shortcode, $format) |
202
|
|
|
* @param string $text |
203
|
|
|
* |
204
|
|
|
* @return ShortcodeInterface |
205
|
|
|
*/ |
206
|
|
|
public function unserializeFromText($text) { return $this->unserialize($text, 'text'); } |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @deprecated use serialize($shortcode, $format) |
210
|
|
|
* @param string $text |
211
|
|
|
* |
212
|
|
|
* @return ShortcodeInterface |
213
|
|
|
*/ |
214
|
|
|
public function unserializeFromJson($text) { return $this->unserialize($text, 'json'); } |
215
|
|
|
} |
216
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.