-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageList.php
More file actions
150 lines (137 loc) · 3.5 KB
/
PageList.php
File metadata and controls
150 lines (137 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Definition of class PageList
*
* @copyright 2014-today Justso GmbH
* @author j.schirrmacher@justso.de
* @package justso\justtexts\model
*/
namespace justso\justtexts;
use justso\justapi\Bootstrap;
use justso\justapi\InvalidParameterException;
use justso\justapi\RequestHelper;
use justso\justapi\SystemEnvironmentInterface;
/**
* Class PageList
* @package justso\justtexts\model
*/
class PageList
{
/**
* List of pages
* @var Page[]
*/
private $pages;
/** @var SystemEnvironmentInterface */
private $env;
/**
* Initializes the page list
*/
public function __construct(SystemEnvironmentInterface $env)
{
$this->env = $env;
$this->pages = array();
$configuration = $env->getBootstrap()->getConfiguration();
foreach ($configuration['pages'] as $key => $value) {
$page = $this->createPageObject($key, $value);
$this->pages[$page->getId()] = $page;
}
}
/**
* @param string $key
* @param string $value
* @return \justso\justtexts\PageInterface
*/
private function createPageObject($key, $value, RequestHelper $request = null)
{
return $this->env->getDIC()->get('\justso\justtexts\Page', [$key, $value, $request]);
}
/**
* Returns a list of page names.
*
* @return string[]
*/
public function getPages()
{
return array_keys($this->pages);
}
/**
* Returns the Page object identified by its page name.
*
* @param string $name
* @return Page
* @throws \justso\justapi\InvalidParameterException
*/
public function getPage($name)
{
if (!isset($this->pages[$name])) {
throw new InvalidParameterException("Page not found");
}
return $this->pages[$name];
}
/**
* Adds a new page with the given $id with data from the request.
*
* @param string $id
* @param RequestHelper $request
* @return Page
*/
public function addPageFromRequest($id, RequestHelper $request)
{
$page = $this->createPageObject(null, null, $request);
$this->pages[$id] = $page;
$this->persist();
return $page;
}
/**
* Changes a page's attributes according to data from the request.
*
* @param string $id
* @param RequestHelper $request
* @return Page
*/
public function changePageFromRequest($id, $request)
{
$this->getPage($id);
$page = $this->createPageObject(null, null, $request);
$this->pages[$id] = $page;
$this->persist();
return $page;
}
/**
* Changes the id of a page.
*
* @param string $id
* @param string $newName
*/
public function renamePage($id, $newName)
{
$this->pages[$newName] = $this->getPage($id);
unset($this->pages[$id]);
$this->persist();
}
/**
* Deletes a page.
*
* @param $id
*/
public function deletePage($id)
{
$this->getPage($id);
unset($this->pages[$id]);
$this->persist();
}
/**
* Persists page list in configuration file.
*/
private function persist()
{
$bootstrap = Bootstrap::getInstance();
$config = $bootstrap->getConfiguration();
$config['pages'] = array();
foreach ($this->pages as $page) {
$page->appendConfig($config['pages']);
}
$bootstrap->setConfiguration($config);
}
}