Completed
Push — master ( 062b68...81f5ad )
by Terzi
08:48
created

Mutable::editors()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 5
nop 2
dl 0
loc 16
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Form\Collection;
4
5
use Terranet\Administrator\Collection\Mutable as BaseMutableCollection;
6
use Terranet\Administrator\Columns\MediaElement;
7
use Terranet\Administrator\Exception;
8
use Terranet\Administrator\Field\Textarea;
9
use Terranet\Administrator\Form\FormElement;
10
use Terranet\Administrator\Form\FormSection;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\FormSection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Terranet\Administrator\Form\InputFactory;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\InputFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Terranet\Administrator\Form\Type\Ckeditor;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\Type\Ckeditor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Terranet\Administrator\Form\Type\Markdown;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\Type\Markdown was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Terranet\Administrator\Form\Type\Medium;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\Type\Medium was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Terranet\Administrator\Form\Type\Tinymce;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\Type\Tinymce was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class Mutable extends BaseMutableCollection
18
{
19
    /**
20
     * Insert a new form element.
21
     *
22
     * @param $element
23
     * @param mixed string|Closure $inputType
24
     * @param mixed null|int|string $position
25
     *
26
     * @throws Exception
27
     *
28
     * @return $this
29
     */
30
    public function create($element, $inputType = null, $position = null)
31
    {
32
        if (!(is_string($element) || $element instanceof FormElement)) {
33
            throw new Exception('$element must be string or FormElement instance.');
34
        }
35
36
        // Create new element from string declaration ("title").
37
        if (is_string($element)) {
38
            $element = (new FormElement($element));
39
        }
40
41
        // Create Form Input Element from string declaration ("textarea")
42
        if (is_string($inputType)) {
43
            $oldInput = $element->getInput();
44
            $newInput = InputFactory::make($element->id(), $inputType);
45
46
            $newInput->setRelation(
47
                $oldInput->getRelation()
0 ignored issues
show
Bug introduced by
The method getRelation() does not exist on Terranet\Administrator\Contracts\Form\Element. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Form\Element. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                $oldInput->/** @scrutinizer ignore-call */ 
48
                           getRelation()
Loading history...
48
            )->setTranslatable(
49
                $oldInput->getTranslatable()
0 ignored issues
show
Bug introduced by
The method getTranslatable() does not exist on Terranet\Administrator\Contracts\Form\Element. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Form\Element. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                $oldInput->/** @scrutinizer ignore-call */ 
50
                           getTranslatable()
Loading history...
50
            );
51
52
            $element->setInput(
53
                $newInput
54
            );
55
        }
56
57
        // Allow a callable input type.
58
        if (is_callable($inputType)) {
59
            call_user_func_array($inputType, [$element]);
60
        }
61
62
        if (is_numeric($position)) {
63
            return $this->insert($element, $position);
64
        }
65
66
        // Push element
67
        $this->push($element);
68
69
        if (null !== $position) {
70
            return $this->move($element->id(), $position);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * Create a section.
78
     *
79
     * @param $section
80
     * @param null $position
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $position is correct as it would always require null to be passed?
Loading history...
81
     *
82
     * @return $this
83
     */
84
    public function section($section, $position = null)
85
    {
86
        if (is_string($section)) {
87
            $section = new FormSection($section);
88
        }
89
90
        return null !== $position ? $this->insert($section, $position) : $this->push($section);
91
    }
92
93
    /**
94
     * Whether the collection has active editor of specific type.
95
     *
96
     * @param $editor
97
     * @return bool
98
     * @throws Exception
99
     */
100
    public function hasEditors($editor)
101
    {
102
        $this->validateEditor($editor);
103
104
        return (bool) $this->filter(function ($field) use ($editor) {
105
            return $field instanceof Textarea && $field->editorEnabled($editor);
106
        })->count();
107
    }
108
109
    /**
110
     * Set rich editors.
111
     *
112
     * @param mixed string|array $fields
113
     */
114
    public function editors($fields, string $editor = null)
115
    {
116
        if (is_array($fields)) {
117
            foreach ($fields as $field => $editor) {
118
                $this->editors($field, $editor);
119
            }
120
        } elseif (is_string($fields) && $editor) {
121
            $item = $this->find($fields);
122
            if ($item instanceof Textarea) {
123
                if (method_exists($item, $editor)) {
124
                    $item->$editor();
125
                }
126
            }
127
        }
128
129
        return $this;
130
    }
131
132
    /**
133
     * Set fields descriptions.
134
     *
135
     * @param mixed string|array $fields
136
     */
137
    public function hints($fields, string $hint = null)
138
    {
139
        if (is_array($fields)) {
140
            foreach ($fields as $field => $hint) {
141
                $this->hints($field, $hint);
142
            }
143
        } elseif (is_string($fields) && $hint) {
144
            $item = $this->find($fields);
145
            $item->setDescription($hint);
146
        }
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param $editor
153
     *
154
     * @throws Exception
155
     */
156
    protected function validateEditor($editor)
157
    {
158
        if (!in_array($editor, ['ckeditor', 'tinymce', 'medium', 'markdown'], true)) {
159
            throw new Exception(sprintf('Unknown editor %s', $editor));
160
        }
161
    }
162
163
    /**
164
     * Create element object from string.
165
     *
166
     * @param $element
167
     *
168
     * @return mixed
169
     */
170
    protected function createElement($element)
171
    {
172
        if (is_string($element)) {
173
            $element = new FormElement($element);
174
        }
175
176
        return $element;
177
    }
178
179
    /**
180
     * @param $collection
181
     *
182
     * @return FormElement|\Terranet\Administrator\Columns\MediaElement
183
     */
184
    protected function createMediaElement($collection): MediaElement
185
    {
186
        return FormElement::media($collection);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administ...ent::media($collection) returns the type Terranet\Administrator\Form\FormElement which is incompatible with the type-hinted return Terranet\Administrator\Columns\MediaElement.
Loading history...
187
    }
188
}
189