Passed
Push — develop ( 7b7005...f909de )
by Brent
36s
created

Page::removeAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Site;
4
5
use Pageon\Html\Meta\Meta;
6
use Brendt\Stitcher\Exception\TemplateNotFoundException;
7
use Brendt\Stitcher\Site\Http\Header;
8
9
/**
10
 * A Page object represents a page entry configured in a YAML file located in the `src/sites/` directory.
11
 * Constructing a new Page requires a unique ID and an array of data. This array can hold several different arguments:
12
 *
13
 *      - `template`: the only required argument. This variable is a path to a template file.
14
 *              This path is relative to the `directories.src` or `directories.template` configuration entry
15
 * @see     \Brendt\Stitcher\Stitcher::loadTemplates
16
 *
17
 *
18
 *      - `data`: an optional array of variables which will be mapped onto the template.
19
 *              Each of these variables is parsed during compile time.
20
 * @see     \Brendt\Stitcher\Parser\AbstractParser
21
 *
22
 *      - `adapters`: an optional array of Adapters for this page. Adapters are used to adapt a page's configuration
23
 *              to another one.
24
 * @see     \Brendt\Stitcher\Adapter\Adapter
25
 *
26
 * @package Brendt\Stitcher\Site
27
 */
28
class Page
29
{
30
    protected $meta;
31
    protected $data;
32
    protected $id;
33
    protected $templatePath;
34
    protected $variables = [];
35
    protected $adapters;
36
    protected $parsedVariables = [];
37
    /** @var Header[] */
38
    private $headers = [];
39
40
    public function __construct($id, array $data = [], Meta $meta = null) {
41
        if (!isset($data['template'])) {
42
            throw new TemplateNotFoundException("No template was set for page {$id}");
43
        }
44
45
        $this->id = $id;
46
        $this->data = $data;
47
        $this->templatePath = $data['template'];
48
        $this->meta = $meta ?? new Meta();
49
50
        if (isset($data['variables'])) {
51
            $this->variables += $data['variables'];
52
        }
53
54
        if (isset($data['adapters'])) {
55
            foreach ($data['adapters'] as $type => $adapterConfig) {
56
                $this->adapters[$type] = $adapterConfig;
57
            }
58
        }
59
    }
60
61
    public static function copy(Page $page): Page {
62
        return new Page($page->id, $page->data);
63
    }
64
65
    /**
66
     * Defines a variable as parsed.
67
     * Parsed variables will be ignored by Stitcher when compiling the website.
68
     * Adapters can define parsed variables to indicate Stitcher it should skip parsing that variable during compile
69
     * time.
70
     *
71
     * @param $name
72
     *
73
     * @return Page
74
     *
75
     * @see \Brendt\Stitcher\Stitcher::parseVariables
76
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
77
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
78
     */
79
    public function setVariableIsParsed($name) {
80
        $this->parsedVariables[$name] = true;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Check whether a variable is parsed or not.
87
     * Parsed variables will be ignored by Stitcher during compile time.
88
     *
89
     * @param $name
90
     *
91
     * @return bool
92
     *
93
     * @see \Brendt\Stitcher\Stitcher::parseVariables
94
     */
95
    public function isParsedVariable($name) {
96
        return isset($this->parsedVariables[$name]);
97
    }
98
99
    public function getId() {
100
        return $this->id;
101
    }
102
103
    public function getTemplatePath() {
104
        return $this->templatePath;
105
    }
106
107
    public function getVariables() {
108
        return $this->variables;
109
    }
110
111
    public function getAdapters() {
112
        return $this->adapters;
113
    }
114
115
    /**
116
     * Get an adapter configuration by name.
117
     *
118
     * @param $name
119
     *
120
     * @return array
121
     *
122
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
123
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
124
     * @see \Brendt\Stitcher\Application\DevController::run
125
     */
126
    public function getAdapterConfig($name) {
127
        if (!isset($this->adapters[$name])) {
128
            return [];
129
        }
130
131
        return $this->adapters[$name];
132
    }
133
134
    /**
135
     * Get a variable by name.
136
     *
137
     * @param $name
138
     *
139
     * @return mixed|null
140
     *
141
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
142
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
143
     */
144
    public function getVariable($name) {
145
        if (!isset($this->variables[$name])) {
146
            return null;
147
        }
148
149
        return $this->variables[$name];
150
    }
151
152
    /**
153
     * Set the value of a variable.
154
     *
155
     * @param $name
156
     * @param $value
157
     *
158
     * @return Page
159
     *
160
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
161
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
162
     * @see \Brendt\Stitcher\Stitcher::parseVariables
163
     */
164
    public function setVariableValue($name, $value) {
165
        $this->variables[$name] = $value;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Remove an adapter.
172
     *
173
     * @param $name
174
     *
175
     * @return Page
176
     *
177
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
178
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
179
     */
180
    public function removeAdapter($name) {
181
        if (isset($this->adapters[$name])) {
182
            unset($this->adapters[$name]);
183
        }
184
185
        return $this;
186
    }
187
188
    /**
189
     * Set the ID of this page.
190
     * An page's ID can be re-set after constructing when an adapter is creating other pages based on an existing page.
191
     *
192
     * @param string $id
193
     *
194
     * @return Page
195
     *
196
     * @see \Brendt\Stitcher\adapter\CollectionAdapter::transform
197
     * @see \Brendt\Stitcher\adapter\PagincationAdapter::transform
198
     */
199
    public function setId($id) {
200
        $this->id = $id;
201
202
        return $this;
203
    }
204
205
    public function addHeader(Header $header) {
206
        $this->headers[] = $header;
207
    }
208
209
    /**
210
     * @return Header[]
211
     */
212
    public function getHeaders() : array {
213
        return $this->headers;
214
    }
215
216
    public function getMeta(): Meta {
217
        return $this->meta;
218
    }
219
}
220