Completed
Push — master ( 0f9330...0a7784 )
by
unknown
06:04
created

FieldFactory::get()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 28
cp 0
rs 4.909
cc 9
eloc 21
nc 72
nop 0
crap 90
1
<?php
2
namespace Anavel\Crud\Abstractor\Eloquent;
3
4
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldAbstractorFactoryContract;
5
use Anavel\Crud\Abstractor\Exceptions\FactoryException;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Types\Type as DbalType;
8
use FormManager\FactoryInterface as FormManagerFactory;
9
10
class FieldFactory implements FieldAbstractorFactoryContract
11
{
12
    /**
13
     * @var FormManagerFactory
14
     */
15
    protected $factory;
16
17
    /**
18
     * @var Column
19
     */
20
    protected $column;
21
22
    /**
23
     * @var array
24
     */
25
    protected $config;
26
27
    /**
28
     * @var array
29
     */
30
    protected $databaseTypeToFormType = array(
31
        DbalType::INTEGER  => 'number',
32
        DbalType::SMALLINT => 'number',
33
        DbalType::BIGINT   => 'number',
34
        DbalType::STRING   => 'text',
35
        DbalType::TEXT     => 'textarea',
36
        DbalType::BOOLEAN  => 'checkbox',
37
        DbalType::DATE     => 'date',
38
        DbalType::TIME     => 'time',
39
        DbalType::DATETIME => 'datetime',
40
        DbalType::DECIMAL  => 'number',
41
        DbalType::FLOAT    => 'number',
42
        'email',
43
        'password',
44
        'hidden',
45
        'select',
46
        'file'
47
    );
48
49
    public function __construct(FormManagerFactory $factory)
50
    {
51
        $this->factory = $factory;
52
    }
53
54
    /**
55
     *
56
     */
57
    public function setColumn(Column $column)
58
    {
59
        $this->column = $column;
60
61
        return $this;
62
    }
63
64
    /**
65
     *
66
     */
67
    public function setConfig(array $config)
68
    {
69
        $this->config = $config;
70
71
        return $this;
72
    }
73
74
    /**
75
     *
76
     */
77
    public function get()
78
    {
79
        $formElement = $this->getFormElement();
80
81
        $field = new Field($this->column, $formElement, $this->config['name'], $this->config['presentation']);
82
83
        if (! empty($this->config['validation'])) {
84
            if ($this->config['validation'] === 'no_validate') {
85
                $this->config['no_validate'] = true;
86
            } else {
87
                $field->setValidationRules($this->config['validation']);
88
            }
89
        }
90
91
        if (! empty($this->config['functions'])) {
92
            $field->setFunctions($this->config['functions']);
93
        }
94
95
        if (! empty($this->config['no_validate']) && $this->config['no_validate'] === true) {
96
            $field->noValidate(true);
97
        }
98
99
        if (get_class($formElement) === \FormManager\Fields\Password::class) {
100
            $field->hideValue(true);
101
            $field->saveIfEmpty(false);
102
            $field->noValidate(true);
103
        }
104
105
        if (count($rules = $field->getValidationRulesArray()) > 0) {
106
            if (in_array('required', $rules)) {
107
                $field->setFormElementAttributes(
108
                    [
109
                        'required' => true
110
                    ]
111
                );
112
            }
113
        }
114
115
        return $field;
116
    }
117
118
    protected function getFormElement()
119
    {
120
        if (! empty($this->config['form_type'])) {
121
            if (! in_array($this->config['form_type'], $this->databaseTypeToFormType)) {
122
                throw new FactoryException("Unknown form type " . $this->config['form_type']);
123
            }
124
125
            $formElementType = $this->config['form_type'];
126
        } else {
127
            if (! array_key_exists($this->column->getType()->getName(), $this->databaseTypeToFormType)) {
128
                throw new FactoryException("No form type found for database type " . $this->column->getType()->getName());
129
            }
130
131
            $formElementType = $this->databaseTypeToFormType[$this->column->getType()->getName()];
132
        }
133
134
        if (isset($this->config['defaults']) && is_array($this->config['defaults'])) {
135
            $formElementType = 'select';
136
        }
137
138
139
        $formElement = $this->factory->get($formElementType, []);
140
141
        if (! empty($this->config['attr']) && is_array($this->config['attr'])) {
142
            $formElement->attr($this->config['attr']);
143
        }
144
145
        if ($formElementType !== 'hidden') {
146
            $formElement->class('form-control')
147
                ->label($this->getPresentation())
148
                ->placeholder($this->getPresentation());
149
        }
150
151
        if ($formElementType === 'textarea') {
152
            $formElement->class('form-control ' . config('anavel-crud.text_editor'));
153
        }
154
155
        if (isset($this->config['defaults'])) {
156
            if (! is_array($this->config['defaults'])) {
157
                $formElement->val(transcrud($this->config['defaults']));
158
            } else {
159
                $defaults = [];
160
                foreach ($this->config['defaults'] as $key => $default) {
161
                    $defaults[$key] = transcrud($default);
162
                }
163
                $formElement->options($defaults);
164
            }
165
        }
166
167
        return $formElement;
168
    }
169
170
    public function getPresentation()
171
    {
172
        return transcrud($this->config['presentation']) ? : ucfirst(str_replace('_', ' ',
173
            transcrud($this->config['name'])));
174
    }
175
}
176