1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Giftd\Editor; |
4
|
|
|
|
5
|
|
|
abstract class Customization |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Actual customization data. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $data = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Preview data. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $previewData = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Customization-related settings. |
23
|
|
|
* |
24
|
|
|
* @var SettingsManager |
25
|
|
|
*/ |
26
|
|
|
protected $settings; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates groupped editables list. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
abstract public function editables(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Builds template data. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
abstract public function build(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns ['customization_name' => 'setting_key'] map. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function settingsMap() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
// |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Triggers before the save settings operation. |
56
|
|
|
*/ |
57
|
|
|
public function beforeSaving() |
58
|
|
|
{ |
59
|
|
|
// |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Saves customization settings. |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function save() |
68
|
|
|
{ |
69
|
|
|
$this->beforeSaving(); |
70
|
|
|
|
71
|
|
|
$this->settings->saveReplaced(); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns customization data. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function all() |
82
|
|
|
{ |
83
|
|
|
return $this->data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets preview data. |
88
|
|
|
* |
89
|
|
|
* @param array $previewData |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setPreviewData(array $previewData = []) |
94
|
|
|
{ |
95
|
|
|
if (!count($previewData)) { |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$settingsMap = $this->settingsMap(); |
100
|
|
|
|
101
|
|
|
if (count($settingsMap) > 0) { |
102
|
|
|
$previewData = $this->renamePreviewDataKeys($previewData, $settingsMap); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->settings->replace($previewData); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets settings manager. |
112
|
|
|
* |
113
|
|
|
* @param SettingsManager $settings |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function setSettings(SettingsManager $settings) |
118
|
|
|
{ |
119
|
|
|
$this->settings = $settings; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Renames customization keys to settings keys. |
126
|
|
|
* |
127
|
|
|
* @param array $previewData |
128
|
|
|
* @param array $map |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected function renamePreviewDataKeys(array $previewData, array $map) |
133
|
|
|
{ |
134
|
|
|
foreach ($map as $customizationName => $settingsKey) { |
135
|
|
|
if (!array_key_exists($customizationName, $previewData)) { |
136
|
|
|
continue; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$previewData[$settingsKey] = $previewData[$customizationName]; |
140
|
|
|
unset($previewData[$customizationName]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $previewData; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns value from preview data or default value. |
148
|
|
|
* |
149
|
|
|
* @param string $key |
150
|
|
|
* @param null $defaultValue |
151
|
|
|
* |
152
|
|
|
* @return mixed|null |
153
|
|
|
*/ |
154
|
|
|
protected function setting(string $key, $defaultValue = null) |
155
|
|
|
{ |
156
|
|
|
return $this->settings->get($key, $defaultValue); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns value from preview data or default value with replaced placeholders. |
161
|
|
|
* |
162
|
|
|
* @param string $key |
163
|
|
|
* @param array $placeholders |
164
|
|
|
* @param null $defaultValue |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
protected function settingWithPlaceholders(string $key, array $placeholders = [], $defaultValue = null) |
169
|
|
|
{ |
170
|
|
|
$text = $this->setting($key, $defaultValue); |
171
|
|
|
|
172
|
|
|
return $this->settings->replacePlaceholders($text, $placeholders); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Creates new editables group. |
177
|
|
|
* |
178
|
|
|
* @param string $id |
179
|
|
|
* @param string $title |
180
|
|
|
* @param \Closure $settingsFactory |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
protected function group(string $id, string $title, \Closure $settingsFactory) |
185
|
|
|
{ |
186
|
|
|
return [ |
187
|
|
|
'id' => $id, |
188
|
|
|
'title' => $title, |
189
|
|
|
'settings' => $settingsFactory(), |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Creates new Editable. |
195
|
|
|
* |
196
|
|
|
* @param string $name |
197
|
|
|
* @param string $type |
198
|
|
|
* @param string|null $title |
199
|
|
|
* @param null $value |
200
|
|
|
* @param array $attributes |
201
|
|
|
* |
202
|
|
|
* @return Editable |
203
|
|
|
*/ |
204
|
|
|
protected function editable(string $name, string $type, string $title = null, $value = null, array $attributes = []) |
205
|
|
|
{ |
206
|
|
|
return (new Editable($name)) |
207
|
|
|
->titledAs($title) |
208
|
|
|
->usingType($type) |
209
|
|
|
->withValue($value) |
210
|
|
|
->withAttributes($attributes); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|