Field::renderData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.7333
1
<?php
2
3
4
namespace EasyPanel\Parsers\Fields;
5
6
7
use EasyPanel\Concerns\Translatable;
8
9
class Field
10
{
11
    use Translatable;
12
13
    protected $title;
14
    protected $model;
15
    protected $key;
16
    protected $style;
17
    protected $alt;
18
    protected $badgeType = 'info';
19
    protected $dataStub = 'text.stub';
20
    protected $headStub = 'sortable.stub';
21
    protected $target = 'text.stub';
22
    protected $height = 50;
23
    protected $width = 50;
24
    protected $trueColor = 'success';
25
    protected $trueValue = 'True !';
26
    protected $falseColor = 'danger';
27
    protected $falseValue = 'False !';
28
29
    public function __construct($title)
30
    {
31
        $this->title = $title;
32
        $this->addText($title);
33
    }
34
35
    public static function title($title)
36
    {
37
        return new static($title);
38
    }
39
40
    public function style($style)
41
    {
42
        $this->style = $style;
43
44
        return $this;
45
    }
46
47
    public function asImage()
48
    {
49
        if($this->dataStub != 'linked-image.stub'){
50
            $this->dataStub = 'image.stub';
51
        }
52
53
        return $this;
54
    }
55
56
    public function clickableImage($target = '_blank')
57
    {
58
        $this->dataStub = 'linked-image.stub';
59
        $this->target = $target;
60
61
        return $this;
62
    }
63
64
    public function asBadge()
65
    {
66
        $this->dataStub = 'badge.stub';
67
68
        return $this;
69
    }
70
71
    public function asBooleanBadge($trueColor = 'success', $falseColor = 'danger')
72
    {
73
        $this->dataStub = 'bool-badge.stub';
74
        $this->trueColor($trueColor);
75
        $this->falseColor($falseColor);
76
77
        return $this;
78
    }
79
80
    public function trueColor($color)
81
    {
82
        $this->trueColor = $color;
83
84
        return $this;
85
    }
86
87
    public function falseColor($color)
88
    {
89
        $this->falseColor = $color;
90
91
        return $this;
92
    }
93
94
    public function trueValue($value)
95
    {
96
        $this->trueValue = $value;
97
        $this->addText($value);
98
99
        return $this;
100
    }
101
102
    public function falseValue($value)
103
    {
104
        $this->falseValue = $value;
105
        $this->addText($value);
106
107
        return $this;
108
    }
109
110
    public function roundedImage()
111
    {
112
        $this->style .= " rounded-circle ";
113
114
        return $this;
115
    }
116
117
    public function alt($alt)
118
    {
119
        $this->alt = $alt;
120
        $this->addText($alt);
121
122
        return $this;
123
    }
124
125
    public function height($height)
126
    {
127
        $this->height = $height;
128
129
        return $this;
130
    }
131
132
    public function width($width)
133
    {
134
        $this->height = $width;
135
136
        return $this;
137
    }
138
139
    public function getTitle()
140
    {
141
        return $this->title;
142
    }
143
144
    public function badgeType($type)
145
    {
146
        $this->badgeType = $type;
147
148
        return $this;
149
    }
150
151
    public function roundedBadge()
152
    {
153
        $this->style .= ' badge-pill ';
154
155
        return $this;
156
    }
157
158
    public function setKey($key)
159
    {
160
        $this->key = $key;
161
162
        return $this;
163
    }
164
165
    public function setModel($model)
166
    {
167
        $this->model = $model;
168
169
        return $this;
170
    }
171
172
    public function renderTitle()
173
    {
174
        $stubContent = $this->getTitleStubContent();
175
176
        $array = [
177
            '{{ key }}' => $this->key,
178
            '{{ title }}' => $this->title
179
        ];
180
181
        return str_replace(array_keys($array), array_values($array), $stubContent);
182
    }
183
184
    public function withoutSorting()
185
    {
186
        $this->headStub = 'not-sortable.stub';
187
188
        return $this;
189
    }
190
191
    public function renderData()
192
    {
193
        $stubContent = $this->getDataStubContent();
194
195
        $array = [
196
            '{{ key }}' => $this->parseRelationalKey($this->key),
197
            '{{ model }}' => $this->model,
198
            '{{ height }}' => $this->height,
199
            '{{ width }}' => $this->width,
200
            '{{ style }}' => $this->style,
201
            '{{ alt }}' => $this->alt,
202
            '{{ badgeType }}' => $this->badgeType,
203
            '{{ target }}' => $this->target,
204
            '{{ trueColor }}' => $this->trueColor,
205
            '{{ trueValue }}' => $this->trueValue,
206
            '{{ falseColor }}' => $this->falseColor,
207
            '{{ falseValue }}' => $this->falseValue,
208
        ];
209
210
        $this->translate();
211
212
        return str_replace(array_keys($array), array_values($array), $stubContent);
213
    }
214
215
    private function getDataStubContent()
216
    {
217
        return file_get_contents(__DIR__.'/stubs/'.$this->dataStub);
218
    }
219
220
    private function getTitleStubContent()
221
    {
222
        if ($this->isRelational()){
223
            return file_get_contents(__DIR__.'/stubs/titles/not-sortable.stub');
224
        }
225
226
        return file_get_contents(__DIR__.'/stubs/titles/'.$this->headStub);
227
    }
228
229
    private function isRelational()
230
    {
231
        return \Str::contains($this->key, '.');
232
    }
233
234
    private function parseRelationalKey($key){
235
        return str_replace('.', '->', $key);
236
    }
237
238
}
239