|
1
|
|
|
<?php |
|
2
|
|
|
namespace VKBansal\FrontMatter; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* FrontMatter Parser |
|
8
|
|
|
* @package VKBansal\FrontMatter\Parser |
|
9
|
|
|
* @version 1.3.0 |
|
10
|
|
|
* @author Vivek Kumar Bansal <[email protected]> |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
class Parser |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Regex for seperators |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $matcherRegex = "/^-{3}\s?(\w*)\r?\n(.*)\r?\n-{3}\r?\n(.*)/s"; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constanst for Document::dump() |
|
23
|
|
|
* @see Document::dump() |
|
24
|
|
|
*/ |
|
25
|
|
|
const DUMP_YAML = 'yaml'; |
|
26
|
|
|
const DUMP_JSON = 'json'; |
|
27
|
|
|
const DUMP_INI = 'ini'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Parse the given content |
|
31
|
|
|
* @param string $input content to be parsed |
|
32
|
|
|
* @return Document |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function parse($input) |
|
35
|
|
|
{ |
|
36
|
|
|
if (preg_match(self::$matcherRegex, $input, $matches)) { |
|
37
|
|
|
$header = self::parseHeader($matches[2], strtolower($matches[1])); |
|
38
|
|
|
$content = $matches[3]; |
|
39
|
|
|
} else { |
|
40
|
|
|
$content = $input; |
|
41
|
|
|
$header = []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return new Document($content, $header); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Convert a Document to string |
|
49
|
|
|
* @param Document $document |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function dump (Document $document, $mode = null) |
|
53
|
|
|
{ |
|
54
|
|
|
switch ($mode) { |
|
55
|
|
|
case 'yaml': |
|
56
|
|
|
return "--- yaml\n".Yaml::dump($document->getConfig())."---\n".$document->getContent(); |
|
57
|
|
|
|
|
58
|
|
|
case 'json': |
|
59
|
|
|
return "--- json\n".json_encode($document->getConfig(), JSON_PRETTY_PRINT)."\n---\n".$document->getContent(); |
|
60
|
|
|
|
|
61
|
|
|
case 'ini': |
|
62
|
|
|
return "--- ini\n".self::dumpIni($document->getConfig())."---\n".$document->getContent(); |
|
63
|
|
|
|
|
64
|
|
|
default: |
|
65
|
|
|
return "---\n".Yaml::dump($document->getConfig())."---\n".$document->getContent(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Determines if given content is valid |
|
71
|
|
|
* @param string $input Input to be validated |
|
72
|
|
|
* @return boolean True if valid else false |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function isValid($input) |
|
75
|
|
|
{ |
|
76
|
|
|
return preg_match(self::$matcherRegex, $input) === 1; |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Parses header |
|
82
|
|
|
* @param string $header |
|
83
|
|
|
* @param string $parser |
|
84
|
|
|
* @return array|string |
|
85
|
|
|
*/ |
|
86
|
|
|
private static function parseHeader($header, $parser) |
|
87
|
|
|
{ |
|
88
|
|
|
switch($parser){ |
|
89
|
|
|
case 'yaml': |
|
90
|
|
|
return Yaml::parse($header); |
|
|
|
|
|
|
91
|
|
|
case 'json': |
|
92
|
|
|
return json_decode($header, true); |
|
93
|
|
|
case 'ini': |
|
94
|
|
|
return parse_ini_string($header, true); |
|
95
|
|
|
default: |
|
96
|
|
|
return Yaml::parse($header); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Encodes PHP array to ini string |
|
102
|
|
|
* @param array $config |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
private static function dumpIni(array $config) |
|
106
|
|
|
{ |
|
107
|
|
|
$sections = $globals = ''; |
|
108
|
|
|
|
|
109
|
|
|
if (!empty($config)) { |
|
110
|
|
|
foreach ($config as $section => $item) { |
|
111
|
|
|
if (!is_array($item)) { |
|
112
|
|
|
//To write globals at top |
|
113
|
|
|
$globals .= "{$section} = {$item}\n"; |
|
114
|
|
|
} else { |
|
115
|
|
|
$sections = "\n[{$section}]\n"; |
|
116
|
|
|
foreach ($item as $key => $value) { |
|
117
|
|
|
if (is_array($value)) { |
|
118
|
|
|
foreach ($value as $arrKey => $arrValue) { |
|
119
|
|
|
$sections .= "{$key}[{$arrKey}] = $arrValue\n"; |
|
120
|
|
|
} |
|
121
|
|
|
} else { |
|
122
|
|
|
$sections .= "{$key} = {$value}\n"; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return $globals.$sections; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.