Completed
Push — dev ( 17f62c )
by Marc
09:19
created

FieldFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php namespace Mascame\Artificer\Fields;
2
3
use Mascame\Artificer\Model\Model;
4
use Mascame\Artificer\Options\FieldOption;
5
use \Illuminate\Support\Str as Str;
6
7
class FieldFactory
8
{
9
10
    public $fieldClass;
11
    public $fields;
12
    public $relatedFields = null;
13
    public $custom_fields = null;
14
15
    /**
16
     * @var FieldParser
17
     */
18
    public $parser;
19
20
    /**
21
     * @var Model
22
     */
23
    public $modelObject;
24
    public $data;
25
26
    public $namespace = '\Mascame\Artificer\Fields\Types\\';
27
28
    public $classMap = array();
29
30
    /**
31
     * @param $type
32
     * @param $field
33
     * @param $value
34
     * @return mixed
35
     * @throws \Exception
36
     */
37
    public function make($type, $field, $value)
38
    {
39
        $fieldClass = $this->fieldClass = $this->getFieldTypeClass($type);
0 ignored issues
show
Bug introduced by
The method getFieldTypeClass() does not seem to exist on object<Mascame\Artificer\Fields\FieldFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
41
        return new $fieldClass($field, $value, $this->isRelation($field));
42
    }
43
44
    /**
45
     * @param $data
46
     * @return mixed
47
     */
48
    public function makeFields($data)
49
    {
50
        $this->data = $data;
51
52
        $this->withCustomFields();
53
54
        foreach ($this->withRelated() as $field) {
55
56
            $type = $this->getTypeFromConfig($field);
57
58
            if ( ! $type) $type = $this->parser->parse($field);
59
60
            $this->fields[$field] = $this->make(
61
                $type,
62
                $field,
63
                $this->fieldValue($field)
64
            );
65
        }
66
67
        return $this->fields;
68
    }
69
70
    /**
71
     * @param $name
72
     * @return bool|mixed
73
     */
74
    protected function getTypeFromConfig($name) {
75
        if (FieldOption::has('type', $name) || FieldOption::has('relationship.type', $name)) {
76
            return (FieldOption::has('type', $name)) ?
77
                FieldOption::get('type', $name) :
78
                FieldOption::get('relationship.type', $name);
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * @param $name
86
     * @return bool
87
     */
88
    protected function isRelation($name)
89
    {
90
        return Str::contains($this->fieldClass, '\\Relations\\') || in_array($name, $this->relatedFields);
91
    }
92
93
    /**
94
     * @return array|null
95
     */
96
    public function getRelated()
97
    {
98
        if ($this->relatedFields) return $this->relatedFields;
99
100
        if (null == $fields = FieldOption::all()) {
101
            return $this->relatedFields = [];
102
        }
103
104
        /*
105
         * We compare columns with config array to determine if there are new fields
106
         */
107
        $this->relatedFields = array_diff(array_keys($fields), $this->modelObject->columns);
108
109
        return $this->relatedFields;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    protected function withRelated()
116
    {
117
        $related = $this->getRelated();
118
119
        if ( ! empty($related)) {
120
            foreach ($related as $field) {
121
                $this->modelObject->columns[] = $field;
122
            }
123
        }
124
125
        return $this->modelObject->columns;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    protected function withCustomFields()
132
    {
133
        if (isset($this->modelObject->options['fields'])) {
134
135
            foreach ($this->modelObject->options['fields'] as $name => $data) {
136
                if ( ! in_array($name, $this->modelObject->columns)) {
137
                    $this->modelObject->columns[] = $name;
138
                }
139
            }
140
141
        }
142
143
        return $this->modelObject->columns;
144
    }
145
146
    /**
147
     * @param $field
148
     * @return null
149
     */
150
    public function fieldValue($field)
151
    {
152
        return (isset($this->data->$field)) ? $this->data->$field : null;
153
    }
154
155
}