1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelHtml\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use ReflectionMethod; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait FormAccessible |
13
|
|
|
* |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
trait FormAccessible |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A cached ReflectionClass instance for $this. |
25
|
|
|
* |
26
|
|
|
* @var \ReflectionClass |
27
|
|
|
*/ |
28
|
|
|
protected $reflection; |
29
|
|
|
|
30
|
|
|
/* ----------------------------------------------------------------- |
31
|
|
|
| Main Methods |
32
|
|
|
| ----------------------------------------------------------------- |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get form value from the eloquent model. |
37
|
|
|
* |
38
|
|
|
* @param string $key |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
18 |
|
public function getFormValue(string $key) |
43
|
|
|
{ |
44
|
18 |
|
$value = $this->getAttributeFromArray($key); |
45
|
|
|
|
46
|
18 |
|
if (in_array($key, $this->getDates()) && ! is_null($value)) |
47
|
12 |
|
$value = $this->asDateTime($value); |
48
|
|
|
|
49
|
18 |
|
return $this->hasFormMutator($key) |
50
|
12 |
|
? $this->mutateFormAttribute($key, $value) |
51
|
18 |
|
: data_get($this, $key); // No form mutator, let the model resolve this |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/* ----------------------------------------------------------------- |
55
|
|
|
| Eloquent Methods |
56
|
|
|
| ----------------------------------------------------------------- |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get an attribute from the $attributes array. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
abstract protected function getAttributeFromArray($key); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the attributes that should be converted to dates. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
abstract public function getDates(); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return a timestamp as DateTime object. |
77
|
|
|
* |
78
|
|
|
* @param mixed $value |
79
|
|
|
* |
80
|
|
|
* @return \Carbon\Carbon |
81
|
|
|
*/ |
82
|
|
|
abstract protected function asDateTime($value); |
83
|
|
|
|
84
|
|
|
/* ----------------------------------------------------------------- |
85
|
|
|
| Other Methods |
86
|
|
|
| ----------------------------------------------------------------- |
87
|
|
|
*/ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if has a form mutator. |
91
|
|
|
* |
92
|
|
|
* @param string $key |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
18 |
|
protected function hasFormMutator(string $key): bool |
97
|
|
|
{ |
98
|
18 |
|
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC); |
99
|
|
|
|
100
|
18 |
|
return collect($methods)->filter(function (ReflectionMethod $method) use ($key) { |
101
|
18 |
|
return $method->name === $this->getMutateFromMethodName($key); |
102
|
18 |
|
})->isNotEmpty(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Mutate the form attribute. |
107
|
|
|
* |
108
|
|
|
* @param string $key |
109
|
|
|
* @param mixed $value |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
12 |
|
private function mutateFormAttribute(string $key, $value) |
114
|
|
|
{ |
115
|
12 |
|
return $this->{$this->getMutateFromMethodName($key)}($value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the mutate form method name. |
120
|
|
|
* |
121
|
|
|
* @param string $key |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
18 |
|
private function getMutateFromMethodName(string $key): string |
126
|
|
|
{ |
127
|
18 |
|
return 'form'.Str::studly($key).'Attribute'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get a ReflectionClass Instance. |
132
|
|
|
* |
133
|
|
|
* @return \ReflectionClass |
134
|
|
|
*/ |
135
|
18 |
|
protected function getReflection(): ReflectionClass |
136
|
|
|
{ |
137
|
18 |
|
if (is_null($this->reflection)) { |
138
|
18 |
|
$this->reflection = new ReflectionClass($this); |
139
|
|
|
} |
140
|
|
|
|
141
|
18 |
|
return $this->reflection; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|