fibable/lib/FeedParser.php

44 lines
1.4 KiB
PHP

<?php
final class FeedParser
{
public static function parse(string $file_path): Post
{
$post = new Post();
$post->date = self::getDate($file_path);
$post->id = preg_replace(ArticleParser::PATTERN_DATE, ArticleParser::REPLACE_REF, $file_path);
$lines = file($file_path, FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
if (substr($line, 0, 2) === "# ") {
$post->title = substr($line, 2);
continue;
}
if (preg_match(ArticleParser::PATTERN_TAGS, $line, $matches)) {
$post->tags = explode(",", $matches[1]);
break;
}
}
$post->content = self::getContent($post->date, $post->id);
return $post;
}
private static function getDate(string $file_path): int
{
$strdate = preg_replace(ArticleParser::PATTERN_DATE, ArticleParser::REPLACE_DATE, $file_path);
list($year, $month, $day) = explode("-", $strdate, 3);
$strtime = date("H:i:s", filemtime($file_path));
list($hour, $minute, $second) = explode(":", $strtime);
return mktime($hour, $minute, $second, $month, $day, $year);
}
private static function getContent(int $date, string $id): string
{
$file = "./data/cache/" . date("Y-m-d", $date) . "_${id}.phtml";
ob_start();
include $file;
return ob_get_clean();
}
}