1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Attributes; |
4
|
|
|
|
5
|
|
|
class AttributeManager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Render Attribute. |
9
|
|
|
* |
10
|
|
|
* @param string $action |
11
|
|
|
* @param string $attribute |
12
|
|
|
* @param string $field |
13
|
|
|
* @param string $model |
14
|
|
|
* @param string $modelManager |
15
|
|
|
* |
16
|
|
|
* @return \Illuminate\Http\Response |
17
|
|
|
*/ |
18
|
|
|
public function renderAttribute($action, $attribute, $field, $model, $modelManager) |
19
|
|
|
{ |
20
|
|
|
if (!isset($field['type'])) { |
21
|
|
|
throw new \Exception('Attribute Field Type cannot be empty or undefined.'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($this->attributeTypeExists($field['type'])) { |
25
|
|
|
$fieldType = $this->resolveAttributeClass($field['type']); |
26
|
|
|
|
27
|
|
|
return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns an array of all of the Available Attribute Types. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
protected function availableAttributes() |
39
|
|
|
{ |
40
|
|
|
$availableAttributes = []; |
41
|
|
|
|
42
|
|
|
foreach (\Flare::config('attributes') as $attributeType => $attributeFullClassname) { |
43
|
|
|
$availableAttributes = array_add( |
44
|
|
|
$availableAttributes, |
45
|
|
|
$attributeType, |
46
|
|
|
$attributeFullClassname |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $availableAttributes; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Determines if an AttributeType class exists or not. |
55
|
|
|
* |
56
|
|
|
* @param string $type |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected function attributeTypeExists($type) |
61
|
|
|
{ |
62
|
|
|
return $this->resolveAttributeClass($type) ? true : false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Resolves the Class of an Attribute and returns it as a string. |
67
|
|
|
* |
68
|
|
|
* @param string $type |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function resolveAttributeClass($type) |
73
|
|
|
{ |
74
|
|
|
if (class_exists($type)) { |
75
|
|
|
return $type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (array_key_exists($type, $attributes = $this->availableAttributes())) { |
79
|
|
|
return $this->availableAttributes()[$type]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return false; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.