-1, 'path' => '/404.md', 'title' => '404 Page', 'metadata' => [ 'type' => '404' ] ]; return $post_data; } public static function _deduce_type($path) { $ext = pathinfo($path, PATHINFO_EXTENSION); if(preg_match("/\.(\w+)\.md$/", $path, $ext_match)) { $ext = $ext_match[1]; } $ext_mapping = [ '' => 'directory', 'md' => 'text/markdown', 'png' => 'image', 'jpg' => 'image', 'jpeg' => 'image' ]; return $ext_mapping[$ext] ?? '?'; } public static function _deduce_icon($type) { $icon_mapping = [ '' => 'question', 'text/markdown' => 'markdown', 'blog' => 'markdown', 'blog_list' => 'rectangle-list', 'directory' => 'folder', 'gallery' => 'images', 'image' => 'image' ]; return $icon_mapping[$type] ?? 'unknown'; } function __construct($post_handler, $post_data, $site_defaults) { $this->handler = $post_handler; $this->content_html = null; $this->content_markdown = null; $this->site_defaults = $site_defaults; if(!isset($post_data) or !isset($post_data['id'])) { $post_data = $this->_generate_404($post_data); } $data = $post_data; if($data['path'] == '') { $data['path'] = '/'; $data['title'] ??= 'root'; $data['basename'] ??= 'root'; } $post_data['host'] ??= 'localhost:8081'; $data['url'] ??= 'http://' . $post_data['host'] . $post_data['path']; $data['basename'] ??= basename($data['path']); $data['title'] ??= basename($data['path']); $data['tags'] ??= []; $data['type'] ??= self::_deduce_type($post_data['path']); $data['icon'] ??= self::_deduce_icon($data['type']); if(isset($sql_meta['media_url'])) { $data['thumb_url'] ??= $data['media_url']; } $data['preview_image'] ??= $data['banners'][0]['src'] ?? null; $data['brief'] ??= $data['title']; $this->data = $data; } public function __get($name) { if($name == 'html') { return $this->get_html(); } if($name == 'markdown') { return $this->get_markdown(); } if($name == 'json') { return $this->to_json(); } if($name == 'child_posts') { return $this->get_child_posts(); } if($name == 'parent') { return $this->get_parent_post(); } if(isset($this->data[$name])) { return $this->data[$name]; } if(is_null($this->site_defaults)) { throw new RuntimeException("Post site defaults have not been set properly!"); } return $this->site_defaults[$name] ?? null; } public function offsetGet($offset) : mixed { return $this->__get($offset) ?? null; } public function offsetExists($offset) : bool { if(isset($this->data[$offset])) { return true; } if(isset($this->site_defaults[$offset])) { return true; } return !is_null($this->offsetGet($offset)); } public function offsetSet($offset, $value) : void { $this->data[$offset] = $value; } public function offsetUnset($offset) : void { unset($this->data[$offset]); } public function get_html() { $this->content_html ??= $this->handler->render_post($this); return $this->content_html; } public function get_markdown() { $this->content_markdown ??= $this->handler->get_markdown_for($this); return $this->content_markdown; } public function get_child_posts(...$search_args) { if(count($search_args) == 0) { $this->_child_posts ??= $this->handler->get_children_for($this); return $this->_child_posts; } else { return $this->handler->get_children_for($this, ...$search_args); } } public function to_array($options = []) { $out_data = $this->data; if(isset($options['markdown'])) { $out_data['markdown'] = $this->get_markdown(); } if(isset($options['html'])) { $out_data['html'] = $this->get_html(); } if(isset($options['children'])) { die(); } return $out_data; } public function to_json($options = []) { return json_encode($this->to_array($options)); } public function get_parent_post() { $parent_path = dirname($this->data['path']); if($parent_path == '') return null; $this->_parent_post ??= $this->handler->get_post($parent_path); return $this->_parent_post; } } ?>