Completed
Push — master ( 2b201f...eb239b )
by Timur
02:13
created

Customization::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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