Completed
Push — dev ( 17f62c...81bde7 )
by Marc
10:28
created

FieldFactory::makeFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
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
use Mascame\Artificer\Fields\Field as FieldWrapper;
7
use Mascame\Formality\Field\Field;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Mascame\Artificer\Fields\Field.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
class FieldFactory extends \Mascame\Formality\Factory\Factory
10
{
11
12
    public $fieldClass;
13
    public $fields;
14
    public $relatedFields = null;
15
    public $custom_fields = null;
16
17
    /**
18
     * @var Model
19
     */
20
    public $modelObject;
21
    public $data;
22
23
    public $namespace = '\Mascame\Artificer\Fields\Types\\';
24
25
    /**
26
     * @param $data
27
     * @return mixed
28
     */
29
    public function makeFields()
30
    {
31
        $fields = parent::makeFields();
32
33
        foreach($fields as $key => $field) {
34
            /**
35
             * @var $field Field
36
             */
37
            $field->setOptions([
38
                'attributes' => [
39
                    'class' => 'form-control'
40
                ]
41
            ]);
42
43
            $fields[$key] = new FieldWrapper($field);
44
        }
45
46
        return $fields;
47
    }
48
49
    /**
50
     * @param $name
51
     * @return bool|mixed
52
     */
53
    protected function getTypeFromConfig($name) {
54
        if (FieldOption::has('type', $name) || FieldOption::has('relationship.type', $name)) {
55
            return (FieldOption::has('type', $name)) ?
56
                FieldOption::get('type', $name) :
57
                FieldOption::get('relationship.type', $name);
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * @param $name
65
     * @return bool
66
     */
67
    protected function isRelation($name)
68
    {
69
        return Str::contains($this->fieldClass, '\\Relations\\') || in_array($name, $this->relatedFields);
70
    }
71
72
    /**
73
     * @return array|null
74
     */
75
    public function getRelated()
76
    {
77
        if ($this->relatedFields) return $this->relatedFields;
78
79
        if (null == $fields = FieldOption::all()) {
80
            return $this->relatedFields = [];
81
        }
82
83
        /*
84
         * We compare columns with config array to determine if there are new fields
85
         */
86
        $this->relatedFields = array_diff(array_keys($fields), $this->modelObject->columns);
87
88
        return $this->relatedFields;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    protected function withRelated()
95
    {
96
        $related = $this->getRelated();
97
98
        if ( ! empty($related)) {
99
            foreach ($related as $field) {
100
                $this->modelObject->columns[] = $field;
101
            }
102
        }
103
104
        return $this->modelObject->columns;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    protected function withCustomFields()
111
    {
112
        if (isset($this->modelObject->options['fields'])) {
113
114
            foreach ($this->modelObject->options['fields'] as $name => $data) {
115
                if ( ! in_array($name, $this->modelObject->columns)) {
116
                    $this->modelObject->columns[] = $name;
117
                }
118
            }
119
120
        }
121
122
        return $this->modelObject->columns;
123
    }
124
125
    /**
126
     * @param $field
127
     * @return null
128
     */
129
    public function fieldValue($field)
130
    {
131
        return (isset($this->data->$field)) ? $this->data->$field : null;
132
    }
133
134
}