Editable::asInline()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
nc 1
cc 1
eloc 3
nop 1
1
<?php
2
3
namespace Giftd\Editor;
4
5
class Editable implements \JsonSerializable
6
{
7
    /**
8
     * Editable attributes.
9
     *
10
     * @var array
11
     */
12
    protected $attributes = [];
13
14
    /**
15
     * Editable constructor.
16
     *
17
     * @param string $name
18
     */
19
    public function __construct(string $name)
20
    {
21
        $this->attributes['name'] = $name;
22
    }
23
24
    /**
25
     * Returns Editable's attributes.
26
     *
27
     * @return array
28
     */
29
    public function attributes()
30
    {
31
        return $this->attributes;
32
    }
33
34
    /**
35
     * Data for JSON serialization.
36
     *
37
     * @return array
38
     */
39
    public function jsonSerialize()
40
    {
41
        return $this->attributes();
42
    }
43
44
    /**
45
     * Sets editable title.
46
     *
47
     * @param string|null $title
48
     *
49
     * @return $this
50
     */
51
    public function titledAs(string $title = null)
52
    {
53
        $this->attributes['title'] = $title;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Sets editable type.
60
     *
61
     * @param string $type
62
     *
63
     * @return $this
64
     */
65
    public function usingType(string $type)
66
    {
67
        $this->attributes['type'] = $type;
68
69
        if ($type === 'text') {
70
            $this->asInline(false);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * Sets editable value.
78
     *
79
     * @param null $value
80
     *
81
     * @return $this
82
     */
83
    public function withValue($value = null)
84
    {
85
        $this->attributes['value'] = $value;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Sets editable options list.
92
     *
93
     * @param array $options
94
     *
95
     * @return $this
96
     */
97
    public function withOptions(array $options = [])
98
    {
99
        $data = [];
100
101
        foreach ($options as $value => $label) {
102
            $data[] = ['value' => $value, 'label' => $label];
103
        }
104
105
        $this->attributes['options'] = $data;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Sets custom editable attributes.
112
     *
113
     * @param array $attributes
114
     *
115
     * @return $this
116
     */
117
    public function withAttributes(array $attributes = [])
118
    {
119
        $this->attributes = array_merge($this->attributes, $attributes);
120
121
        return $this;
122
    }
123
124
    /**
125
     * Set upload URL.
126
     *
127
     * @param string $url
128
     *
129
     * @return Editable
130
     */
131
    public function uploadTo(string $url)
132
    {
133
        return $this->withSetting('upload_url', $url);
134
    }
135
136
    /**
137
     * Set request headers.
138
     *
139
     * @param array $headers
140
     *
141
     * @return Editable
142
     */
143
    public function withHeaders(array $headers)
144
    {
145
        return $this->withSetting('headers', $headers);
146
    }
147
148
    /**
149
     * Set custom setting value.
150
     *
151
     * @param string $setting
152
     * @param $value
153
     *
154
     * @return $this
155
     */
156
    public function withSetting(string $setting, $value)
157
    {
158
        if (!isset($this->attributes['settings'])) {
159
            $this->attributes['settings'] = [];
160
        }
161
162
        $this->attributes['settings'][$setting] = $value;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Creates new placeholder and pushes it to editable's placeholders list.
169
     *
170
     * @param string $name
171
     * @param string $title
172
     * @param $value
173
     * @param array $attributes
174
     *
175
     * @return $this
176
     */
177
    public function placeholder(string $name, string $title, $value, array $attributes = [])
178
    {
179
        if (!isset($this->attributes['placeholders'])) {
180
            $this->attributes['placeholders'] = [];
181
        }
182
183
        $this->attributes['placeholders'][$name] = array_merge([
184
            'title' => $title,
185
            'value' => $value,
186
        ], $attributes);
187
188
        return $this;
189
    }
190
191
    /**
192
     * Creates multiple placeholders from array.
193
     *
194
     * @param array $placeholders
195
     * @return $this
196
     */
197
    public function placeholders(array $placeholders)
198
    {
199
        foreach ($placeholders as $ph) {
200
            if (!isset($ph['name']) || !isset($ph['title']) || !isset($ph['value'])) {
201
                continue;
202
            }
203
204
            $this->placeholder($ph['name'], $ph['title'], $ph['value'], $ph['attributes'] ?? []);
205
        }
206
207
        return $this;
208
    }
209
210
    /**
211
     * Sets inline mode.
212
     *
213
     * @param bool $inline = true
214
     *
215
     * @return $this
216
     */
217
    public function asInline(bool $inline = true)
218
    {
219
        $this->attributes['inline'] = $inline;
220
221
        return $this;
222
    }
223
}
224