|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Shortcode\Serializer; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Shortcode\Parser\RegexParser; |
|
5
|
|
|
use Thunder\Shortcode\Shortcode\Shortcode; |
|
6
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
|
7
|
|
|
use Thunder\Shortcode\Syntax\Syntax; |
|
8
|
|
|
use Thunder\Shortcode\Syntax\SyntaxInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
final class TextSerializer implements SerializerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var SyntaxInterface */ |
|
16
|
|
|
private $syntax; |
|
17
|
|
|
|
|
18
|
28 |
|
public function __construct(SyntaxInterface $syntax = null) |
|
19
|
|
|
{ |
|
20
|
28 |
|
$this->syntax = $syntax ?: new Syntax(); |
|
21
|
28 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** @inheritDoc */ |
|
24
|
13 |
|
public function serialize(ShortcodeInterface $shortcode) |
|
25
|
|
|
{ |
|
26
|
13 |
|
$open = $this->syntax->getOpeningTag(); |
|
27
|
13 |
|
$close = $this->syntax->getClosingTag(); |
|
28
|
13 |
|
$marker = $this->syntax->getClosingTagMarker(); |
|
29
|
|
|
|
|
30
|
13 |
|
$parameters = $this->serializeParameters($shortcode->getParameters()); |
|
31
|
13 |
|
$bbCode = null !== $shortcode->getBbCode() |
|
32
|
13 |
|
? $this->serializeValue($shortcode->getBbCode()) |
|
33
|
13 |
|
: ''; |
|
34
|
13 |
|
$return = $open.$shortcode->getName().$bbCode.$parameters; |
|
35
|
|
|
|
|
36
|
13 |
|
return null === $shortcode->getContent() |
|
37
|
13 |
|
? $return.' '.$marker.$close |
|
38
|
13 |
|
: $return.$close.(string)$shortcode->getContent().$open.$marker.$shortcode->getName().$close; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @psalm-param array<string,string|null> $parameters |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
13 |
|
private function serializeParameters(array $parameters) |
|
47
|
|
|
{ |
|
48
|
|
|
// unfortunately array_reduce() does not support keys |
|
49
|
13 |
|
$return = ''; |
|
50
|
13 |
|
foreach ($parameters as $key => $value) { |
|
51
|
9 |
|
$return .= ' '.$key.$this->serializeValue($value); |
|
52
|
13 |
|
} |
|
53
|
|
|
|
|
54
|
13 |
|
return $return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string|null $value |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
10 |
|
private function serializeValue($value) |
|
63
|
|
|
{ |
|
64
|
10 |
|
if (null === $value) { |
|
65
|
1 |
|
return ''; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
9 |
|
$delimiter = $this->syntax->getParameterValueDelimiter(); |
|
69
|
9 |
|
$separator = $this->syntax->getParameterValueSeparator(); |
|
70
|
|
|
|
|
71
|
9 |
|
return $separator.(preg_match('/^\w+$/u', $value) |
|
72
|
9 |
|
? $value |
|
73
|
9 |
|
: $delimiter.$value.$delimiter); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
10 |
|
public function unserialize($text) |
|
77
|
|
|
{ |
|
78
|
10 |
|
$parser = new RegexParser(); |
|
79
|
|
|
|
|
80
|
10 |
|
$shortcodes = $parser->parse($text); |
|
81
|
|
|
|
|
82
|
10 |
|
if (empty($shortcodes)) { |
|
83
|
1 |
|
$msg = 'Failed to unserialize shortcode from text %s!'; |
|
84
|
1 |
|
throw new \InvalidArgumentException(sprintf($msg, $text)); |
|
85
|
|
|
} |
|
86
|
9 |
|
if (count($shortcodes) > 1) { |
|
87
|
1 |
|
$msg = 'Provided text %s contains more than one shortcode!'; |
|
88
|
1 |
|
throw new \InvalidArgumentException(sprintf($msg, $text)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** @var ShortcodeInterface $parsed */ |
|
92
|
8 |
|
$parsed = array_shift($shortcodes); |
|
93
|
|
|
|
|
94
|
8 |
|
$name = $parsed->getName(); |
|
95
|
8 |
|
$parameters = $parsed->getParameters(); |
|
96
|
8 |
|
$content = $parsed->getContent(); |
|
97
|
8 |
|
$bbCode = $parsed->getBbCode(); |
|
98
|
|
|
|
|
99
|
8 |
|
return new Shortcode($name, $parameters, $content, $bbCode); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|