-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathRegexParser.php
More file actions
132 lines (115 loc) · 4.21 KB
/
RegexParser.php
File metadata and controls
132 lines (115 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Thunder\Shortcode\Parser;
use Thunder\Shortcode\Shortcode\ParsedShortcode;
use Thunder\Shortcode\Shortcode\Shortcode;
use Thunder\Shortcode\Syntax\Syntax;
use Thunder\Shortcode\Syntax\SyntaxInterface;
use Thunder\Shortcode\Utility\RegexBuilderUtility;
/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class RegexParser implements ParserInterface
{
/** @var SyntaxInterface */
private $syntax;
/** @var non-empty-string */
private $shortcodeRegex;
/** @var non-empty-string */
private $singleShortcodeRegex;
/** @var non-empty-string */
private $parametersRegex;
/** @param SyntaxInterface|null $syntax */
public function __construct($syntax = null)
{
if(null !== $syntax && false === $syntax instanceof SyntaxInterface) {
throw new \LogicException('Parameter $syntax must be an instance of SyntaxInterface.');
}
$this->syntax = $syntax ?: new Syntax();
$this->shortcodeRegex = RegexBuilderUtility::buildShortcodeRegex($this->syntax);
$this->singleShortcodeRegex = RegexBuilderUtility::buildSingleShortcodeRegex($this->syntax);
$this->parametersRegex = RegexBuilderUtility::buildParametersRegex($this->syntax);
}
/**
* @param string $text
*
* @return ParsedShortcode[]
*/
public function parse($text)
{
preg_match_all($this->shortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE);
// loop instead of array_map to pass the arguments explicitly
$shortcodes = array();
foreach($matches[0] as $match) {
/** @psalm-suppress PossiblyFalseArgument */
$offset = mb_strlen(substr($text, 0, $match[1]), 'utf-8');
$shortcodes[] = $this->parseSingle($match[0], $offset);
}
return array_filter($shortcodes);
}
/**
* @param string $text
* @param int $offset
*
* @return ParsedShortcode
*/
private function parseSingle($text, $offset)
{
preg_match($this->singleShortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE);
/** @psalm-var array<string,array{0:string,1:int}> $matches */
$name = $matches['name'][0];
$parameters = isset($matches['parameters'][0]) ? $this->parseParameters($matches['parameters'][0]) : array();
$bbCode = isset($matches['bbCode'][0]) && $matches['bbCode'][1] !== -1
? $this->extractValue($matches['bbCode'][0])
: null;
$content = isset($matches['content'][0]) && $matches['content'][1] !== -1 ? $matches['content'][0] : null;
return new ParsedShortcode(new Shortcode($name, $parameters, $content, $bbCode), $text, $offset);
}
/**
* @param string $text
*
* @psalm-return array<string,string|null>
*/
private function parseParameters($text)
{
preg_match_all($this->parametersRegex, $text, $argsMatches);
// loop because PHP 5.3 can't handle $this properly and I want separate methods
$return = array();
foreach ($argsMatches[1] as $item) {
/** @psalm-var array{0:string,1:string} $parts */
$parts = explode($this->syntax->getParameterValueSeparator(), $item, 2);
$return[trim($parts[0])] = $this->parseValue(isset($parts[1]) ? $parts[1] : null);
}
return $return;
}
/**
* @param string|null $value
*
* @return string|null
*/
private function parseValue($value)
{
return null === $value ? null : $this->extractValue(trim($value));
}
/**
* @param string $value
*
* @return string
* @psalm-suppress InvalidFalsableReturnType
*/
private function extractValue($value)
{
$length = strlen($this->syntax->getParameterValueDelimiter());
/** @psalm-suppress FalsableReturnStatement */
return $this->isDelimitedValue($value) ? substr($value, $length, -1 * $length) : $value;
}
/**
* @param string $value
*
* @return bool
*/
private function isDelimitedValue($value)
{
return preg_match('/^'.$this->syntax->getParameterValueDelimiter().'/us', $value)
&& preg_match('/'.$this->syntax->getParameterValueDelimiter().'$/us', $value);
}
}