1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BrightNucleus ChainMail Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus/ChainMail |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\ChainMail\Section; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\ChainMail\Exception\FailedToInitialiseSection; |
15
|
|
|
use BrightNucleus\ChainMail\Section; |
16
|
|
|
use BrightNucleus\Config\ConfigInterface; |
17
|
|
|
use BrightNucleus\View\ViewBuilder; |
18
|
|
|
use RuntimeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract Class AbstractSection. |
22
|
|
|
* |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
* |
25
|
|
|
* @package BrightNucleus\ChainMail |
26
|
|
|
* @author Alain Schlesser <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractSection implements Section |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Name of the Section. |
33
|
|
|
* |
34
|
|
|
* @since 1.0.0 |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $sectionName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Configuration Settings. |
42
|
|
|
* |
43
|
|
|
* @since 1.0.0 |
44
|
|
|
* |
45
|
|
|
* @var ConfigInterface |
46
|
|
|
*/ |
47
|
|
|
protected $config; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Content of the section. |
51
|
|
|
* |
52
|
|
|
* @since 1.0.0 |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $content; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* ViewBuilder to create template and section views. |
60
|
|
|
* |
61
|
|
|
* @since 1.0.0 |
62
|
|
|
* |
63
|
|
|
* @var ViewBuilder |
64
|
|
|
*/ |
65
|
|
|
protected $viewBuilder; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Instantiate a AbstractSection object. |
69
|
|
|
* |
70
|
|
|
* @since 1.0.0 |
71
|
|
|
* |
72
|
|
|
* @param ConfigInterface $config Configuration settings. |
73
|
|
|
* @param array $arguments Arguments that are passed through the constructor. |
74
|
|
|
* Contained elements: |
75
|
|
|
* string $section, string $content |
76
|
|
|
* |
77
|
|
|
* @throws RuntimeException |
78
|
|
|
*/ |
79
|
8 |
|
public function __construct($config, array $arguments) |
80
|
|
|
{ |
81
|
8 |
|
$this->config = $config; |
82
|
8 |
|
list($section, $content, $this->viewBuilder) = $arguments; |
83
|
8 |
|
$this->setSectionName($section); |
84
|
8 |
|
$this->content = $content; |
85
|
8 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the name of the Section. |
89
|
|
|
* |
90
|
|
|
* @since 1.0.0 |
91
|
|
|
* |
92
|
|
|
* @return string Name of the section. |
93
|
|
|
*/ |
94
|
8 |
|
public function getSectionName() |
95
|
|
|
{ |
96
|
8 |
|
return $this->sectionName; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the name of the Section. |
101
|
|
|
* |
102
|
|
|
* @since 1.0.0 |
103
|
|
|
* |
104
|
|
|
* @param string|null $section Optional. Name of the section. |
105
|
|
|
* |
106
|
|
|
* @throws FailedToInitialiseSection If no section name was passed. |
107
|
|
|
* @throws FailedToInitialiseSection If an unknown section name was passed. |
108
|
|
|
*/ |
109
|
8 |
|
protected function setSectionName($section = null) |
110
|
|
|
{ |
111
|
8 |
|
if (null === $section) { |
112
|
|
|
throw new FailedToInitialiseSection( |
113
|
|
|
'Initialised section without passing it a section name.' |
114
|
|
|
); |
115
|
|
|
} |
116
|
8 |
|
if ( ! array_key_exists($section, $this->config['sections'])) { |
117
|
|
|
throw new FailedToInitialiseSection( |
118
|
|
|
'Initialised section with an unknown section name.' |
119
|
|
|
); |
120
|
|
|
} |
121
|
8 |
|
$this->sectionName = $section; |
122
|
8 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Render the current Renderable for a given context. |
126
|
|
|
* |
127
|
|
|
* @since 1.0.0 |
128
|
|
|
* |
129
|
|
|
* @param array $context The context in which to render the Renderable. |
130
|
|
|
* |
131
|
|
|
* @return string Rendered output of the Renderable. |
132
|
|
|
* @throws RuntimeException |
133
|
|
|
*/ |
134
|
8 |
|
public function render(array $context) |
135
|
|
|
{ |
136
|
|
|
|
137
|
8 |
|
$viewName = $this->getViewName($context); |
138
|
8 |
|
$view = $this->viewBuilder->create($viewName); |
139
|
|
|
|
140
|
8 |
|
$context['css_class'] = $this->getCSSClass(); |
141
|
8 |
|
$context['content'] = $this->content; |
142
|
|
|
|
143
|
8 |
|
return $view->render($context); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the name of the View to use for rendering. |
148
|
|
|
* |
149
|
|
|
* @since 1.0.0 |
150
|
|
|
* |
151
|
|
|
* @param array $context The context in which to render the template. |
152
|
|
|
* |
153
|
|
|
* @return string Name of the view. |
154
|
|
|
*/ |
155
|
8 |
|
protected function getViewName(array $context) |
156
|
|
|
{ |
157
|
8 |
|
return $this->config['sections'][$this->getSectionName()]['view_name'] |
158
|
8 |
|
. '.' . $context['format']; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the CSS class that is used for the section. |
163
|
|
|
* |
164
|
|
|
* @since 1.0.0 |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
8 |
|
protected function getCSSClass() |
169
|
|
|
{ |
170
|
8 |
|
return $this->config['sections'][$this->getSectionName()]['css_class']; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|