1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class BBCodeOutputAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource BBCodeOutputAbstract.php |
6
|
|
|
* @created 23.04.2018 |
7
|
|
|
* @package chillerlan\BBCode\Output |
8
|
|
|
* @author smiley <[email protected]> |
9
|
|
|
* @copyright 2018 smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\BBCode\Output; |
14
|
|
|
|
15
|
|
|
use chillerlan\Traits\{ |
16
|
|
|
ClassLoader, ContainerInterface |
17
|
|
|
}; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Psr\SimpleCache\CacheInterface; |
20
|
|
|
|
21
|
|
|
abstract class BBCodeOutputAbstract implements BBCodeOutputInterface{ |
22
|
|
|
use ClassLoader; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
protected $modules = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string[] |
31
|
|
|
*/ |
32
|
|
|
protected $tagmap = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Holds an array of singletags |
36
|
|
|
* |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
protected $singletags = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Holds an array of noparse tags |
43
|
|
|
* |
44
|
|
|
* @var string[] |
45
|
|
|
*/ |
46
|
|
|
protected $noparse = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \chillerlan\BBCode\BBCodeOptions |
50
|
|
|
*/ |
51
|
|
|
protected $options; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Psr\SimpleCache\CacheInterface |
55
|
|
|
*/ |
56
|
|
|
protected $cache; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var \Psr\Log\LoggerInterface |
60
|
|
|
*/ |
61
|
|
|
protected $logger; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var \chillerlan\BBCode\Output\BBCodeModuleInterface[] |
65
|
|
|
*/ |
66
|
|
|
protected $moduleInterfaces = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $eol = PHP_EOL; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* BBCodeOutputInterface constructor. |
75
|
|
|
* |
76
|
|
|
* @param \chillerlan\Traits\ContainerInterface $options |
77
|
|
|
* @param \Psr\SimpleCache\CacheInterface $cache |
78
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
79
|
|
|
*/ |
80
|
|
|
public function __construct(ContainerInterface $options, CacheInterface $cache, LoggerInterface $logger){ |
81
|
|
|
$options->replacement_eol = $options->replacement_eol ?? $this->eol; |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->options = $options; |
|
|
|
|
84
|
|
|
$this->cache = $cache; |
85
|
|
|
$this->logger = $logger; |
86
|
|
|
|
87
|
|
|
foreach($this->modules as $module){ |
88
|
|
|
/** @var \chillerlan\BBCode\Output\BBCodeModuleInterface $moduleInterface */ |
89
|
|
|
$moduleInterface = $this->loadClass($module, BBCodeModuleInterface::class, $this->options, $this->cache, $this->logger); |
90
|
|
|
foreach($moduleInterface->getTags() as $tag){ |
91
|
|
|
$this->tagmap[$tag] = $module; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->noparse = array_merge($this->noparse, $moduleInterface->getNoparse()); |
|
|
|
|
95
|
|
|
$this->singletags = array_merge($this->singletags, $moduleInterface->getSingleTags()); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->moduleInterfaces[$module] = $moduleInterface; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
|
|
public function getTags():array { |
106
|
|
|
return array_keys($this->tagmap); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
|
|
public function getSingleTags():array { |
113
|
|
|
return $this->singletags; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
|
|
public function getNoparse():array{ |
120
|
|
|
return $this->noparse; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getEOL():string{ |
127
|
|
|
return $this->options->replacement_eol; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
|
|
public function transform(string $tag, array $attributes, string $content, string $match, int $callback_count):string{ |
134
|
|
|
|
135
|
|
|
if(!in_array($tag, array_keys($this->tagmap), true)){ |
136
|
|
|
return $match; // $content |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return call_user_func_array([$this->moduleInterfaces[$this->tagmap[$tag]], $tag], [$tag, $attributes, $content, $match, $callback_count]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: