-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
ContentTranslation.php
248 lines (216 loc) · 4.17 KB
/
ContentTranslation.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Kirby\Cms;
use Kirby\Toolkit\Properties;
/**
* Each page, file or site can have multiple
* translated versions of their content,
* represented by this class
*
* @package Kirby Cms
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class ContentTranslation
{
use Properties;
/**
* @var string
*/
protected $code;
/**
* @var array
*/
protected $content;
/**
* @var string
*/
protected $contentFile;
/**
* @var Model
*/
protected $parent;
/**
* @var string
*/
protected $slug;
/**
* Creates a new translation object
*
* @param array $props
*/
public function __construct(array $props)
{
$this->setRequiredProperties($props, ['parent', 'code']);
$this->setOptionalProperties($props, ['slug', 'content']);
}
/**
* Improve `var_dump` output
*
* @return array
*/
public function __debugInfo(): array
{
return $this->toArray();
}
/**
* Returns the language code of the
* translation
*
* @return string
*/
public function code(): string
{
return $this->code;
}
/**
* Returns the translation content
* as plain array
*
* @return array
*/
public function content(): array
{
$parent = $this->parent();
if ($this->content === null) {
$this->content = $parent->readContent($this->code());
}
$content = $this->content;
// merge with the default content
if ($this->isDefault() === false && $defaultLanguage = $parent->kirby()->defaultLanguage()) {
$default = [];
if ($defaultTranslation = $parent->translation($defaultLanguage->code())) {
$default = $defaultTranslation->content();
}
$content = array_merge($default, $content);
}
return $content;
}
/**
* Absolute path to the translation content file
*
* @return string
*/
public function contentFile(): string
{
return $this->contentFile = $this->parent->contentFile($this->code, true);
}
/**
* Checks if the translation file exists
*
* @return bool
*/
public function exists(): bool
{
return file_exists($this->contentFile()) === true;
}
/**
* Returns the translation code as id
*
* @return string
*/
public function id(): string
{
return $this->code();
}
/**
* Checks if the this is the default translation
* of the model
*
* @return bool
*/
public function isDefault(): bool
{
if ($defaultLanguage = $this->parent->kirby()->defaultLanguage()) {
return $this->code() === $defaultLanguage->code();
}
return false;
}
/**
* Returns the parent page, file or site object
*
* @return \Kirby\Cms\Model
*/
public function parent()
{
return $this->parent;
}
/**
* @param string $code
* @return $this
*/
protected function setCode(string $code)
{
$this->code = $code;
return $this;
}
/**
* @param array|null $content
* @return $this
*/
protected function setContent(array $content = null)
{
if ($content !== null) {
$this->content = array_change_key_case($content);
} else {
$this->content = null;
}
return $this;
}
/**
* @param \Kirby\Cms\Model $parent
* @return $this
*/
protected function setParent(Model $parent)
{
$this->parent = $parent;
return $this;
}
/**
* @param string|null $slug
* @return $this
*/
protected function setSlug(string $slug = null)
{
$this->slug = $slug;
return $this;
}
/**
* Returns the custom translation slug
*
* @return string|null
*/
public function slug(): ?string
{
return $this->slug ??= ($this->content()['slug'] ?? null);
}
/**
* Merge the old and new data
*
* @param array|null $data
* @param bool $overwrite
* @return $this
*/
public function update(array $data = null, bool $overwrite = false)
{
$data = array_change_key_case((array)$data);
$this->content = $overwrite === true ? $data : array_merge($this->content(), $data);
return $this;
}
/**
* Converts the most important translation
* props to an array
*
* @return array
*/
public function toArray(): array
{
return [
'code' => $this->code(),
'content' => $this->content(),
'exists' => $this->exists(),
'slug' => $this->slug(),
];
}
}