1 | <?php |
||
13 | final class RegexParser implements ParserInterface |
||
14 | { |
||
15 | /** @var SyntaxInterface */ |
||
16 | private $syntax; |
||
17 | private $shortcodeRegex; |
||
18 | private $singleShortcodeRegex; |
||
19 | private $parametersRegex; |
||
20 | |||
21 | 60 | public function __construct(SyntaxInterface $syntax = null) |
|
28 | |||
29 | /** |
||
30 | * @param string $text |
||
31 | * |
||
32 | * @return ParsedShortcode[] |
||
33 | */ |
||
34 | 101 | public function parse($text) |
|
47 | |||
48 | 92 | private function parseSingle($text, $offset) |
|
49 | { |
||
50 | 92 | preg_match($this->singleShortcodeRegex, $text, $matches, PREG_OFFSET_CAPTURE); |
|
51 | |||
52 | 92 | $name = $matches['name'][0]; |
|
53 | 92 | if(!preg_match('~^'.RegexBuilderUtility::buildNameRegex().'$~us', $name)) { |
|
54 | return null; |
||
55 | } |
||
56 | 92 | $parameters = isset($matches['parameters'][0]) ? $this->parseParameters($matches['parameters'][0]) : array(); |
|
57 | 92 | $bbCode = isset($matches['bbCode'][0]) && $matches['bbCode'][1] !== -1 |
|
58 | 92 | ? $this->extractValue($matches['bbCode'][0]) |
|
59 | 92 | : null; |
|
60 | 92 | $content = isset($matches['content'][0]) && $matches['content'][1] !== -1 ? $matches['content'][0] : null; |
|
61 | |||
62 | 92 | return new ParsedShortcode(new Shortcode($name, $parameters, $content, $bbCode), $text, $offset); |
|
63 | } |
||
64 | |||
65 | 92 | private function parseParameters($text) |
|
66 | { |
||
67 | 92 | preg_match_all($this->parametersRegex, $text, $argsMatches); |
|
68 | |||
69 | // loop because PHP 5.3 can't handle $this properly and I want separate methods |
||
70 | 92 | $return = array(); |
|
71 | 92 | foreach ($argsMatches[1] as $item) { |
|
72 | 35 | $parts = explode($this->syntax->getParameterValueSeparator(), $item, 2); |
|
73 | 35 | $return[trim($parts[0])] = $this->parseValue(isset($parts[1]) ? $parts[1] : null); |
|
74 | 92 | } |
|
75 | |||
76 | 92 | return $return; |
|
77 | } |
||
78 | |||
79 | 35 | private function parseValue($value) |
|
83 | |||
84 | 39 | private function extractValue($value) |
|
90 | |||
91 | 39 | private function isDelimitedValue($value) |
|
96 | } |
||
97 |