Completed
Push — dev ( 726aeb...0f045d )
by Marc
02:06
created

FieldFactory::completeRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php namespace Mascame\Artificer\Fields;
2
3
use Mascame\Artificer\Artificer;
4
use Mascame\Artificer\Fields\Types\Relations\Relation;
5
use Mascame\Artificer\Model\Model;
6
use \Illuminate\Support\Str as Str;
7
use Mascame\Artificer\Fields\FieldWrapper;
8
use Mascame\Formality\Field\Field;
9
10
class FieldFactory extends \Mascame\Formality\Factory\Factory
11
{
12
13
    public $fieldClass;
14
    public $fields;
15
    public $relatedFields = null;
16
    public $custom_fields = null;
17
18
    /**
19
     * @var Model
20
     */
21
    public $modelObject;
22
    public $data;
23
24
    public $namespace = '\Mascame\Artificer\Fields\Types\\';
25
26
    /**
27
     * @param $data
28
     * @return mixed
29
     */
30
    public function makeFields()
31
    {
32
        $fields = parent::makeFields();
33
34
        foreach($fields as $key => $field) {
35
            /**
36
             * @var $field Field
37
             */
38
            $field->setOptions([
39
                'attributes' => [
40
                    'class' => 'form-control'
41
                ]
42
            ]);
43
44
            if (is_a($field, \Mascame\Artificer\Fields\Types\Relations\Relation::class)) {
45
                $field = $this->completeRelation($field);
46
            }
47
48
            $fields[$key] = new FieldWrapper($field);
49
        }
50
51
        return $fields;
52
    }
53
54
    /**
55
     * @param $field Relation
56
     * @return mixed
57
     */
58
    public function completeRelation($field) {
59
        $relationship = $field->getOption('relationship', []);
60
61
        $completedRelation = [
62
            "method" => $field->guessRelatedMethod(),
63
            "type" => $field->getType(),
64
            "model" => $field->guessModel(),
65
            "show" => function($value) {
66
                // Jump to next column avoiding 'id'
67
                return array_values(array_slice($value->toArray(), 1, 1))[0];
68
            }
69
        ];
70
71
        // user config takes preference
72
        $field->setOptions(['relationship' => array_merge($completedRelation, $relationship)]);
73
74
        return $field;
75
    }
76
77
    /**
78
     * @param $name
79
     * @return bool|mixed
80
     */
81
//    protected function getTypeFromConfig($name) {
82
//        if (FieldOption::has('type', $name) || FieldOption::has('relationship.type', $name)) {
83
//            return (FieldOption::has('type', $name)) ?
84
//                FieldOption::get('type', $name) :
85
//                FieldOption::get('relationship.type', $name);
86
//        }
87
//
88
//        return false;
89
//    }
90
91
    /**
92
     * @param $name
93
     * @return bool
94
     */
95
    protected function isRelation($name)
96
    {
97
        return Str::contains($this->fieldClass, '\\Relations\\') || in_array($name, $this->relatedFields);
98
    }
99
100
    /**
101
     * @return array|null
102
     */
103
    public function getRelated()
104
    {
105
        if ($this->relatedFields) return $this->relatedFields;
106
107
        if (null == $fields = FieldOption::all()) {
108
            return $this->relatedFields = [];
109
        }
110
111
        /*
112
         * We compare columns with config array to determine if there are new fields
113
         */
114
        $this->relatedFields = array_diff(array_keys($fields), $this->modelObject->columns);
115
116
        return $this->relatedFields;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    protected function withRelated()
123
    {
124
        $related = $this->getRelated();
125
126
        if ( ! empty($related)) {
127
            foreach ($related as $field) {
128
                $this->modelObject->columns[] = $field;
129
            }
130
        }
131
132
        return $this->modelObject->columns;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
//    protected function withCustomFields()
139
//    {
140
//        if (isset($this->modelObject->options['fields'])) {
141
//
142
//            foreach ($this->modelObject->options['fields'] as $name => $data) {
143
//                if ( ! in_array($name, $this->modelObject->columns)) {
144
//                    $this->modelObject->columns[] = $name;
145
//                }
146
//            }
147
//
148
//        }
149
//
150
//        return $this->modelObject->columns;
151
//    }
152
153
    /**
154
     * @param $field
155
     * @return null
156
     */
157
    public function fieldValue($field)
158
    {
159
        return (isset($this->data->$field)) ? $this->data->$field : null;
160
    }
161
162
}