Passed
Push — master ( f43cbb...9db35c )
by Reza
03:37
created

Field   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 65
dl 0
loc 174
rs 10
c 3
b 0
f 0
wmc 24

22 Methods

Rating   Name   Duplication   Size   Complexity  
A withoutSorting() 0 5 1
A alt() 0 5 1
A __construct() 0 3 1
A renderTitle() 0 10 1
A roundedBadge() 0 5 1
A asImage() 0 7 2
A roundedImage() 0 5 1
A asBadge() 0 5 1
A title() 0 3 1
A renderData() 0 16 1
A getTitle() 0 3 1
A setKey() 0 5 1
A isRelational() 0 3 1
A height() 0 5 1
A parseRelationalKey() 0 2 1
A getTitleStubContent() 0 7 2
A width() 0 5 1
A clickableImage() 0 6 1
A getDataStubContent() 0 3 1
A setModel() 0 5 1
A badgeType() 0 5 1
A style() 0 5 1
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
21
    public function __construct($title)
22
    {
23
        $this->title = $title;
24
    }
25
26
    public static function title($title)
27
    {
28
        return new static($title);
29
    }
30
31
    public function style($style)
32
    {
33
        $this->style = $style;
34
35
        return $this;
36
    }
37
38
    public function asImage()
39
    {
40
        if($this->dataStub != 'linked-image.stub'){
41
            $this->dataStub = 'image.stub';
42
        }
43
44
        return $this;
45
    }
46
47
    public function clickableImage($target = '_blank')
48
    {
49
        $this->dataStub = 'linked-image.stub';
50
        $this->target = $target;
51
52
        return $this;
53
    }
54
55
    public function asBadge()
56
    {
57
        $this->dataStub = 'badge.stub';
58
59
        return $this;
60
    }
61
62
    public function roundedImage()
63
    {
64
        $this->style .= " rounded-circle ";
65
66
        return $this;
67
    }
68
69
    public function alt($alt)
70
    {
71
        $this->alt = $alt;
72
73
        return $this;
74
    }
75
76
    public function height($height)
77
    {
78
        $this->height = $height;
79
80
        return $this;
81
    }
82
83
    public function width($width)
84
    {
85
        $this->height = $width;
86
87
        return $this;
88
    }
89
90
    public function getTitle()
91
    {
92
        return $this->title;
93
    }
94
95
    public function badgeType($type)
96
    {
97
        $this->badgeType = $type;
98
99
        return $this;
100
    }
101
102
    public function roundedBadge()
103
    {
104
        $this->style .= ' badge-pill ';
105
106
        return $this;
107
    }
108
109
    public function setKey($key)
110
    {
111
        $this->key = $key;
112
113
        return $this;
114
    }
115
116
    public function setModel($model)
117
    {
118
        $this->model = $model;
119
120
        return $this;
121
    }
122
123
    public function renderTitle()
124
    {
125
        $stubContent = $this->getTitleStubContent();
126
127
        $array = [
128
            '{{ key }}' => $this->key,
129
            '{{ title }}' => $this->title
130
        ];
131
132
        return str_replace(array_keys($array), array_values($array), $stubContent);
133
    }
134
135
    public function withoutSorting()
136
    {
137
        $this->headStub = 'not-sortable.stub';
138
139
        return $this;
140
    }
141
142
    public function renderData()
143
    {
144
        $stubContent = $this->getDataStubContent();
145
146
        $array = [
147
            '{{ key }}' => $this->parseRelationalKey($this->key),
148
            '{{ model }}' => $this->model,
149
            '{{ height }}' => $this->height,
150
            '{{ width }}' => $this->width,
151
            '{{ style }}' => $this->style,
152
            '{{ alt }}' => $this->alt,
153
            '{{ badgeType }}' => $this->badgeType,
154
            '{{ target }}' => $this->target,
155
        ];
156
157
        return str_replace(array_keys($array), array_values($array), $stubContent);
158
    }
159
160
    private function getDataStubContent()
161
    {
162
        return file_get_contents(__DIR__.'/stubs/'.$this->dataStub);
163
    }
164
165
    private function getTitleStubContent()
166
    {
167
        if ($this->isRelational()){
168
            return file_get_contents(__DIR__.'/stubs/titles/not-sortable.stub');
169
        }
170
171
        return file_get_contents(__DIR__.'/stubs/titles/'.$this->headStub);
172
    }
173
174
    private function isRelational()
175
    {
176
        return \Str::contains($this->key, '.');
177
    }
178
179
    private function parseRelationalKey($key){
180
        return str_replace('.', '->', $key);
181
    }
182
183
}
184