Passed
Push — master ( 4d370f...bcac8b )
by Reza
10:44
created

Field::trueValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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