1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\TextFilter\Filter; |
4
|
|
|
|
5
|
|
|
use \Symfony\Component\Yaml\Yaml; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Filter to replace variable expressions like %var% with the respective value |
9
|
|
|
* defined in the frontmatter. |
10
|
|
|
*/ |
11
|
|
|
class Variable implements FilterInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array $frontmatter parsed as frontmatter, accumulated when parsing |
15
|
|
|
* the text. |
16
|
|
|
*/ |
17
|
|
|
protected $frontmatter; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Parse the text through the filter and do what the filter does, |
23
|
|
|
* return the resulting text and with some optional additional details, |
24
|
|
|
* all wrapped in an key-value array. |
25
|
|
|
* |
26
|
|
|
* @param string $text to parse. |
27
|
|
|
* @param array $frontmatter optional to use while parsing. |
28
|
|
|
* @param array $options custom options to use while parsing. |
29
|
|
|
* |
30
|
|
|
* @return array with the resulting text and frontmatter. |
31
|
|
|
*/ |
32
|
4 |
|
public function parse($text, array $frontmatter, array $options = []) |
33
|
|
|
{ |
34
|
4 |
|
$this->frontmatter = $frontmatter; |
35
|
4 |
|
$text = $this->parseForVariable($text); |
36
|
|
|
|
37
|
|
|
return [ |
38
|
4 |
|
"text" => $text, |
39
|
4 |
|
"frontmatter" => $this->frontmatter, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Detect variable expressions %var% and replace with their values, if the |
47
|
|
|
* variable is defined. |
48
|
|
|
* |
49
|
|
|
* @param string $text to parse for variable expressions.. |
50
|
|
|
* |
51
|
|
|
* @return string as parsed text where variable are replaced with values. |
52
|
|
|
*/ |
53
|
4 |
|
protected function parseForVariable($text) |
54
|
|
|
{ |
55
|
4 |
|
$text = preg_replace_callback( |
56
|
4 |
|
"/(%[a-zA-Z_$][\w$]*%)/", |
57
|
4 |
|
[$this, "getVariableValue"], |
58
|
4 |
|
$text |
59
|
|
|
); |
60
|
|
|
|
61
|
4 |
|
return $text; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Detect and extract inline variables. |
68
|
|
|
* |
69
|
|
|
* @param string $text to parse. |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return boolean|void true when block is found and parsed, else void. |
72
|
|
|
* |
73
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
74
|
|
|
*/ |
75
|
4 |
|
protected function getVariableValue(array $matches) |
76
|
|
|
{ |
77
|
4 |
|
$res = $matches[0]; |
78
|
4 |
|
$variable = substr($matches[0], 1, -1); |
79
|
|
|
|
80
|
4 |
|
if (isset($this->frontmatter[$variable])) { |
81
|
3 |
|
$res = $this->frontmatter[$variable]; |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return $res; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.