FieldFactory::getFieldTypeClass()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php namespace Mascame\Artificer\Fields;
2
3
use Mascame\Artificer\Model\Model;
4
use Mascame\Artificer\Options\AdminOption;
5
use Mascame\Artificer\Options\FieldOption;
6
use \Illuminate\Support\Str as Str;
7
8
class FieldFactory
9
{
10
11
    public $fieldClass;
12
    public $fields;
13
    public $related_fields = null;
14
    public $custom_fields = null;
15
16
    /**
17
     * @var FieldParser
18
     */
19
    public $parser;
20
21
    /**
22
     * @var Model
23
     */
24
    public $modelObject;
25
    public $data;
26
27
    public $namespace = '\Mascame\Artificer\Fields\Types\\';
28
29
    public $classMap = array();
30
31
    /**
32
     * @param Model $model
33
     */
34
    public function __construct(Model $model)
35
    {
36
        $this->classMap = AdminOption::get('classmap');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Mascame\Artificer\Optio...Option::get('classmap') of type * is incompatible with the declared type array of property $classMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->modelObject = $model;
38
        $this->parser = new FieldParser(AdminOption::get('fields.types'));
39
    }
40
41
    /**
42
     * @param $type
43
     * @param $field
44
     * @param $value
45
     * @return mixed
46
     * @throws \Exception
47
     */
48
    public function make($type, $field, $value)
49
    {
50
        $fieldClass = $this->getFieldTypeClass($type);
51
52
        return new $fieldClass($field, $value, $this->isRelation($field));
53
    }
54
55
    /**
56
     * @param $type
57
     * @throws \Exception
58
     */
59
    public function getFieldTypeClass($type)
60
    {
61
        if (isset($this->classMap[$type])) {
62
            return $this->classMap[$type];
63
        }
64
65
        if (class_exists($this->namespace . Str::studly($type))) {
66
            return $this->namespace . Str::studly($type);
67
        }
68
69
        throw new \Exception("No supported Field type [{$type}]");
70
    }
71
72
    /**
73
     * @param $data
74
     * @return mixed
75
     */
76
    public function makeFields($data)
77
    {
78
        $this->data = $data;
79
80
        $this->withCustomFields();
81
82
        foreach ($this->withRelated() as $field) {
83
84
            $fieldType = $this->getTypeFromConfig($field);
85
86
            if (!$fieldType) $fieldType = $this->parser->parse($field);
87
88
            $this->fields[$field] = $this->make(
89
                $fieldType,
90
                $field,
91
                $this->fieldValue($field)
92
            );
93
        }
94
95
        return $this->fields;
96
    }
97
98
    /**
99
     * @param $name
100
     * @return bool|mixed
101
     */
102
    protected function getTypeFromConfig($name) {
103
        if (FieldOption::has('type', $name) || FieldOption::has('relationship.type', $name)) {
104
            return (FieldOption::has('type', $name)) ? FieldOption::get('type',
105
                $name) : FieldOption::get('relationship.type', $name);
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * @param $name
113
     * @return bool
114
     */
115
    protected function isRelation($name)
116
    {
117
        return in_array($name, $this->related_fields);
118
    }
119
120
    /**
121
     * @return array|null
122
     */
123
    public function getRelated()
124
    {
125
        if ($this->related_fields != null) {
126
            return $this->related_fields;
127
        }
128
129
        if (null == $fields = FieldOption::all()) {
130
            return $this->related_fields = array();
131
        }
132
133
        /*
134
         * We compare columns with config array to determine if there are new fields
135
         */
136
        $this->related_fields = array_diff(array_keys($fields), $this->modelObject->columns);
137
138
        return $this->related_fields;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    protected function addRelated()
145
    {
146
        $related = $this->getRelated();
147
148
        if (!empty($related)) {
149
            foreach ($related as $field) {
150
                $this->modelObject->columns[] = $field;
151
            }
152
        }
153
154
        return $this->modelObject->columns;
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    protected function withRelated()
161
    {
162
        return $this->addRelated();
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    protected function addCustomFields()
169
    {
170
        if (isset($this->modelObject->options['fields'])) {
171
            foreach ($this->modelObject->options['fields'] as $name => $data) {
172
                if (!in_array($name, $this->modelObject->columns)) {
173
                    $this->modelObject->columns[] = $name;
174
                }
175
            }
176
        }
177
178
        return $this->modelObject->columns;
179
    }
180
181
    /**
182
     * @return array
183
     */
184
    protected function withCustomFields()
185
    {
186
        return $this->addCustomFields();
187
    }
188
189
    /**
190
     * @param $field
191
     * @return null
192
     */
193
    public function fieldValue($field)
194
    {
195
        return (isset($this->data->$field)) ? $this->data->$field : null;
196
    }
197
198
}