1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Document; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\DataTransformer\DataTransformerInterface; |
11
|
|
|
use allejo\stakx\DataTransformer\DataTransformerManager; |
12
|
|
|
use allejo\stakx\Filesystem\File; |
13
|
|
|
use allejo\stakx\FrontMatter\FrontMatterParser; |
14
|
|
|
|
15
|
|
|
class DataItem extends ReadableDocument implements CollectableItem, TemplateReadyDocument, PermalinkDocument |
16
|
|
|
{ |
17
|
|
|
use CollectableItemTrait; |
18
|
|
|
use PermalinkDocumentTrait; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $data; |
22
|
|
|
|
23
|
|
|
/** @var DataTransformerInterface */ |
24
|
|
|
protected $dataTransformer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* DataItem constructor. |
28
|
|
|
*/ |
29
|
7 |
|
public function __construct(File $file) |
30
|
|
|
{ |
31
|
7 |
|
$this->noReadOnConstructor = true; |
32
|
|
|
|
33
|
7 |
|
parent::__construct($file); |
34
|
7 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the transformer used to convert the file contents into an array,. |
38
|
|
|
*/ |
39
|
7 |
|
public function setDataTransformer(DataTransformerManager $manager) |
40
|
|
|
{ |
41
|
7 |
|
$this->dataTransformer = $manager->getTransformer($this->getExtension()); |
42
|
6 |
|
$this->readContent(); |
43
|
6 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function evaluateFrontMatter(array $variables = [], array $complexVariables = []) |
49
|
|
|
{ |
50
|
|
|
$workspace = array_merge($this->data, $variables); |
51
|
|
|
$parser = new FrontMatterParser($workspace, [ |
52
|
|
|
'filename' => $this->getFileName(), |
53
|
|
|
'basename' => $this->getBaseName(), |
54
|
|
|
]); |
55
|
|
|
$parser->addComplexVariables($complexVariables); |
56
|
|
|
$parser->parse(); |
57
|
|
|
|
58
|
|
|
if (!is_null($parser) && $parser->hasExpansion()) |
59
|
|
|
{ |
60
|
|
|
throw new \LogicException('The permalink for this item has not been set.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$permalink = $workspace['permalink']; |
64
|
|
|
|
65
|
|
View Code Duplication |
if (is_array($permalink)) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->permalink = $permalink[0]; |
68
|
|
|
array_shift($permalink); |
69
|
|
|
$this->redirects = $permalink; |
70
|
|
|
} |
71
|
|
|
else |
72
|
|
|
{ |
73
|
|
|
$this->permalink = $permalink; |
74
|
|
|
$this->redirects = []; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function buildPermalink($force = false) |
82
|
|
|
{ |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
6 |
|
public function readContents($mixed) |
89
|
|
|
{ |
90
|
6 |
|
$content = $this->file->getContents(); |
91
|
6 |
|
$this->data = $this->dataTransformer->transformData($content); |
92
|
6 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
4 |
|
public function getContent() |
98
|
|
|
{ |
99
|
4 |
|
return $this->data; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
2 |
|
public function isDraft() |
106
|
|
|
{ |
107
|
2 |
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
4 |
|
public function createJail() |
114
|
|
|
{ |
115
|
4 |
|
$whiteListedFunctions = array_merge(FrontMatterDocument::$whiteListedFunctions, [ |
116
|
4 |
|
]); |
117
|
|
|
|
118
|
|
|
$jailedFunctions = [ |
119
|
4 |
|
'getDataset' => 'getNamespace', |
120
|
|
|
]; |
121
|
|
|
|
122
|
4 |
|
return new JailedDocument($this, $whiteListedFunctions, $jailedFunctions); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/// |
126
|
|
|
// JsonSerializable implementation |
127
|
|
|
/// |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function jsonSerialize() |
133
|
|
|
{ |
134
|
|
|
return $this->data; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/// |
138
|
|
|
// IteratorAggregate implementation |
139
|
|
|
/// |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function getIterator() |
145
|
|
|
{ |
146
|
1 |
|
return new \ArrayIterator($this->data); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/// |
150
|
|
|
// ArrayAccess implementation |
151
|
|
|
/// |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function offsetExists($offset) |
157
|
|
|
{ |
158
|
|
|
return isset($this->data[$offset]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
2 |
|
public function offsetGet($offset) |
165
|
|
|
{ |
166
|
2 |
|
$fxnCall = 'get' . ucfirst($offset); |
167
|
|
|
|
168
|
2 |
View Code Duplication |
if (in_array($fxnCall, FrontMatterDocument::$whiteListedFunctions) && method_exists($this, $fxnCall)) |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
return call_user_func_array([$this, $fxnCall], []); |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
if (isset($this->data[$offset])) { |
174
|
2 |
|
return $this->data[$offset]; |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function offsetSet($offset, $value) |
184
|
|
|
{ |
185
|
|
|
throw new \LogicException('DataItems are read-only.'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function offsetUnset($offset) |
192
|
|
|
{ |
193
|
|
|
throw new \LogicException('DataItems are read-only.'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.