1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Form\Elements; |
5
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
9
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
10
|
|
|
use Sco\Admin\Contracts\Form\Elements\ElementInterface; |
11
|
|
|
use Sco\Admin\Contracts\WithModel; |
12
|
|
|
|
13
|
|
|
abstract class Element implements |
14
|
|
|
ElementInterface, |
15
|
|
|
WithModel, |
16
|
|
|
Arrayable, |
17
|
|
|
Jsonable, |
18
|
|
|
JsonSerializable |
19
|
|
|
{ |
20
|
|
|
protected $type; |
21
|
|
|
|
22
|
|
|
protected $name; |
23
|
|
|
protected $title; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
protected $defaultValue; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
32
|
|
|
*/ |
33
|
|
|
protected $model; |
34
|
|
|
|
35
|
|
|
public function __construct($name, $title) |
36
|
|
|
{ |
37
|
|
|
$this->name = $name; |
38
|
|
|
$this->title = $title; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getName() |
42
|
|
|
{ |
43
|
|
|
return $this->name; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setName($value) |
47
|
|
|
{ |
48
|
|
|
$this->name = $value; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getTitle() |
54
|
|
|
{ |
55
|
|
|
return $this->title; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setTitle($value) |
59
|
|
|
{ |
60
|
|
|
$this->title = $value; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getModel() |
66
|
|
|
{ |
67
|
|
|
return $this->model; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setModel(Model $model) |
71
|
|
|
{ |
72
|
|
|
$this->model = $model; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getValue() |
78
|
|
|
{ |
79
|
|
|
return $this->getModelValue(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getModelValue() |
83
|
|
|
{ |
84
|
|
|
$model = $this->getModel(); |
85
|
|
|
$value = $this->getDefaultValue(); |
86
|
|
|
if (is_null($model) || !$model->exists) { |
87
|
|
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$relations = explode('.', $this->getName(), 2); |
91
|
|
|
$count = count($relations); |
92
|
|
|
|
93
|
|
|
if ($count == 1) { |
94
|
|
|
return $model->getAttribute($this->getName()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($relations as $relation) { |
98
|
|
|
if ($model->{$relation} instanceof Model) { |
99
|
|
|
$model = $model->{$relation}; |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $model->getAttribute($relation); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getDefaultValue() |
108
|
|
|
{ |
109
|
|
|
return $this->defaultValue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function toArray() |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
'key' => $this->name, |
116
|
|
|
'title' => $this->title, |
117
|
|
|
'type' => $this->type, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function jsonSerialize() |
122
|
|
|
{ |
123
|
|
|
return $this->toArray(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function toJson($options = 0) |
127
|
|
|
{ |
128
|
|
|
return json_encode($this->jsonSerialize(), $options); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function __toString() |
132
|
|
|
{ |
133
|
|
|
return $this->toJson(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|