1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Meta Parser Interface |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Plugin\Phile\ParserMeta\Parser; |
6
|
|
|
|
7
|
|
|
use Phile\ServiceLocator\MetaInterface; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Meta |
12
|
|
|
* |
13
|
|
|
* @author PhileCMS |
14
|
|
|
* @link https://philecms.github.io |
15
|
|
|
* @license http://opensource.org/licenses/MIT |
16
|
|
|
* @package Phile\Plugin\Phile\ParserMeta\Parser |
17
|
|
|
*/ |
18
|
|
|
class Meta implements MetaInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array $config the configuration for this parser |
22
|
|
|
*/ |
23
|
|
|
private $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* the constructor |
27
|
|
|
* |
28
|
|
|
* @param array $config |
29
|
|
|
*/ |
30
|
35 |
|
public function __construct(array $config = null) |
31
|
|
|
{ |
32
|
35 |
|
if (!is_null($config)) { |
33
|
35 |
|
$this->config = $config; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Implements MetaInterface::parse |
39
|
|
|
* |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
28 |
|
public function extractMeta($rawData) |
43
|
|
|
{ |
44
|
28 |
|
list($meta) = $this->splitRawIntoMetaAndContent($rawData); |
45
|
|
|
|
46
|
28 |
|
if ($meta === null) { |
47
|
2 |
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
27 |
|
if (strtolower($this->config['format']) === 'yaml') { |
51
|
1 |
|
$meta = Yaml::parse($meta); |
52
|
|
|
} else { |
53
|
26 |
|
$meta = $this->parsePhileFormat($meta); |
|
|
|
|
54
|
|
|
} |
55
|
27 |
|
$meta = ($meta === null) ? [] : $this->convertKeys($meta); |
56
|
27 |
|
return $meta; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Implements MetaInterface::extractContent |
61
|
|
|
* |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
22 |
|
public function extractContent(?string $rawData): string |
65
|
|
|
{ |
66
|
22 |
|
list(, $content) = $this->splitRawIntoMetaAndContent($rawData); |
|
|
|
|
67
|
|
|
|
68
|
22 |
|
return $content; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Inspects the text and splits meta-data and content |
73
|
|
|
* |
74
|
|
|
* @param string $rawData Text to inspect |
75
|
|
|
* @return array array with [meta-data, content] |
76
|
|
|
*/ |
77
|
28 |
|
protected function splitRawIntoMetaAndContent(string $rawData): array |
78
|
|
|
{ |
79
|
28 |
|
$rawData = trim($rawData); |
80
|
28 |
|
$fences = $this->config['fences']; |
|
|
|
|
81
|
|
|
|
82
|
28 |
|
$meta = null; |
83
|
28 |
|
$content = $rawData; |
84
|
|
|
|
85
|
28 |
|
if ($rawData !== null) { |
86
|
28 |
|
$rawData = trim($rawData); |
87
|
28 |
|
$fences = $this->config['fences']; |
88
|
28 |
|
foreach ($fences as $fence) { |
89
|
28 |
|
if (strncmp($fence['open'], $rawData, strlen($fence['open'])) === 0) { |
90
|
27 |
|
$sub = substr($rawData, strlen($fence['open'])); |
91
|
27 |
|
list($meta, $content) = explode($fence['close'], $sub, 2); |
92
|
27 |
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
28 |
|
return [$meta, $content]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* convert meta data keys |
102
|
|
|
* |
103
|
|
|
* Creates "compatible" keys allowing easy access e.g. as template var. |
104
|
|
|
* |
105
|
|
|
* Conversions applied: |
106
|
|
|
* |
107
|
|
|
* - lowercase all chars |
108
|
|
|
* - replace special chars and whitespace with underscore |
109
|
|
|
* |
110
|
|
|
* @param array $meta meta-data |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
27 |
|
protected function convertKeys(array $meta) |
114
|
|
|
{ |
115
|
27 |
|
$return = []; |
116
|
27 |
|
foreach ($meta as $key => $value) { |
117
|
26 |
|
if (is_array($value)) { |
118
|
1 |
|
$value = $this->convertKeys($value); |
119
|
|
|
} |
120
|
26 |
|
$newKey = strtolower($key); |
121
|
26 |
|
$newKey = preg_replace('/[^\w+]/', '_', $newKey); |
122
|
26 |
|
$return[$newKey] = $value; |
123
|
|
|
} |
124
|
27 |
|
return $return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Phile meta format parser. |
129
|
|
|
* |
130
|
|
|
* @param string $string unparsed meta-data |
131
|
|
|
* @return array|null array with meta-tags; null: on meta-data found |
132
|
|
|
* |
133
|
|
|
* @deprecated since 1.6.0 Phile is going to switch to YAML |
134
|
|
|
*/ |
135
|
26 |
|
protected function parsePhileFormat($string) |
136
|
|
|
{ |
137
|
26 |
|
if (empty($string)) { |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
26 |
|
$meta = []; |
141
|
26 |
|
$lines = explode("\n", $string); |
142
|
26 |
|
foreach ($lines as $line) { |
143
|
26 |
|
$parts = explode(':', $line, 2); |
144
|
26 |
|
if (count($parts) !== 2) { |
145
|
26 |
|
continue; |
146
|
|
|
} |
147
|
25 |
|
$parts = array_map('trim', $parts); |
148
|
25 |
|
$meta[$parts[0]] = $parts[1]; |
149
|
|
|
} |
150
|
26 |
|
return $meta; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.