Passed
Push — master ( 2188ea...f3eaf1 )
by Reza
03:11
created

Field::clickableImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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