public function parseFileMeta($rawContent, array $headers)
{
$meta = array();
$pattern = "/^(\\/(\\*)|---)[[:blank:]]*(?:\r)?\n" . "(?:(.*?)(?:\r)?\n)?(?(2)\\*\\/|---)[[:blank:]]*(?:(?:\r)?\n|\$)/s";
if (preg_match($pattern, $rawContent, $rawMetaMatches) && isset($rawMetaMatches[3])) {
$yamlParser = new \Symfony\Component\Yaml\Parser();
$meta = $yamlParser->parse($rawMetaMatches[3]);
if ($meta !== null) {
// the parser may return a string for non-YAML 1-liners
// assume that this string is the page title
$meta = is_array($meta) ? array_change_key_case($meta, CASE_LOWER) : array('title' => $meta);
} else {
$meta = array();
}
foreach ($headers as $fieldId => $fieldName) {
$fieldName = strtolower($fieldName);
if (isset($meta[$fieldName])) {
// rename field (e.g. remove whitespaces)
if ($fieldId != $fieldName) {
$meta[$fieldId] = $meta[$fieldName];
unset($meta[$fieldName]);
}
} elseif (!isset($meta[$fieldId])) {
// guarantee array key existance
$meta[$fieldId] = '';
}
}
if (!empty($meta['date'])) {
// workaround for issue #336
// Symfony YAML interprets ISO-8601 datetime strings and returns timestamps instead of the string
// this behavior conforms to the YAML standard, i.e. this is no bug of Symfony YAML
if (is_int($meta['date'])) {
$meta['time'] = $meta['date'];
$rawDateFormat = date('H:i:s', $meta['time']) === '00:00:00' ? 'Y-m-d' : 'Y-m-d H:i:s';
$meta['date'] = date($rawDateFormat, $meta['time']);
} else {
$meta['time'] = strtotime($meta['date']);
}
$meta['date_formatted'] = utf8_encode(strftime($this->getConfig('date_format'), $meta['time']));
} else {
$meta['time'] = $meta['date_formatted'] = '';
}
} else {
// guarantee array key existance
$meta = array_fill_keys(array_keys($headers), '');
$meta['time'] = $meta['date_formatted'] = '';
}
return $meta;
}