1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Shortcode\Parser; |
3
|
|
|
|
4
|
|
|
use Thunder\Shortcode\Shortcode\ParsedShortcode; |
5
|
|
|
use Thunder\Shortcode\Shortcode\Shortcode; |
6
|
|
|
use Thunder\Shortcode\Syntax\Syntax; |
7
|
|
|
use Thunder\Shortcode\Syntax\SyntaxInterface; |
8
|
|
|
use Thunder\Shortcode\Utility\RegexBuilderUtility; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class RegexParser implements ParserInterface |
14
|
|
|
{ |
15
|
|
|
/** @var SyntaxInterface */ |
16
|
|
|
private $syntax; |
17
|
|
|
private $shortcodeRegex; |
18
|
|
|
private $singleShortcodeRegex; |
19
|
|
|
private $parametersRegex; |
20
|
|
|
|
21
|
61 |
|
public function __construct(SyntaxInterface $syntax = null) |
22
|
|
|
{ |
23
|
61 |
|
$this->syntax = $syntax ?: new Syntax(); |
24
|
61 |
|
$this->shortcodeRegex = RegexBuilderUtility::buildShortcodeRegex($this->syntax); |
25
|
61 |
|
$this->singleShortcodeRegex = RegexBuilderUtility::buildSingleShortcodeRegex($this->syntax); |
26
|
61 |
|
$this->parametersRegex = RegexBuilderUtility::buildParametersRegex($this->syntax); |
27
|
61 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $text |
31
|
|
|
* |
32
|
|
|
* @return ParsedShortcode[] |
33
|
|
|
*/ |
34
|
102 |
|
public function parse($text) |
35
|
|
|
{ |
36
|
102 |
|
preg_match_all($this->shortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE); |
37
|
|
|
|
38
|
|
|
// loop instead of array_map to pass the arguments explicitly |
39
|
102 |
|
$shortcodes = array(); |
40
|
102 |
|
foreach($matches[0] as $match) { |
41
|
93 |
|
$offset = mb_strlen(substr($text, 0, $match[1]), 'utf-8'); |
42
|
93 |
|
$shortcodes[] = $this->parseSingle($match[0], $offset); |
43
|
102 |
|
} |
44
|
|
|
|
45
|
102 |
|
return array_filter($shortcodes); |
46
|
|
|
} |
47
|
|
|
|
48
|
93 |
|
private function parseSingle($text, $offset) |
49
|
|
|
{ |
50
|
93 |
|
preg_match($this->singleShortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE); |
51
|
|
|
|
52
|
93 |
|
$name = $matches['name'][0]; |
53
|
93 |
|
$parameters = isset($matches['parameters'][0]) ? $this->parseParameters($matches['parameters'][0]) : array(); |
54
|
93 |
|
$bbCode = isset($matches['bbCode'][0]) && $matches['bbCode'][1] !== -1 |
55
|
93 |
|
? $this->extractValue($matches['bbCode'][0]) |
56
|
93 |
|
: null; |
57
|
93 |
|
$content = isset($matches['content'][0]) && $matches['content'][1] !== -1 ? $matches['content'][0] : null; |
58
|
|
|
|
59
|
93 |
|
return new ParsedShortcode(new Shortcode($name, $parameters, $content, $bbCode), $text, $offset); |
60
|
|
|
} |
61
|
|
|
|
62
|
93 |
|
private function parseParameters($text) |
63
|
|
|
{ |
64
|
93 |
|
preg_match_all($this->parametersRegex, $text, $argsMatches); |
65
|
|
|
|
66
|
|
|
// loop because PHP 5.3 can't handle $this properly and I want separate methods |
67
|
93 |
|
$return = array(); |
68
|
93 |
|
foreach ($argsMatches[1] as $item) { |
69
|
35 |
|
$parts = explode($this->syntax->getParameterValueSeparator(), $item, 2); |
70
|
35 |
|
$return[trim($parts[0])] = $this->parseValue(isset($parts[1]) ? $parts[1] : null); |
71
|
93 |
|
} |
72
|
|
|
|
73
|
93 |
|
return $return; |
74
|
|
|
} |
75
|
|
|
|
76
|
35 |
|
private function parseValue($value) |
77
|
|
|
{ |
78
|
35 |
|
return null === $value ? null : $this->extractValue(trim($value)); |
79
|
|
|
} |
80
|
|
|
|
81
|
39 |
|
private function extractValue($value) |
82
|
|
|
{ |
83
|
39 |
|
$length = strlen($this->syntax->getParameterValueDelimiter()); |
84
|
|
|
|
85
|
39 |
|
return $this->isDelimitedValue($value) ? substr($value, $length, -1 * $length) : $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
39 |
|
private function isDelimitedValue($value) |
89
|
|
|
{ |
90
|
39 |
|
return preg_match('/^'.$this->syntax->getParameterValueDelimiter().'/us', $value) |
91
|
39 |
|
&& preg_match('/'.$this->syntax->getParameterValueDelimiter().'$/us', $value); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|