1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EasyPanel\Parsers\Fields; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Field |
8
|
|
|
{ |
9
|
|
|
protected $title; |
10
|
|
|
protected $model; |
11
|
|
|
protected $key; |
12
|
|
|
protected $style; |
13
|
|
|
protected $alt; |
14
|
|
|
protected $badgeType = 'info'; |
15
|
|
|
protected $stub = 'text.stub'; |
16
|
|
|
protected $height = 50; |
17
|
|
|
protected $width = 50; |
18
|
|
|
|
19
|
|
|
public function __construct($title) |
20
|
|
|
{ |
21
|
|
|
$this->title = $title; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function title($title) |
25
|
|
|
{ |
26
|
|
|
return new static($title); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function style($style) |
30
|
|
|
{ |
31
|
|
|
$this->style = $style; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function asImage() |
37
|
|
|
{ |
38
|
|
|
$this->stub = 'image.stub'; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function asBadge() |
44
|
|
|
{ |
45
|
|
|
$this->stub = 'badge.stub'; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function roundedImage() |
51
|
|
|
{ |
52
|
|
|
$this->style .= " rounded-circle "; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function alt($alt) |
56
|
|
|
{ |
57
|
|
|
$this->alt = $alt; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function height($height) |
63
|
|
|
{ |
64
|
|
|
$this->height = $height; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function width($width) |
70
|
|
|
{ |
71
|
|
|
$this->height = $width; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getTitle() |
77
|
|
|
{ |
78
|
|
|
return $this->title; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function badgeType($type) |
82
|
|
|
{ |
83
|
|
|
$this->badgeType = $type; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function roundedBadge() |
89
|
|
|
{ |
90
|
|
|
$this->style .= ' badge-pill '; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setKey($key) |
96
|
|
|
{ |
97
|
|
|
$this->key = $key; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setModel($model) |
103
|
|
|
{ |
104
|
|
|
$this->model = $model; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function renderTitle() |
110
|
|
|
{ |
111
|
|
|
$stubContent = $this->getTitleStubContent(); |
112
|
|
|
|
113
|
|
|
$array = [ |
114
|
|
|
'{{ key }}' => $this->key, |
115
|
|
|
'{{ title }}' => $this->title |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
return str_replace(array_keys($array), array_values($array), $stubContent); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function renderData() |
122
|
|
|
{ |
123
|
|
|
$stubContent = $this->getDataStubContent(); |
124
|
|
|
|
125
|
|
|
$array = [ |
126
|
|
|
'{{ key }}' => $this->parseRelationalKey($this->key), |
127
|
|
|
'{{ model }}' => $this->model, |
128
|
|
|
'{{ height }}' => $this->height, |
129
|
|
|
'{{ width }}' => $this->width, |
130
|
|
|
'{{ style }}' => $this->style, |
131
|
|
|
'{{ alt }}' => $this->alt, |
132
|
|
|
'{{ badgeType }}' => $this->badgeType, |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
return str_replace(array_keys($array), array_values($array), $stubContent); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function getDataStubContent() |
139
|
|
|
{ |
140
|
|
|
return file_get_contents(__DIR__.'/stubs/'.$this->stub); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function getTitleStubContent() |
144
|
|
|
{ |
145
|
|
|
if ($this->isRelational()){ |
146
|
|
|
return file_get_contents(__DIR__.'/stubs/titles/relational.stub'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return file_get_contents(__DIR__.'/stubs/titles/normal.stub'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function isRelational() |
153
|
|
|
{ |
154
|
|
|
return \Str::contains($this->key, '.'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function parseRelationalKey($key){ |
158
|
|
|
return str_replace('.', '->', $key); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|