1 | <?php |
||
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 | 27 | public function __construct(array $config = null) |
|
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 | 25 | public function parse($rawData) |
|
44 | { |
||
45 | 25 | $rawData = trim($rawData); |
|
46 | 25 | $fences = $this->config['fences']; |
|
47 | |||
48 | 25 | $start = $stop = null; |
|
49 | 25 | foreach ($fences as $fence) { |
|
50 | 25 | $start = $fence['open']; |
|
51 | 25 | $length = strlen($start); |
|
52 | 25 | if (substr($rawData, 0, $length) === $start) { |
|
53 | 24 | $stop = $fence['close']; |
|
54 | 25 | break; |
|
55 | } |
||
56 | } |
||
57 | |||
58 | 25 | if ($stop === null) { |
|
59 | 2 | return []; |
|
60 | } |
||
61 | |||
62 | 24 | $meta = trim(substr($rawData, strlen($start), strpos($rawData, $stop, strlen($start)) - (strlen($stop) + 1))); |
|
63 | 24 | if (strtolower($this->config['format']) === 'yaml') { |
|
64 | 1 | $meta = Yaml::parse($meta); |
|
65 | } else { |
||
66 | 23 | $meta = $this->parsePhileFormat($meta); |
|
|
|||
67 | } |
||
68 | 24 | $meta = ($meta === null) ? [] : $this->convertKeys($meta); |
|
69 | 24 | 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 | 23 | protected function convertKeys(array $meta) |
|
86 | { |
||
87 | 23 | $return = []; |
|
88 | 23 | foreach ($meta as $key => $value) { |
|
89 | 23 | if (is_array($value)) { |
|
90 | 1 | $value = $this->convertKeys($value); |
|
91 | } |
||
92 | 23 | $newKey = strtolower($key); |
|
93 | 23 | $newKey = preg_replace('/[^\w+]/', '_', $newKey); |
|
94 | 23 | $return[$newKey] = $value; |
|
95 | } |
||
96 | 23 | 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 | 23 | protected function parsePhileFormat($string) |
|
124 | } |
||
125 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.