1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace html_go\model; |
3
|
|
|
|
4
|
|
|
use DateTimeInterface; |
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use html_go\exceptions\InternalException; |
7
|
|
|
use html_go\indexing\IndexManager; |
8
|
|
|
use html_go\markdown\MarkdownParser; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Responsible for creating <code>Content</code> objects ready to be used in templates. |
12
|
|
|
* @author Marc L. Veary |
13
|
|
|
* @since 1.0 |
14
|
|
|
*/ |
15
|
|
|
final class ModelFactory |
16
|
|
|
{ |
17
|
|
|
private Config $config; |
18
|
|
|
private MarkdownParser $parser; |
19
|
|
|
private IndexManager $manager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ModelFactory constructor. |
23
|
|
|
* @param Config $config |
24
|
|
|
* @param MarkdownParser $parser Implementation of the |
25
|
|
|
* <code>MarkdownParser</code> interface. |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Config $config, MarkdownParser $parser, IndexManager $manager) { |
28
|
|
|
$this->config = $config; |
29
|
|
|
$this->parser = $parser; |
30
|
|
|
$this->manager = $manager; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a content object (stdClass) from an index <code>Element</code> |
35
|
|
|
* object. |
36
|
|
|
* @param \stdClass $indexElement As obtained from the <code>IndexManager</code> |
37
|
|
|
* @return \stdClass |
38
|
|
|
*/ |
39
|
|
|
public function createContentObject(\stdClass $indexElement): \stdClass { |
40
|
|
|
$contentObject = $this->loadDataFile($indexElement); |
41
|
|
|
$contentObject->key = $indexElement->key; |
42
|
|
|
if (!empty($indexElement->category)) { |
43
|
|
|
$contentObject->category = $this->getCategoryObject($indexElement->category); |
44
|
|
|
} |
45
|
|
|
if (isset($indexElement->tags)) { |
46
|
|
|
$contentObject->tags = $indexElement->tags; |
47
|
|
|
} |
48
|
|
|
if (isset($indexElement->date)) { |
49
|
|
|
if (EMPTY_VALUE === $indexElement->date) { |
50
|
|
|
$contentObject->date = $indexElement->date; |
51
|
|
|
$contentObject->timestamp = EMPTY_VALUE; |
52
|
|
|
} else { |
53
|
|
|
$dt = new \DateTime($indexElement->date); |
54
|
|
|
$contentObject->date = $dt->format($this->config->getString(Config::KEY_POST_DATE_FMT)); |
55
|
|
|
$contentObject->timestamp = $dt->format(DateTimeInterface::W3C); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$contentObject->listing = []; |
59
|
|
|
$contentObject->site = $this->getSiteObject(); |
60
|
|
|
return $contentObject; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getCategoryObject(string $slug): \stdClass { |
64
|
|
|
if ($this->manager->elementExists($slug) === false) { |
65
|
|
|
throw new \UnexpectedValueException("Element does not exist [$slug]"); |
66
|
|
|
} |
67
|
|
|
return $this->loadDataFile($this->manager->getElementFromSlugIndex($slug)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function getSiteObject(): \stdClass { |
71
|
|
|
static $site = null; |
72
|
|
|
if (empty($site)) { |
73
|
|
|
$site = new \stdClass(); |
74
|
|
|
$site->url = $this->config->getString(Config::KEY_SITE_URL); |
75
|
|
|
$site->name = $this->config->getString(Config::KEY_SITE_NAME); |
76
|
|
|
$site->title = $this->config->getString(Config::KEY_SITE_TITLE); |
77
|
|
|
$site->description = $this->config->getString(Config::KEY_SITE_DESCRIPTION); |
78
|
|
|
$site->tagline = $this->config->getString(Config::KEY_SITE_TAGLINE); |
79
|
|
|
$site->copyright = $this->config->getString(Config::KEY_SITE_COPYRIGHT); |
80
|
|
|
$site->language = $this->config->getString(Config::KEY_LANG); |
81
|
|
|
$site->theme = $this->config->getString(Config::KEY_THEME_NAME); |
82
|
|
|
$site->tpl_engine = $this->config->getString(Config::KEY_TPL_ENGINE); |
83
|
|
|
} |
84
|
|
|
return $site; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function loadDataFile(\stdClass $indexElement): \stdClass { |
88
|
|
|
if (!isset($indexElement->path) || empty($indexElement->path)) { |
89
|
|
|
throw new InvalidArgumentException("Object does not have 'path' property "./** @scrutinizer ignore-type */print_r($indexElement, true)); // @codeCoverageIgnore |
90
|
|
|
} |
91
|
|
|
if (($data = \file_get_contents($indexElement->path)) === false) { |
92
|
|
|
throw new InternalException("file_get_contents() failed opening [$indexElement->path]"); // @codeCoverageIgnore |
93
|
|
|
} |
94
|
|
|
if (($contentObject = \json_decode($data)) === null) { |
95
|
|
|
$path = $indexElement->path; |
96
|
|
|
throw new InternalException("json_decode returned null decoding [$data] from [$path]"); // @codeCoverageIgnore |
97
|
|
|
} |
98
|
|
|
return $contentObject; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|