|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hafijul233\Form\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use ReflectionMethod; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait FormAccessibleTrait |
|
11
|
|
|
*/ |
|
12
|
|
|
trait FormAccessibleTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* A cached ReflectionClass instance for $this |
|
16
|
|
|
* |
|
17
|
|
|
* @var ReflectionClass |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $reflection; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $key |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getFormValue(string $key) |
|
26
|
|
|
{ |
|
27
|
|
|
$value = $this->getAttributeFromArray($key); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// If the attribute is listed as a date, we will convert it to a DateTime |
|
30
|
|
|
// instance on retrieval, which makes it quite convenient to work with |
|
31
|
|
|
// date fields without having to create a mutator for each property. |
|
32
|
|
|
if (in_array($key, $this->getDates())) { |
|
|
|
|
|
|
33
|
|
|
if (! is_null($value)) { |
|
34
|
|
|
$value = $this->asDateTime($value); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// If the attribute has a get mutator, we will call that then return what |
|
39
|
|
|
// it returns as the value, which is useful for transforming values on |
|
40
|
|
|
// retrieval from the model to a form that is more useful for usage. |
|
41
|
|
|
if ($this->hasFormMutator($key)) { |
|
42
|
|
|
return $this->mutateFormAttribute($key, $value); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$keys = explode('.', $key); |
|
46
|
|
|
|
|
47
|
|
|
if ($this->isNestedModel($keys[0])) { |
|
48
|
|
|
$relatedModel = $this->getRelation($keys[0]); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
unset($keys[0]); |
|
51
|
|
|
$key = implode('.', $keys); |
|
52
|
|
|
|
|
53
|
|
|
if (method_exists($relatedModel, 'hasFormMutator') && $key !== '' && $relatedModel->hasFormMutator($key)) { |
|
54
|
|
|
return $relatedModel->getFormValue($key); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return data_get($relatedModel, empty($key) ? null : $key); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// No form mutator, let the model resolve this |
|
61
|
|
|
return data_get($this, $key); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function hasFormMutator(string $key): bool |
|
69
|
|
|
{ |
|
70
|
|
|
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC); |
|
71
|
|
|
|
|
72
|
|
|
$mutator = collect($methods) |
|
73
|
|
|
->first(function (ReflectionMethod $method) use ($key) { |
|
74
|
|
|
return $method->getName() === 'form'.Str::studly($key).'Attribute'; |
|
75
|
|
|
}); |
|
76
|
|
|
|
|
77
|
|
|
return (bool) $mutator; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get a ReflectionClass Instance |
|
82
|
|
|
* |
|
83
|
|
|
* @return ReflectionClass |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getReflection(): ReflectionClass |
|
86
|
|
|
{ |
|
87
|
|
|
if (! $this->reflection) { |
|
88
|
|
|
$this->reflection = new ReflectionClass($this); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->reflection; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $key |
|
96
|
|
|
* @param $value |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
private function mutateFormAttribute($key, $value) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->{'form'.Str::studly($key).'Attribute'}($value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Check for a nested model. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $key |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function isNestedModel(string $key): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return in_array($key, array_keys($this->getRelations())); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|