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 | 34 | public function __construct(array $config = null) |
|
31 | { |
||
32 | 34 | if (!is_null($config)) { |
|
33 | 34 | $this->config = $config; |
|
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * parse the content and extract meta information |
||
39 | * |
||
40 | * @param string $rawData raw page data |
||
41 | * @return array with key/value store |
||
42 | */ |
||
43 | 27 | public function parse($rawData) |
|
44 | { |
||
45 | 27 | $rawData = trim($rawData); |
|
46 | 27 | $fences = $this->config['fences']; |
|
47 | |||
48 | 27 | $start = $stop = null; |
|
49 | 27 | foreach ($fences as $fence) { |
|
50 | 27 | $start = $fence['open']; |
|
51 | 27 | $length = strlen($start); |
|
52 | 27 | if (substr($rawData, 0, $length) === $start) { |
|
53 | 27 | $stop = $fence['close']; |
|
54 | 27 | break; |
|
55 | } |
||
56 | } |
||
57 | |||
58 | 27 | if ($stop === null) { |
|
59 | 1 | return []; |
|
60 | } |
||
61 | |||
62 | 27 | $meta = trim(substr($rawData, strlen($start), strpos($rawData, $stop, strlen($start)) - (strlen($stop) + 1))); |
|
63 | 27 | if (strtolower($this->config['format']) === 'yaml') { |
|
64 | 1 | $meta = Yaml::parse($meta); |
|
65 | } else { |
||
66 | 26 | $meta = $this->parsePhileFormat($meta); |
|
0 ignored issues
–
show
|
|||
67 | } |
||
68 | 27 | $meta = ($meta === null) ? [] : $this->convertKeys($meta); |
|
69 | 27 | return $meta; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * convert meta data keys |
||
74 | * |
||
75 | * Creates "compatible" keys allowing easy access e.g. as template var. |
||
76 | * |
||
77 | * Conversions applied: |
||
78 | * |
||
79 | * - lowercase all chars |
||
80 | * - replace special chars and whitespace with underscore |
||
81 | * |
||
82 | * @param array $meta meta-data |
||
83 | * @return array |
||
84 | */ |
||
85 | 26 | protected function convertKeys(array $meta) |
|
86 | { |
||
87 | 26 | $return = []; |
|
88 | 26 | foreach ($meta as $key => $value) { |
|
89 | 26 | if (is_array($value)) { |
|
90 | 1 | $value = $this->convertKeys($value); |
|
91 | } |
||
92 | 26 | $newKey = strtolower($key); |
|
93 | 26 | $newKey = preg_replace('/[^\w+]/', '_', $newKey); |
|
94 | 26 | $return[$newKey] = $value; |
|
95 | } |
||
96 | 26 | return $return; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Phile meta format parser. |
||
101 | * |
||
102 | * @param string $string unparsed meta-data |
||
103 | * @return array|null array with meta-tags; null: on meta-data found |
||
104 | * |
||
105 | * @deprecated since 1.6.0 Phile is going to switch to YAML |
||
106 | */ |
||
107 | 26 | protected function parsePhileFormat($string) |
|
108 | { |
||
109 | 26 | if (empty($string)) { |
|
110 | 1 | return null; |
|
111 | } |
||
112 | 25 | $meta = []; |
|
113 | 25 | $lines = explode("\n", $string); |
|
114 | 25 | foreach ($lines as $line) { |
|
115 | 25 | $parts = explode(':', $line, 2); |
|
116 | 25 | if (count($parts) !== 2) { |
|
117 | continue; |
||
118 | } |
||
119 | 25 | $parts = array_map('trim', $parts); |
|
120 | 25 | $meta[$parts[0]] = $parts[1]; |
|
121 | } |
||
122 | 25 | return $meta; |
|
123 | } |
||
124 | } |
||
125 |
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.