Completed
Push — master ( 90cc04...822bf4 )
by Maxime
03:27
created

FormView   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
C add() 0 31 13
A renderFormView() 0 4 1
A renderRestView() 0 6 1
A view() 0 15 1
1
<?php namespace Distilleries\FormBuilder;
2
3
use Kris\LaravelFormBuilder\Form;
4
5
class FormView extends Form
6
{
7
8
9
    // ------------------------------------------------------------------------------------------------
10
    // ------------------------------------------------------------------------------------------------
11
    // ------------------------------------------------------------------------------------------------
12 140
13
    public function add($name, $type = 'text', array $options = [], $modify = false, $noOveride = false)
14
{
15
16 140
17 140
    if (!isset($options['noInEditView'])) {
18 132
        $options['noInEditView'] = false;
19 132
    }
20
21
22 140
    if (!empty($options) && empty($options['default_value']) && !empty($options['value'])) {
23 140
        $options['default_value'] = $options['value'];
24 4
    }
25
26 4
27 4
    if (!empty($this->formOptions) && !empty($this->formOptions['do_not_display_' . $name]) && $this->formOptions['do_not_display_' . $name] === true) {
28 4
        $type = 'hidden';
29 4
30
        if (!empty($options) && !empty($options['selected'])) {
31 4
            $options['default_value'] = $options['selected'];
32
        }
33 140
34 140
    }
35 36
36 36
    if ($type == 'choice' && !isset($options['selected'])) {
37 4
        if (isset($this->model->{$name})) {
38 4
            $options['selected'] = $this->model->{$name};
39 36
        }
40
    }
41 140
42
    return parent::add($name, $type, $options, $modify);
43
}
44
45
    // ------------------------------------------------------------------------------------------------
46
    // ------------------------------------------------------------------------------------------------
47
    // ------------------------------------------------------------------------------------------------
48 76
49
    public function renderFormView(array $options = [])
50 76
    {
51
        return $this->view($options, $this->fields);
52
    }
53
54
    // ------------------------------------------------------------------------------------------------
55
56 4
57
    public function renderRestView()
58 4
    {
59
        $fields = $this->getUnrenderedFields();
60 4
61
        return $this->view([], $fields);
62
    }
63
64
    // ------------------------------------------------------------------------------------------------
65 80
66
    protected function view($options, $fields)
67 80
    {
68
        $formOptions = $this->formHelper->mergeOptions($this->formOptions, $options);
69 80
70 80
        return $this->formHelper->getView()
71 80
            ->make($this->formHelper->getConfig('form'))
72 80
            ->with(['showFields' => true])
73 80
            ->with(['showEnd' => false])
74 80
            ->with(['showStart' => false])
75 80
            ->with(['isNotEditable' => true])
76 80
            ->with('formOptions', $formOptions)
77 80
            ->with('fields', $fields)
78 80
            ->with('model', $this->getModel())
79
            ->render();
80
    }
81
}