1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/addendum |
7
|
|
|
* @licence AGPL, Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
11
|
|
|
* @link http://maslosoft.com/addendum/ - maslosoft addendum |
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum\Matcher; |
16
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Exceptions\ParseException; |
18
|
|
|
use Maslosoft\Addendum\Interfaces\Matcher\MatcherInterface; |
19
|
|
|
|
20
|
|
|
class AnnotationsMatcher implements MatcherInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
use Traits\PluginsTrait; |
24
|
|
|
|
25
|
32 |
|
public function matches($string, &$annotations) |
26
|
|
|
{ |
27
|
32 |
|
$annotations = []; |
28
|
32 |
|
$annotationMatcher = new AnnotationMatcher; |
29
|
32 |
|
$annotationMatcher->setPlugins($this->getPlugins()); |
30
|
32 |
|
while (true) |
31
|
|
|
{ |
32
|
32 |
|
if (preg_match('~\s(?=@)~', $string, $matches, PREG_OFFSET_CAPTURE)) |
33
|
32 |
|
{ |
34
|
32 |
|
$offset = $matches[0][1] + 1; |
35
|
32 |
|
$string = substr($string, $offset); |
36
|
32 |
|
} |
37
|
|
|
else |
38
|
|
|
{ |
39
|
32 |
|
return; // no more annotations |
40
|
|
|
} |
41
|
|
|
|
42
|
32 |
|
if (($length = $annotationMatcher->matches($string, $data)) !== false) |
43
|
32 |
|
{ |
44
|
31 |
|
$srcString = $string; |
45
|
31 |
|
$string = substr($string, $length); |
46
|
31 |
|
list($name, $params) = $data; |
47
|
31 |
|
$annotations[$name][] = $params; |
48
|
|
|
|
49
|
|
|
// If have some params, match should be fine |
50
|
31 |
|
if (!empty($params)) |
51
|
31 |
|
{ |
52
|
21 |
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Check if we should have some match |
56
|
18 |
|
$stringParams = trim(preg_split("~[\r\n]~", $srcString)[0], '() '); |
57
|
|
|
|
58
|
18 |
|
if (strlen($stringParams) > $length) |
59
|
18 |
|
{ |
60
|
|
|
$msgParams = [ |
61
|
1 |
|
$name, |
62
|
|
|
$stringParams |
63
|
1 |
|
]; |
64
|
1 |
|
$msg = vsprintf('Could not parse params `%s` annotation near `%s`', $msgParams); |
65
|
1 |
|
throw new ParseException($msg); |
66
|
|
|
} |
67
|
17 |
|
} |
68
|
32 |
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|