1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Attributes; |
4
|
|
|
|
5
|
|
|
class AttributeManager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Create a new Attribute Instance |
9
|
|
|
* |
10
|
|
|
* @param string $type |
11
|
|
|
* @param string $action |
|
|
|
|
12
|
|
|
* @param string $attribute |
13
|
|
|
* @param string $field |
14
|
|
|
* @param string $model |
15
|
|
|
* @param string $modelManager |
16
|
|
|
*/ |
17
|
|
|
public function createAttribute($type, $attribute, $field, $model = null, $modelManager = null) |
18
|
|
|
{ |
19
|
|
|
if ($this->attributeTypeExists($type)) { |
20
|
|
|
$fieldType = $this->resolveAttributeClass($type); |
21
|
|
|
|
22
|
|
|
return new $fieldType($attribute, $field, $model, $modelManager); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return new BaseAttribute($attribute, $field, $model, $modelManager); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Render Attribute. |
30
|
|
|
* |
31
|
|
|
* @param string $action |
32
|
|
|
* @param string $attribute |
33
|
|
|
* @param string $field |
34
|
|
|
* @param string $model |
35
|
|
|
* @param string $modelManager |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Http\Response |
38
|
|
|
*/ |
39
|
|
|
public function renderAttribute($action, $attribute, $field, $model = null, $modelManager = null) |
40
|
|
|
{ |
41
|
|
|
if (!isset($field['type'])) { |
42
|
|
|
throw new \Exception('Attribute Field Type cannot be empty or undefined.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return call_user_func_array([$this->createAttribute($field['type'], $action, $attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Determines if an AttributeType class exists or not. |
50
|
|
|
* |
51
|
|
|
* @param string $type |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function attributeTypeExists($type) |
56
|
|
|
{ |
57
|
|
|
return $this->resolveAttributeClass($type) ? true : false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns an array of all of the Available Attribute Types. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
protected function availableAttributes() |
66
|
|
|
{ |
67
|
|
|
$availableAttributes = []; |
68
|
|
|
|
69
|
|
|
foreach (\Flare::config('attributes') as $attributeType => $attributeFullClassname) { |
70
|
|
|
$availableAttributes = array_add( |
71
|
|
|
$availableAttributes, |
72
|
|
|
$attributeType, |
73
|
|
|
$attributeFullClassname |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $availableAttributes; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Resolves the Class of an Attribute and returns it as a string. |
82
|
|
|
* |
83
|
|
|
* @param string $type |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function resolveAttributeClass($type) |
88
|
|
|
{ |
89
|
|
|
if (class_exists($type)) { |
90
|
|
|
return $type; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (array_key_exists($type, $attributes = $this->availableAttributes())) { |
94
|
|
|
return $this->availableAttributes()[$type]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.