|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Shortcode\Event; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Shortcode\Shortcode\ReplacedShortcode; |
|
5
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This event is called just before returning processed text result at each |
|
9
|
|
|
* processing level to alter the way shortcodes are replaced with their handlers |
|
10
|
|
|
* results in the source text. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ReplaceShortcodesEvent |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var ShortcodeInterface|null */ |
|
17
|
|
|
private $shortcode; |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $text; |
|
20
|
|
|
/** @var ReplacedShortcode[] */ |
|
21
|
|
|
private $replacements = array(); |
|
22
|
|
|
/** @var string|null */ |
|
23
|
|
|
private $result; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $text |
|
27
|
|
|
* @param ReplacedShortcode[] $replacements |
|
28
|
|
|
*/ |
|
29
|
57 |
|
public function __construct($text, array $replacements, ShortcodeInterface $shortcode = null) |
|
30
|
|
|
{ |
|
31
|
57 |
|
$this->shortcode = $shortcode; |
|
32
|
57 |
|
$this->text = $text; |
|
33
|
|
|
|
|
34
|
57 |
|
$this->setReplacements($replacements); |
|
35
|
57 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ReplacedShortcode[] $replacements |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
57 |
|
private function setReplacements(array $replacements) |
|
43
|
|
|
{ |
|
44
|
57 |
|
foreach($replacements as $replacement) { |
|
45
|
57 |
|
$this->addReplacement($replacement); |
|
46
|
57 |
|
} |
|
47
|
57 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @return void */ |
|
50
|
57 |
|
private function addReplacement(ReplacedShortcode $replacement) |
|
51
|
|
|
{ |
|
52
|
57 |
|
$this->replacements[] = $replacement; |
|
53
|
57 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @return string */ |
|
56
|
1 |
|
public function getText() |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->text; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @return ReplacedShortcode[] */ |
|
62
|
2 |
|
public function getReplacements() |
|
63
|
|
|
{ |
|
64
|
2 |
|
return $this->replacements; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @return ShortcodeInterface|null */ |
|
68
|
1 |
|
public function getShortcode() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->shortcode; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $result |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function setResult($result) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->result = $result; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @return string|null */ |
|
84
|
2 |
|
public function getResult() |
|
85
|
|
|
{ |
|
86
|
2 |
|
return $this->result; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** @return bool */ |
|
90
|
57 |
|
public function hasResult() |
|
91
|
|
|
{ |
|
92
|
57 |
|
return null !== $this->result; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|