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
|
|
|
* Returns customization data. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function all() |
60
|
|
|
{ |
61
|
|
|
return $this->data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets preview data. |
66
|
|
|
* |
67
|
|
|
* @param array $previewData |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function setPreviewData(array $previewData = []) |
72
|
|
|
{ |
73
|
|
|
if (!count($previewData)) { |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$settingsMap = $this->settingsMap(); |
78
|
|
|
|
79
|
|
|
if (count($settingsMap) > 0) { |
80
|
|
|
$previewData = $this->renamePreviewDataKeys($previewData, $settingsMap); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->settings->replace($previewData); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets settings manager. |
90
|
|
|
* |
91
|
|
|
* @param SettingsManager $settings |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setSettings(SettingsManager $settings) |
96
|
|
|
{ |
97
|
|
|
$this->settings = $settings; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Renames customization keys to settings keys. |
104
|
|
|
* |
105
|
|
|
* @param array $previewData |
106
|
|
|
* @param array $map |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function renamePreviewDataKeys(array $previewData, array $map) |
111
|
|
|
{ |
112
|
|
|
foreach ($map as $customizationName => $settingsKey) { |
113
|
|
|
if (!array_key_exists($customizationName, $previewData)) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$previewData[$settingsKey] = $previewData[$customizationName]; |
118
|
|
|
unset($previewData[$customizationName]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $previewData; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns value from preview data or default value. |
126
|
|
|
* |
127
|
|
|
* @param string $key |
128
|
|
|
* @param null $defaultValue |
129
|
|
|
* |
130
|
|
|
* @return mixed|null |
131
|
|
|
*/ |
132
|
|
|
protected function setting(string $key, $defaultValue = null) |
133
|
|
|
{ |
134
|
|
|
return $this->settings->get($key, $defaultValue); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns value from preview data or default value with replaced placeholders. |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @param array $placeholders |
142
|
|
|
* @param null $defaultValue |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
protected function settingWithPlaceholders(string $key, array $placeholders = [], $defaultValue = null) |
147
|
|
|
{ |
148
|
|
|
$text = $this->setting($key, $defaultValue); |
149
|
|
|
|
150
|
|
|
return $this->settings->replacePlaceholders($text, $placeholders); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Creates new editables group. |
155
|
|
|
* |
156
|
|
|
* @param string $id |
157
|
|
|
* @param string $title |
158
|
|
|
* @param \Closure $settingsFactory |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
protected function group(string $id, string $title, \Closure $settingsFactory) |
163
|
|
|
{ |
164
|
|
|
return [ |
165
|
|
|
'id' => $id, |
166
|
|
|
'title' => $title, |
167
|
|
|
'settings' => $settingsFactory(), |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Creates new Editable. |
173
|
|
|
* |
174
|
|
|
* @param string $name |
175
|
|
|
* @param string $type |
176
|
|
|
* @param string|null $title |
177
|
|
|
* @param null $value |
178
|
|
|
* @param array $attributes |
179
|
|
|
* |
180
|
|
|
* @return Editable |
181
|
|
|
*/ |
182
|
|
|
protected function editable(string $name, string $type, string $title = null, $value = null, array $attributes = []) |
183
|
|
|
{ |
184
|
|
|
return (new Editable($name)) |
185
|
|
|
->titledAs($title) |
186
|
|
|
->usingType($type) |
187
|
|
|
->withValue($value) |
188
|
|
|
->withAttributes($attributes); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|