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 object (stdClass). |
35
|
|
|
* @param \stdClass $indexElement As obtained from the <code>IndexManager</code> |
36
|
|
|
* @return \stdClass |
37
|
|
|
*/ |
38
|
|
|
public function createContentObject(\stdClass $indexElement): \stdClass { |
39
|
|
|
if ($indexElement->section === TAG_SECTION) { |
40
|
|
|
$contentObject = $this->createEmptyContentObject(); |
41
|
|
|
$contentObject->title = \substr($indexElement->key, \strlen(TAG_SECTION) + 1); |
42
|
|
|
} else { |
43
|
|
|
$contentObject = $this->loadDataFile($indexElement); |
44
|
|
|
$contentObject->body = $this->restoreNewlines($contentObject->body); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$contentObject->key = $indexElement->key; |
48
|
|
|
$contentObject->list = []; |
49
|
|
|
$contentObject->site = $this->getSiteObject(); |
50
|
|
|
|
51
|
|
|
if (!empty($indexElement->category)) { |
52
|
|
|
$contentObject->category = $this->getCategoryObject($indexElement->category); |
53
|
|
|
} |
54
|
|
|
if (isset($indexElement->tags)) { |
55
|
|
|
$contentObject->tags = $indexElement->tags; |
56
|
|
|
} |
57
|
|
|
if (!empty($indexElement->date)) { |
58
|
|
|
$dt = $this->getContentDate($indexElement->date); |
59
|
|
|
$contentObject->date = $dt[0]; |
60
|
|
|
$contentObject->timestamp = $dt[1]; |
61
|
|
|
} |
62
|
|
|
if (empty($contentObject->summary)) { |
63
|
|
|
$contentObject->summary = $this->getSummary($contentObject->body); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $contentObject; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $dateStr |
71
|
|
|
* @return array<string> |
72
|
|
|
*/ |
73
|
|
|
private function getContentDate(string $dateStr): array { |
74
|
|
|
if (EMPTY_VALUE === $dateStr) { |
75
|
|
|
$date = $timestamp = EMPTY_VALUE; |
76
|
|
|
} else { |
77
|
|
|
$dt = new \DateTime($dateStr); |
78
|
|
|
$date = $dt->format($this->config->getString(Config::KEY_POST_DATE_FMT)); |
79
|
|
|
$timestamp = $dt->format(DateTimeInterface::W3C); |
80
|
|
|
} |
81
|
|
|
return [$date, $timestamp]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getCategoryObject(string $slug): \stdClass { |
85
|
|
|
if ($this->manager->elementExists($slug) === false) { |
86
|
|
|
throw new \UnexpectedValueException("Element does not exist [$slug]"); // @codeCoverageIgnore |
87
|
|
|
} |
88
|
|
|
return $this->loadDataFile($this->manager->getElementFromSlugIndex($slug)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getSiteObject(): \stdClass { |
92
|
|
|
static $site = null; |
93
|
|
|
if (empty($site)) { |
94
|
|
|
$site = new \stdClass(); |
95
|
|
|
$site->url = $this->config->getString(Config::KEY_SITE_URL); |
96
|
|
|
$site->name = $this->config->getString(Config::KEY_SITE_NAME); |
97
|
|
|
$site->title = $this->config->getString(Config::KEY_SITE_TITLE); |
98
|
|
|
$site->description = $this->config->getString(Config::KEY_SITE_DESCRIPTION); |
99
|
|
|
$site->tagline = $this->config->getString(Config::KEY_SITE_TAGLINE); |
100
|
|
|
$site->copyright = $this->config->getString(Config::KEY_SITE_COPYRIGHT); |
101
|
|
|
$site->language = $this->config->getString(Config::KEY_LANG); |
102
|
|
|
$site->theme = $this->config->getString(Config::KEY_THEME_NAME); |
103
|
|
|
$site->tpl_engine = $this->config->getString(Config::KEY_TPL_ENGINE); |
104
|
|
|
} |
105
|
|
|
return $site; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function loadDataFile(\stdClass $indexElement): \stdClass { |
109
|
|
|
if (empty($indexElement->path)) { |
110
|
|
|
throw new InvalidArgumentException("Object does not have 'path' property "./** @scrutinizer ignore-type */print_r($indexElement, true)); // @codeCoverageIgnore |
111
|
|
|
} |
112
|
|
|
$path = $indexElement->path; |
113
|
|
|
if (($data = \file_get_contents($path)) === false) { |
114
|
|
|
throw new InternalException("file_get_contents() failed opening [$path]"); // @codeCoverageIgnore |
115
|
|
|
} |
116
|
|
|
if (($contentObject = \json_decode($data)) === null) { |
117
|
|
|
throw new InternalException("json_decode returned null decoding [$data] from [$path]"); // @codeCoverageIgnore |
118
|
|
|
} |
119
|
|
|
return $contentObject; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* This is used for tags only as tags don't have an associated file on the |
124
|
|
|
* filesystem. |
125
|
|
|
* @return \stdClass |
126
|
|
|
*/ |
127
|
|
|
private function createEmptyContentObject(): \stdClass { |
128
|
|
|
$obj = new \stdClass(); |
129
|
|
|
$obj->key = EMPTY_VALUE; |
130
|
|
|
$obj->section = EMPTY_VALUE; |
131
|
|
|
$obj->body = EMPTY_VALUE; |
132
|
|
|
$obj->title = EMPTY_VALUE; |
133
|
|
|
return $obj; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the summary for the content object. If '<!--more-->' is used within |
138
|
|
|
* the body, then this is removed once the summary is obtained. |
139
|
|
|
* @param string $body the body. The '<!--more--> removed if found thus passed by reference. |
140
|
|
|
* @return string The summary text. If no summary is defined, returns an empty string |
141
|
|
|
*/ |
142
|
|
|
private function getSummary(string &$body): string { |
143
|
|
|
$pos = \strpos($body, '<!--more-->'); |
144
|
|
|
if ($pos !== false) { |
145
|
|
|
$summary = \substr($body, 0, $pos); |
146
|
|
|
$body = \str_replace('<!--more-->', '', $body); |
147
|
|
|
return $summary; |
148
|
|
|
} |
149
|
|
|
return ''; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Newlines must be encoded for PHP functions. So we use '<nl>' to for '\n'. |
154
|
|
|
* This method replaces '<nl>' with '\n'. |
155
|
|
|
* @param string $text |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
private function restoreNewlines(string $text): string { |
159
|
|
|
return \str_replace('<nl>', '\n', $text); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|