Completed
Push — master ( 976295...cf57a5 )
by Kristijan
03:22
created

ChildFormType   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.67%

Importance

Changes 0
Metric Value
dl 0
loc 212
ccs 71
cts 75
cp 0.9467
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 5

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplate() 0 4 1
A getForm() 0 4 1
A getDefaults() 0 10 1
A getAllAttributes() 0 5 1
A alterFieldValues() 0 4 1
A alterValid() 0 4 1
A createChildren() 0 16 4
A removeChild() 0 9 2
A getRenderData() 0 5 1
A __call() 0 10 2
A isValidValue() 0 4 1
C getClassFromOptions() 0 68 14
1
<?php
2
3
namespace Kris\LaravelFormBuilder\Fields;
4
5
use Kris\LaravelFormBuilder\Form;
6
7
class ChildFormType extends ParentType
8
{
9
10
    /**
11
     * @var Form
12
     */
13
    protected $form;
14
15
    /**
16
     * @inheritdoc
17
     */
18 21
    protected function getTemplate()
19
    {
20 21
        return 'child_form';
21
    }
22
23
    /**
24
     * @return Form
25
     */
26 2
    public function getForm()
27
    {
28 2
        return $this->form;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 21
    protected function getDefaults()
35
    {
36
        return [
37 21
            'class' => null,
38
            'value' => null,
39
            'formOptions' => [],
40
            'data' => [],
41
            'exclude' => []
42
        ];
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 2
    public function getAllAttributes()
49
    {
50
        // Collect all children's attributes.
51 2
        return $this->parent->getFormHelper()->mergeAttributes($this->children);
52
    }
53
54
    /**
55
     * Allow form-specific value alters.
56
     *
57
     * @param  array $values
58
     * @return void
59
     */
60 2
    public function alterFieldValues(array &$values)
61
    {
62 2
        $this->parent->getFormHelper()->alterFieldValues($this->form, $values);
63 2
    }
64
65
    /**
66
     * Allow form-specific valid alters.
67
     *
68
     * @param  Form  $mainForm
69
     * @param  bool  $isValid
70
     * @return void
71
     */
72 2
    public function alterValid(Form $mainForm, &$isValid)
73
    {
74 2
        $this->parent->getFormHelper()->alterValid($this->form, $mainForm, $isValid);
75 2
    }
76
77
    /**
78
     * @return mixed|void
79
     */
80 21
    protected function createChildren()
81
    {
82 21
        $this->form = $this->getClassFromOptions();
83
84 18
        if ($this->form->getFormOption('files')) {
85 1
            $this->parent->setFormOption('files', true);
86
        }
87 18
        $model = $this->getOption($this->valueProperty);
88 18
        if ($this->isValidValue($model)) {
89 5
            foreach ($this->form->getFields() as $name => $field) {
90 4
                $field->setValue($this->getModelValueAttribute($model, $name));
91
            }
92
        }
93
94 18
        $this->children = $this->form->getFields();
95 18
    }
96
97
    /**
98
     * @return Form
99
     * @throws \Exception
100
     */
101 21
    protected function getClassFromOptions()
102
    {
103 21
        if ($this->form instanceof Form) {
104
            return $this->form->setName($this->name);
105
        }
106
107 21
        $class = $this->getOption('class');
108
109 21
        if (!$class) {
110 1
            throw new \InvalidArgumentException(
111 1
                'Please provide full name or instance of Form class.'
112
            );
113
        }
114
115 20
        if (is_string($class)) {
116
            $options = [
117 9
                'model' => $this->getOption($this->valueProperty) ?: $this->parent->getModel(),
118 9
                'name' => $this->name,
119 9
                'language_name' => $this->parent->getLanguageName(),
120 9
                'translation_template' => $this->parent->getTranslationTemplate(),
121
            ];
122
123 9
            if (!$this->parent->clientValidationEnabled()) {
124 1
                $options['client_validation'] = false;
125
            }
126
127 9
            if (!$this->parent->haveErrorsEnabled()) {
128 1
                $options['errors_enabled'] = false;
129
            }
130
131 9
            $formOptions = array_merge($options, $this->getOption('formOptions'));
132
133 9
            $data = array_merge($this->parent->getData(), $this->getOption('data'));
134
135 9
            return $this->parent->getFormBuilder()->create($class, $formOptions, $data);
136
        }
137
138 11
        if ($class instanceof Form) {
139 10
            $class->setName($this->name, false);
140 10
            $class->setModel($class->getModel() ?: $this->parent->getModel());
0 ignored issues
show
Deprecated Code introduced by
The method Kris\LaravelFormBuilder\Form::setModel() has been deprecated with message: deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
141
142 10
            if (!$class->getData()) {
143 10
                $class->addData($this->parent->getData());
0 ignored issues
show
Deprecated Code introduced by
The method Kris\LaravelFormBuilder\Form::addData() has been deprecated with message: deprecated since 1.6.12, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data
will be switched to protected in 1.7.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
144
            }
145
146 10
            if (!$class->getLanguageName()) {
147 9
                $class->setLanguageName($this->parent->getLanguageName());
148
            }
149
150 10
            if (!$class->getTranslationTemplate()) {
151 9
                $class->setTranslationTemplate($this->parent->getTranslationTemplate());
152
            }
153
154 10
            if (!$this->parent->clientValidationEnabled()) {
155
                $class->setClientValidationEnabled(false);
156
            }
157
158 10
            if (!$this->parent->haveErrorsEnabled()) {
159
                $class->setErrorsEnabled(false);
160
            }
161
162 10
            return $class->setName($this->name);
163
        }
164
165 1
        throw new \InvalidArgumentException(
166 1
            'Class provided does not exist or it passed in wrong format.'
167
        );
168
    }
169
170
    /**
171
     * @inheritdoc
172
     */
173 1
    public function removeChild($key)
174
    {
175 1
        if ($this->getChild($key)) {
176 1
            $this->form->remove($key);
177 1
            return parent::removeChild($key);
178
        }
179
180
        return $this;
181
    }
182
183
    /**
184
     * @inheritdoc
185
     */
186 3
    protected function getRenderData() {
187 3
        $data = parent::getRenderData();
188 3
        $data['child_form'] = $this->form;
189 3
        return $data;
190
    }
191
192
    /**
193
     * @param $method
194
     * @param $arguments
195
     *
196
     * @return Form|null
197
     */
198 5
    public function __call($method, $arguments)
199
    {
200 5
        if (method_exists($this->form, $method)) {
201 5
            return call_user_func_array([$this->form, $method], $arguments);
202
        }
203
204 1
        throw new \BadMethodCallException(
205 1
            'Method ['.$method.'] does not exist on form ['.get_class($this->form).']'
206
        );
207
    }
208
209
    /**
210
     * Check if provided value is valid for this type.
211
     *
212
     * @return bool
213
     */
214 21
    protected function isValidValue($value)
215
    {
216 21
        return $value !== null;
217
    }
218
}
219