Completed
Pull Request — master (#74)
by Adrian
11:16
created

Generator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 25.67%

Importance

Changes 0
Metric Value
dl 0
loc 151
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 addValidationRules() 0 16 3
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 23 2
A getValidationRules() 0 16 4
A addFormFields() 0 17 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
91
        $formFields = [];
92
        foreach ($this->fields as $fieldGroupName => $fieldGroup) {
93
            $tempFields = $this->addFormFields($fieldGroup, $fieldGroupName);
94
            $formFields[key($tempFields)] = $this->factory->get('group', [$tempFields[key($tempFields)]]);
95
        }
96
97
        $form->add($formFields);
98
99
100
        return $form;
101
    }
102
103
    public function getValidationRules()
104
    {
105
        $rules = [];
106
107
        foreach ($this->fields as $fieldGroupName => $fieldGroup) {
108
            foreach ($fieldGroup as $fieldKey => $field) {
109
                if (is_array($field)) {
110
                    $rules[$fieldGroupName] = $this->addValidationRules($field, $fieldKey);
111
                } else {
112
                    $rules[$fieldGroupName][$field->getName()] = $field->getValidationRules();
113
                }
114
            }
115
        }
116
117
        return $rules;
118
    }
119
120
    /**
121
     * @param array $fields
122
     * @param $key
123
     *
124
     * @return array
125
     */
126
    protected function addFormFields(array $fields, $key)
127
    {
128
        $formFields = [];
129
        $tempFields = [];
130
131
        foreach ($fields as $fieldKey => $field) {
132
            if (is_array($field)) {
133
                $group = $this->addFormFields($field, $fieldKey);
134
                $tempFields[key($group)] = $this->factory->get('group', [$group[key($group)]]);
135
            } else {
136
                $tempFields[$field->getName()] = $field->getFormField();
137
            }
138
        }
139
        $formFields[$key] = $tempFields;
140
141
        return $formFields;
142
    }
143
144
    protected function addValidationRules(array $fields, $key)
145
    {
146
        $rules = [];
147
        $tempFields = [];
148
        foreach ($fields as $fieldKey => $field) {
149
            if (is_array($field)) {
150
                $group = $this->addValidationRules($field, $fieldKey);
151
                $tempFields[key($group)] = $group[key($group)];
152
            } else {
153
                $tempFields[$field->getName()] = $field->getValidationRules();
154
            }
155
        }
156
        $rules[$key] = $tempFields;
157
158
        return $rules;
159
    }
160
}
161