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\ShortcodeInterface; |
17
|
|
|
use Thunder\Shortcode\Syntax\CommonSyntax; |
18
|
|
|
use Thunder\Shortcode\Syntax\SyntaxInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ShortcodeFacade |
24
|
|
|
{ |
25
|
|
|
/** @var ProcessorInterface */ |
26
|
|
|
private $processor; |
27
|
|
|
/** @var ParserInterface */ |
28
|
|
|
private $parser; |
29
|
|
|
/** @var SyntaxInterface */ |
30
|
|
|
private $syntax; |
31
|
|
|
|
32
|
|
|
/** @var HandlerContainer */ |
33
|
|
|
private $handlers; |
34
|
|
|
/** @var EventContainer */ |
35
|
|
|
private $events; |
36
|
|
|
|
37
|
|
|
/** @var SerializerInterface */ |
38
|
|
|
private $textSerializer; |
39
|
|
|
/** @var SerializerInterface */ |
40
|
|
|
private $jsonSerializer; |
41
|
|
|
/** @var SerializerInterface */ |
42
|
|
|
private $xmlSerializer; |
43
|
|
|
/** @var SerializerInterface */ |
44
|
|
|
private $yamlSerializer; |
45
|
|
|
|
46
|
5 |
|
public function __construct() |
47
|
|
|
{ |
48
|
5 |
|
$this->syntax = new CommonSyntax(); |
49
|
5 |
|
$this->handlers = new HandlerContainer(); |
50
|
5 |
|
$this->events = new EventContainer(); |
51
|
|
|
|
52
|
5 |
|
$this->parser = new RegularParser($this->syntax); |
53
|
5 |
|
$this->rebuildProcessor(); |
54
|
|
|
|
55
|
5 |
|
$this->textSerializer = new TextSerializer(); |
56
|
5 |
|
$this->jsonSerializer = new JsonSerializer(); |
57
|
5 |
|
$this->yamlSerializer = new YamlSerializer(); |
58
|
5 |
|
$this->xmlSerializer = new XmlSerializer(); |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** @deprecated use constructor and customize using exposed methods */ |
62
|
1 |
|
public static function create(HandlerContainerInterface $handlers, SyntaxInterface $syntax) |
63
|
|
|
{ |
64
|
1 |
|
$self = new self(); |
65
|
|
|
|
66
|
1 |
|
$self->handlers = $handlers; |
|
|
|
|
67
|
1 |
|
$self->syntax = $syntax; |
68
|
1 |
|
$self->rebuildProcessor(); |
69
|
|
|
|
70
|
1 |
|
return $self; |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
private function rebuildProcessor() |
74
|
|
|
{ |
75
|
5 |
|
$this->processor = new Processor($this->parser, $this->handlers); |
76
|
5 |
|
$this->processor = $this->processor->withEventContainer($this->events); |
77
|
5 |
|
} |
78
|
|
|
|
79
|
2 |
|
public function process($text) |
80
|
|
|
{ |
81
|
2 |
|
return $this->processor->process($text); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function parse($text) |
85
|
|
|
{ |
86
|
1 |
|
return $this->parser->parse($text); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function setParser(ParserInterface $parser) |
90
|
|
|
{ |
91
|
1 |
|
$this->parser = $parser; |
92
|
1 |
|
$this->rebuildProcessor(); |
93
|
|
|
|
94
|
1 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function addHandler($name, $handler) |
98
|
|
|
{ |
99
|
2 |
|
$this->handlers->add($name, $handler); |
100
|
|
|
|
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function addHandlerAlias($alias, $name) |
105
|
|
|
{ |
106
|
1 |
|
$this->handlers->addAlias($alias, $name); |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function addEventHandler($name, $handler) |
112
|
|
|
{ |
113
|
1 |
|
$this->events->addListener($name, $handler); |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* --- SERIALIZATION --------------------------------------------------- */ |
119
|
|
|
|
120
|
2 |
View Code Duplication |
public function serialize(ShortcodeInterface $shortcode, $format) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
switch($format) { |
123
|
2 |
|
case 'text': return $this->textSerializer->serialize($shortcode); |
124
|
2 |
|
case 'json': return $this->jsonSerializer->serialize($shortcode); |
125
|
2 |
|
case 'yaml': return $this->yamlSerializer->serialize($shortcode); |
126
|
2 |
|
case 'xml': return $this->xmlSerializer->serialize($shortcode); |
127
|
1 |
|
default: throw new \InvalidArgumentException(sprintf('Invalid serialization format %s!', $format)); |
128
|
1 |
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
View Code Duplication |
public function unserialize($text, $format) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
switch($format) { |
134
|
2 |
|
case 'text': return $this->textSerializer->unserialize($text); |
135
|
2 |
|
case 'json': return $this->jsonSerializer->unserialize($text); |
136
|
2 |
|
case 'yaml': return $this->yamlSerializer->unserialize($text); |
137
|
2 |
|
case 'xml': return $this->xmlSerializer->unserialize($text); |
138
|
1 |
|
default: throw new \InvalidArgumentException(sprintf('Invalid unserialization format %s!', $format)); |
139
|
1 |
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @deprecated use serialize($shortcode, $format) */ |
143
|
|
|
public function serializeToText(ShortcodeInterface $s) { return $this->serialize($s, 'text'); } |
144
|
|
|
/** @deprecated use serialize($shortcode, $format) */ |
145
|
|
|
public function serializeToJson(ShortcodeInterface $s) { return $this->serialize($s, 'json'); } |
146
|
|
|
/** @deprecated use unserialize($shortcode, $format) */ |
147
|
|
|
public function unserializeFromText($text) { return $this->unserialize($text, 'text'); } |
148
|
|
|
/** @deprecated use unserialize($shortcode, $format) */ |
149
|
|
|
public function unserializeFromJson($text) { return $this->unserialize($text, 'json'); } |
150
|
|
|
} |
151
|
|
|
|
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.