Customization::settingsMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * Customization-related settings.
16
     *
17
     * @var SettingsManager
18
     */
19
    protected $settings;
20
21
    /**
22
     * List of disabled common editables names.
23
     *
24
     * @var array
25
     */
26
    protected $disabledEditables = [];
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 value for given customization data key or default value.
78
     *
79
     * @param string $key
80
     * @param mixed  $defaultValue = null
81
     *
82
     * @return mixed
83
     */
84
    public function get(string $key, $defaultValue = null)
85
    {
86
        return $this->data[$key] ?? $defaultValue;
87
    }
88
89
    /**
90
     * Returns customization data.
91
     *
92
     * @return array
93
     */
94
    public function all()
95
    {
96
        return $this->data;
97
    }
98
99
    /**
100
     * Sets preview data.
101
     *
102
     * @param array $previewData
103
     *
104
     * @return $this
105
     */
106
    public function setPreviewData(array $previewData = [])
107
    {
108
        if (!count($previewData)) {
109
            return $this;
110
        }
111
112
        $settingsMap = $this->settingsMap();
113
114
        if (count($settingsMap) > 0) {
115
            $previewData = $this->renamePreviewDataKeys($previewData, $settingsMap);
116
        }
117
118
        $this->settings->replace($previewData);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Sets settings manager.
125
     *
126
     * @param SettingsManager $settings
127
     *
128
     * @return $this
129
     */
130
    public function setSettings(SettingsManager $settings)
131
    {
132
        $this->settings = $settings;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param array|string $editables
139
     *
140
     * @return $this
141
     */
142
    public function disableEditables($editables)
143
    {
144
        if (is_string($editables)) {
145
            $editables = [$editables];
146
        } elseif (!is_array($editables)) {
147
            throw new \InvalidArgumentException('Editables must be an array or string, '.gettype($editables).' given');
148
        }
149
150
        $this->disabledEditables = array_merge($this->disabledEditables, $editables);
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getDisabledEditables()
159
    {
160
        return $this->disabledEditables;
161
    }
162
163
    /**
164
     * Renames customization keys to settings keys.
165
     *
166
     * @param array $previewData
167
     * @param array $map
168
     *
169
     * @return array
170
     */
171
    protected function renamePreviewDataKeys(array $previewData, array $map)
172
    {
173
        foreach ($map as $customizationName => $settingsKey) {
174
            if (!array_key_exists($customizationName, $previewData)) {
175
                continue;
176
            } elseif ($customizationName === $settingsKey) {
177
                continue;
178
            }
179
180
            $previewData[$settingsKey] = $previewData[$customizationName];
181
            unset($previewData[$customizationName]);
182
        }
183
184
        return $previewData;
185
    }
186
187
    /**
188
     * Returns value from preview data or default value.
189
     *
190
     * @param string $key
191
     * @param null   $defaultValue
192
     *
193
     * @return mixed|null
194
     */
195
    protected function setting(string $key, $defaultValue = null)
196
    {
197
        return $this->settings->get($key, $defaultValue);
198
    }
199
200
    /**
201
     * Returns value from preview data or default value with replaced placeholders.
202
     *
203
     * @param string $key
204
     * @param array  $placeholders
205
     * @param null   $defaultValue
206
     *
207
     * @return string
208
     */
209
    protected function settingWithPlaceholders(string $key, array $placeholders = [], $defaultValue = null)
210
    {
211
        $text = $this->setting($key, $defaultValue);
212
213
        return $this->settings->replacePlaceholders($text, $placeholders);
214
    }
215
216
    /**
217
     * Creates new editables group.
218
     *
219
     * @param string   $id
220
     * @param string   $title
221
     * @param \Closure $settingsFactory
222
     *
223
     * @return array
224
     */
225
    protected function group(string $id, string $title, \Closure $settingsFactory)
226
    {
227
        return [
228
            'id' => $id,
229
            'title' => $title,
230
            'settings' => $settingsFactory(),
231
        ];
232
    }
233
234
    /**
235
     * Creates new Editable.
236
     *
237
     * @param string      $name
238
     * @param string      $type
239
     * @param string|null $title
240
     * @param null        $value
241
     * @param array       $attributes
242
     *
243
     * @return Editable
244
     */
245
    protected function editable(string $name, string $type, string $title = null, $value = null, array $attributes = [])
246
    {
247
        return (new Editable($name))
248
            ->titledAs($title)
249
            ->usingType($type)
250
            ->withValue($value)
251
            ->withAttributes($attributes);
252
    }
253
}
254