FieldFactory::get()   D
last analyzed

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