Completed
Push — master ( 80f340...bd55a1 )
by Arjay
01:26
created

Editor::toJson()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 30

Duplication

Lines 5
Ratio 16.67 %

Importance

Changes 0
Metric Value
dl 5
loc 30
rs 9.1288
c 0
b 0
f 0
cc 5
nc 10
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html\Editor;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Support\Fluent;
7
use Illuminate\Support\Str;
8
use Yajra\DataTables\Html\Editor\Fields\Field;
9
10
class Editor extends Fluent
11
{
12
    use HasEvents;
13
14
    const DISPLAY_LIGHTBOX = 'lightbox';
15
    const DISPLAY_ENVELOPE = 'envelope';
16
    const DISPLAY_BOOTSTRAP = 'bootstrap';
17
    const DISPLAY_FOUNDATION = 'foundation';
18
    const DISPLAY_JQUERYUI = 'jqueryui';
19
20
    /**
21
     * Editor constructor.
22
     *
23
     * @param string $instance
24
     */
25
    public function __construct($instance = 'editor')
26
    {
27
        $attributes['instance'] = $instance;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
28
29
        parent::__construct($attributes);
30
    }
31
32
    /**
33
     * Make new Editor instance.
34
     *
35
     * @param string $instance
36
     * @return Editor
37
     */
38
    public static function make($instance = 'editor')
39
    {
40
        return new static($instance);
41
    }
42
43
    /**
44
     * Append raw scripts.
45
     *
46
     * @param string $scripts
47
     * @return Editor
48
     */
49
    public function scripts($scripts)
50
    {
51
        $this->attributes['scripts'] = $scripts;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Set Editor's variable name / instance.
58
     *
59
     * @param $instance
60
     * @return $this
61
     */
62
    public function instance($instance)
63
    {
64
        $this->attributes['instance'] = $instance;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Set Editor's ajax parameter.
71
     *
72
     * @param string|array $ajax
73
     * @return $this
74
     * @see https://editor.datatables.net/reference/option/ajax
75
     */
76
    public function ajax($ajax)
77
    {
78
        $this->attributes['ajax'] = $ajax;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Set Editor's table source.
85
     *
86
     * @param string $table
87
     * @return $this
88
     * @see https://editor.datatables.net/reference/option/table
89
     */
90
    public function table($table)
91
    {
92
        $this->attributes['table'] = $table;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Set Editor's idSrc option.
99
     *
100
     * @param string $idSrc
101
     * @return $this
102
     * @see https://editor.datatables.net/reference/option/idSrc
103
     */
104
    public function idSrc($idSrc = 'DT_RowId')
105
    {
106
        $this->attributes['idSrc'] = $idSrc;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Set Editor's display option.
113
     *
114
     * @param string $display
115
     * @return $this
116
     * @see https://editor.datatables.net/reference/option/display
117
     */
118
    public function display($display)
119
    {
120
        $this->attributes['display'] = $display;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Set Editor's fields.
127
     *
128
     * @param array $fields
129
     * @return $this
130
     * @see https://editor.datatables.net/reference/option/fields
131
     */
132
    public function fields(array $fields)
133
    {
134
        $this->attributes['fields'] = $fields;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Set Editor's language.
141
     *
142
     * @param array $language
143
     * @return $this
144
     * @see https://editor.datatables.net/reference/option/i18n
145
     */
146
    public function language(array $language)
147
    {
148
        $this->attributes['language'] = $language;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Set Editor's template.
155
     *
156
     * @param string $template
157
     * @return $this
158
     * @see https://editor.datatables.net/reference/option/template
159
     */
160
    public function template($template)
161
    {
162
        $this->attributes['template'] = $template;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Convert the fluent instance to an array.
169
     *
170
     * @return array
171
     */
172
    public function toArray()
173
    {
174
        $array = parent::toArray();
175
176
        foreach ($array['fields'] as &$field) {
177
            if ($field instanceof Field) {
178
                $field = $field->toArray();
179
            }
180
        }
181
182
        return $array;
183
    }
184
185
    /**
186
     * Convert the fluent instance to JSON.
187
     *
188
     * @param  int  $options
189
     * @return string
190
     */
191
    public function toJson($options = 0)
192
    {
193
        $parameters = $this->jsonSerialize();
194
195
        $values = [];
196
        $replacements = [];
197
198
        foreach (array_dot($parameters) as $key => $value) {
0 ignored issues
show
Deprecated Code introduced by
The function array_dot() has been deprecated with message: Arr::dot() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
199
            if ($key === 'table') {
200
                array_set($parameters, $key, '#' . $value);
0 ignored issues
show
Deprecated Code introduced by
The function array_set() has been deprecated with message: Arr::set() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
201
            }
202
203 View Code Duplication
            if ($this->isCallbackFunction($value, $key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
                $values[] = trim($value);
205
                array_set($parameters, $key, '%' . $key . '%');
0 ignored issues
show
Deprecated Code introduced by
The function array_set() has been deprecated with message: Arr::set() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
206
                $replacements[] = '"%' . $key . '%"';
207
            }
208
        }
209
210
        $new = [];
211
        foreach ($parameters as $key => $value) {
212
            array_set($new, $key, $value);
0 ignored issues
show
Deprecated Code introduced by
The function array_set() has been deprecated with message: Arr::set() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
213
        }
214
215
        $json = json_encode($new, $options);
216
217
        $json = str_replace($replacements, $values, $json);
218
219
        return $json;
220
    }
221
222
    /**
223
     * Check if given key & value is a valid callback js function.
224
     *
225
     * @param string $value
226
     * @param string $key
227
     * @return bool
228
     */
229
    protected function isCallbackFunction($value, $key)
230
    {
231
        if (empty($value)) {
232
            return false;
233
        }
234
235
        $callbacks = config('datatables-html.callback', ['$', '$.', 'function']);
236
237
        return Str::startsWith(trim($value), $callbacks) || Str::contains($key, ['editor', 'minDate', 'maxDate']);
238
    }
239
}
240