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