1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace html_go\model; |
3
|
|
|
|
4
|
|
|
use html_go\exceptions\InternalException; |
5
|
|
|
|
6
|
|
|
abstract class AdminModelFactory |
7
|
|
|
{ |
8
|
|
|
protected Config $config; |
9
|
|
|
|
10
|
|
|
public function __construct(Config $config) { |
11
|
|
|
$this->config = $config; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a content object (stdClass) specifically for the admin console. |
16
|
|
|
* @param array<mixed> $params When populating with variable arguments, use the following |
17
|
|
|
* <b>named parameters<b>: |
18
|
|
|
* <ul> |
19
|
|
|
* <li>title: (required)</li> |
20
|
|
|
* <li>context: (required)</li> |
21
|
|
|
* <li>template: (required)</li> |
22
|
|
|
* <li>section: (required)</li> |
23
|
|
|
* <li>action: (required)</li> |
24
|
|
|
* <li>list: (optional)</li> |
25
|
|
|
* </ul> |
26
|
|
|
* @return \stdClass |
27
|
|
|
*/ |
28
|
|
|
public function createAdminContentObject(array $params): \stdClass { |
29
|
|
|
$contentObject = new \stdClass(); |
30
|
|
|
$this->checkSetOrFail('title', $params); |
31
|
|
|
$this->checkSetOrFail('context', $params); |
32
|
|
|
$this->checkSetOrFail('template', $params); |
33
|
|
|
$this->checkSetOrFail('section', $params); |
34
|
|
|
$this->checkSetOrFail('action', $params); |
35
|
|
|
|
36
|
|
|
$contentObject->site = $this->getSiteObject(); |
37
|
|
|
$list = []; |
38
|
|
|
if (empty($params['list']) === false && \is_array($params['list'])) { |
|
|
|
|
39
|
|
|
$list = $params['list']; |
40
|
|
|
} |
41
|
|
|
$contentObject->list = $list; |
42
|
|
|
return $this->mergeStdClassAndArray($contentObject, $params); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a content object (stdClass) which has all the basic admin related properties, but |
47
|
|
|
* nothing for the specific request. |
48
|
|
|
* @param array<mixed> $params |
49
|
|
|
* @return \stdClass |
50
|
|
|
*/ |
51
|
|
|
public function createAdminContentObjectEmpty(array $params): \stdClass { |
52
|
|
|
$contentObject = new \stdClass(); |
53
|
|
|
$this->checkSetOrFail('context', $params); |
54
|
|
|
$this->checkSetOrFail('section', $params); |
55
|
|
|
$this->checkSetOrFail('action', $params); |
56
|
|
|
$contentObject->site = $this->getSiteObject(); |
57
|
|
|
$contentObject->list = []; |
58
|
|
|
return $this->mergeStdClassAndArray($contentObject, $params); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \stdClass $contentObject |
63
|
|
|
* @param array<mixed> $params |
64
|
|
|
* @throws InternalException |
65
|
|
|
* @return \stdClass |
66
|
|
|
*/ |
67
|
|
|
private function mergeStdClassAndArray(\stdClass $contentObject, array $params): \stdClass { |
68
|
|
|
if (($json = json_encode($contentObject)) === false) { |
69
|
|
|
throw new InternalException('json_encode failed!'); |
70
|
|
|
} |
71
|
|
|
return (object)\array_merge(json_decode($json, true), $params); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* @param array<string> $params |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
private function checkSetOrFail(string $key, array $params): void { |
81
|
|
|
if (empty($params[$key])) { |
82
|
|
|
throw new \InvalidArgumentException("The '$key:' parameter has not been set!"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getSiteObject(): \stdClass { |
87
|
|
|
static $site = null; |
88
|
|
|
if (empty($site)) { |
89
|
|
|
$site = new \stdClass(); |
90
|
|
|
$site->url = $this->config->getString(Config::KEY_SITE_URL); |
91
|
|
|
$site->name = $this->config->getString(Config::KEY_SITE_NAME); |
92
|
|
|
$site->title = $this->config->getString(Config::KEY_SITE_TITLE); |
93
|
|
|
$site->description = $this->config->getString(Config::KEY_SITE_DESCRIPTION); |
94
|
|
|
$site->tagline = $this->config->getString(Config::KEY_SITE_TAGLINE); |
95
|
|
|
$site->copyright = $this->config->getString(Config::KEY_SITE_COPYRIGHT); |
96
|
|
|
$site->language = $this->config->getString(Config::KEY_LANG); |
97
|
|
|
$site->theme = $this->config->getString(Config::KEY_THEME_NAME); |
98
|
|
|
$site->tpl_engine = $this->config->getString(Config::KEY_TPL_ENGINE); |
99
|
|
|
} |
100
|
|
|
return $site; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|