Generator   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 25.67%

Importance

Changes 0
Metric Value
dl 0
loc 149
ccs 19
cts 74
cp 0.2567
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setModelFields() 0 4 1
A addModelFields() 0 4 1
A setModelRelations() 0 4 1
A setRelatedModelFields() 0 14 4
A getForm() 0 21 2
A getValidationRules() 0 16 4
A addFormFields() 0 17 3
A addValidationRules() 0 16 3
1
<?php
2
3
namespace Anavel\Crud\Http\Form;
4
5
use Anavel\Crud\Contracts\Form\Generator as GeneratorContract;
6
use Doctrine\DBAL\Types\Type as DbalType;
7
use FormManager\FactoryInterface;
8
use Illuminate\Support\Collection;
9
10
class Generator implements GeneratorContract
11
{
12
    protected $factory;
13
14
    /**
15
     * @var array
16
     */
17
    protected $fields;
18
19
    /**
20
     * @var Collection
21
     */
22
    protected $relations;
23
24
    /**
25
     * @var array
26
     */
27
    protected $databaseTypeToFormType = [
28
        DbalType::INTEGER  => 'number',
29
        DbalType::STRING   => 'text',
30
        DbalType::TEXT     => 'textarea',
31
        DbalType::BOOLEAN  => 'checkbox',
32
        DbalType::DATE     => 'date',
33
        DbalType::TIME     => 'time',
34
        DbalType::DATETIME => 'datetime',
35
        DbalType::DECIMAL  => 'number',
36
        DbalType::FLOAT    => 'number',
37
        'email',
38
        'password',
39
        'hidden',
40
        'select',
41
    ];
42
43 3
    public function __construct(FactoryInterface $factory)
44
    {
45 3
        $this->factory = $factory;
46 3
        $this->fields = [];
47 3
    }
48
49
    public function setModelFields(array $fields)
50
    {
51
        $this->fields = $fields;
52
    }
53
54 1
    public function addModelFields(array $fields)
55
    {
56 1
        $this->fields = array_merge_recursive($this->fields, $fields);
57 1
    }
58
59 1
    public function setModelRelations(Collection $relations)
60
    {
61 1
        $this->relations = $relations;
62 1
    }
63
64 1
    public function setRelatedModelFields(Collection $relations)
65
    {
66 1
        $this->setModelRelations($relations);
67
68 1
        if ($this->relations->count() > 0) {
69 1
            foreach ($this->relations as $relation) {
70 1
                if ($relation instanceof Collection) {
71
                    $this->addModelFields($relation->get('relation')->getEditFields());
72
                } else {
73 1
                    $this->addModelFields($relation->getEditFields());
74
                }
75 1
            }
76 1
        }
77 1
    }
78
79
    public function getForm($action)
80
    {
81
        $form = $this->factory->get('form', []);
82
83
        $form->attr([
84
            'action'  => $action,
85
            'enctype' => 'multipart/form-data',
86
            'method'  => 'post',
87
            'class'   => 'form-horizontal',
88
        ]);
89
90
        $formFields = [];
91
        foreach ($this->fields as $fieldGroupName => $fieldGroup) {
92
            $tempFields = $this->addFormFields($fieldGroup, $fieldGroupName);
93
            $formFields[key($tempFields)] = $this->factory->get('group', [$tempFields[key($tempFields)]]);
94
        }
95
96
        $form->add($formFields);
97
98
        return $form;
99
    }
100
101
    public function getValidationRules()
102
    {
103
        $rules = [];
104
105
        foreach ($this->fields as $fieldGroupName => $fieldGroup) {
106
            foreach ($fieldGroup as $fieldKey => $field) {
107
                if (is_array($field)) {
108
                    $rules[$fieldGroupName] = $this->addValidationRules($field, $fieldKey);
109
                } else {
110
                    $rules[$fieldGroupName][$field->getName()] = $field->getValidationRules();
111
                }
112
            }
113
        }
114
115
        return $rules;
116
    }
117
118
    /**
119
     * @param array $fields
120
     * @param $key
121
     *
122
     * @return array
123
     */
124
    protected function addFormFields(array $fields, $key)
125
    {
126
        $formFields = [];
127
        $tempFields = [];
128
129
        foreach ($fields as $fieldKey => $field) {
130
            if (is_array($field)) {
131
                $group = $this->addFormFields($field, $fieldKey);
132
                $tempFields[key($group)] = $this->factory->get('group', [$group[key($group)]]);
133
            } else {
134
                $tempFields[$field->getName()] = $field->getFormField();
135
            }
136
        }
137
        $formFields[$key] = $tempFields;
138
139
        return $formFields;
140
    }
141
142
    protected function addValidationRules(array $fields, $key)
143
    {
144
        $rules = [];
145
        $tempFields = [];
146
        foreach ($fields as $fieldKey => $field) {
147
            if (is_array($field)) {
148
                $group = $this->addValidationRules($field, $fieldKey);
149
                $tempFields[key($group)] = $group[key($group)];
150
            } else {
151
                $tempFields[$field->getName()] = $field->getValidationRules();
152
            }
153
        }
154
        $rules[$key] = $tempFields;
155
156
        return $rules;
157
    }
158
}
159