Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

FormBuilder::contentRender()   D

Complexity

Conditions 10
Paths 20

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 4.8196
cc 10
eloc 18
nc 20
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** MicroFormBuilder */
2
3
namespace Micro\Form;
4
5
use Micro\Base\Exception;
6
use Micro\Web\Html;
7
use Micro\Widget\FormWidget;
8
9
/**
10
 * Class FormBuilder.
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/lugnsk/micro
14
 * @copyright Copyright &copy; 2013 Oleg Lunegov
15
 * @license /LICENSE
16
 * @package Micro
17
 * @subpackage Form
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class FormBuilder
22
{
23
    /** @var FormWidget $widget widget for render */
24
    protected $widget;
25
    /** @var Form $form generator for elements */
26
    protected $form;
27
    /** @var array $config config array */
28
    private $config;
29
    /** @var IFormModel $model model for get data */
30
    private $model;
31
32
33
    /**
34
     * Constructor object
35
     *
36
     * @access public
37
     *
38
     * @param array $config
39
     * @param IFormModel $model
40
     * @param string $method method of request
41
     * @param string $type type data
42
     * @param string $action path URL action
43
     * @param array $attr attributes for form
44
     *
45
     * @result void
46
     * @throws \Micro\Base\Exception
47
     */
48
    public function __construct(
49
        array $config = [],
50
        IFormModel $model = null,
51
        $method = 'GET',
52
        $type = 'text/plain',
53
        $action = '',
54
        array $attr = []
55
    ) {
56
        $this->config = $config;
57
        $this->model = $model;
58
        $this->widget = new FormWidget([
59
            'action' => $action,
60
            'method' => $method,
61
            'type' => $type,
62
            'client' => $model->getClient(),
0 ignored issues
show
Bug introduced by
It seems like $model is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
63
            'attributes' => $attr
64
        ]);
65
    }
66
67
    /**
68
     * Set model data
69
     *
70
     * Loading data in model from array
71
     *
72
     * @access public
73
     *
74
     * @param array $data array to change
75
     *
76
     * @return void
77
     */
78
    public function setModelData(array $data = [])
79
    {
80
        $this->model->setModelData($data);
81
    }
82
83
    /**
84
     * Validation model
85
     *
86
     * @access public
87
     *
88
     * @return bool
89
     * @throws \Micro\Base\Exception
90
     */
91
    public function validateModel()
92
    {
93
        return $this->model->validate();
94
    }
95
96
    /**
97
     * Getting model
98
     *
99
     * @access public
100
     * @return \Micro\Mvc\Models\Model
101
     */
102
    public function getModel()
103
    {
104
        return $this->model;
105
    }
106
107
    /**
108
     * Convert object to string
109
     *
110
     * @access public
111
     * @return string
112
     * @throws \Micro\Base\Exception
113
     */
114
    public function __toString()
115
    {
116
        return $this->render();
117
    }
118
119
    /**
120
     * Render form builder
121
     *
122
     * @access public
123
     * @return string
124
     */
125
    public function render()
126
    {
127
        ob_start();
128
129
        $this->beginRender();
130
        try {
131
            $this->contentRender();
132
        } catch (Exception $e) {
133
            //
134
        }
135
        $this->endRender();
136
137
        return ob_get_clean();
138
    }
139
140
    /**
141
     * Render form heading
142
     *
143
     * @access public
144
     * @return void
145
     */
146
    public function beginRender()
147
    {
148
        $this->form = $this->widget->init();
149
        if (!empty($this->config['legend'])) {
150
            echo Html::openTag('fieldset');
151
            echo Html::legend($this->config['legend']);
152
        }
153
        if (!empty($this->config['description'])) {
154
            echo Html::openTag('div',
155
                ['class' => 'description']), $this->config['description'], Html::closeTag('div');
156
        }
157
        if ($this->model) {
158
            $errors = $this->getModelErrors();
159
            if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
160
                echo Html::openTag('div', ['class' => 'errors']);
161
                foreach ($errors AS $error) {
162
                    echo Html::openTag('div', ['class' => 'error']), $error, Html::closeTag('div');
163
                }
164
                echo Html::closeTag('div');
165
            }
166
        }
167
    }
168
169
    /**
170
     * Get errors from model
171
     *
172
     * @access public
173
     * @return array
174
     */
175
    public function getModelErrors()
176
    {
177
        return $this->model->getErrors();
178
    }
179
180
    /**
181
     * Render form elements
182
     *
183
     * @access public
184
     *
185
     * @param null|array $conf configuration array
186
     *
187
     * @return void
188
     * @throws \Micro\Base\Exception
189
     */
190
    public function contentRender($conf = null)
191
    {
192
        if (!$conf) {
193
            $conf = $this->config;
194
        }
195
        foreach ($conf['elements'] AS $key => $value) {
196
            if (is_array($conf['elements'][$key])) {
197
                if ($value['type'] === 'form') {
198
                    $subForm = new FormBuilder($value, !empty($value['model']) ? $value['model'] : null);
199
                    echo $subForm;
200
                } elseif ($this->model) {
201
                    echo $this->form->$value['type']($this->model, $key,
202
                        !empty($value['options']) ? $value['options'] : []);
203
                } else {
204
                    echo Html::$value['type']($key, $value['value'], $value['options']);
205
                }
206
            } else {
207
                echo $conf['elements'][$key];
208
            }
209
        }
210
        foreach ($this->config['buttons'] AS $button) {
211
            $type = $button['type'] . 'Button';
212
            echo Html::$type($button['label'], !empty($button['options']) ? $button['options'] : []);
213
        }
214
    }
215
216
    /**
217
     * Finish form render
218
     *
219
     * @access public
220
     * @return void
221
     */
222
    public function endRender()
223
    {
224
        if (!empty($this->config['legend'])) {
225
            echo Html::closeTag('fieldset');
226
        }
227
        $this->widget->run();
228
    }
229
}
230