Passed
Push — master ( 6b8d45...b1f6fe )
by Reza
03:41
created

Field::asImage()   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 0
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 $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 alt($alt)
51
    {
52
        $this->alt = $alt;
53
54
        return $this;
55
    }
56
57
    public function height($height)
58
    {
59
        $this->height = $height;
60
61
        return $this;
62
    }
63
64
    public function width($width)
65
    {
66
        $this->height = $width;
67
68
        return $this;
69
    }
70
71
    public function getTitle()
72
    {
73
        return $this->title;
74
    }
75
76
    public function badgeType($type)
77
    {
78
        $this->badgeType = $type;
79
80
        return $this;
81
    }
82
83
    public function roundedBadge()
84
    {
85
        $this->style .= ' badge-pill ';
86
87
        return $this;
88
    }
89
90
    public function setKey($key)
91
    {
92
        $this->key = $key;
93
94
        return $this;
95
    }
96
97
    public function setModel($model)
98
    {
99
        $this->model = $model;
100
101
        return $this;
102
    }
103
104
    public function renderTitle()
105
    {
106
        $stubContent = $this->getTitleStubContent();
107
108
        $array = [
109
            '{{ key }}' => $this->key,
110
            '{{ title }}' => $this->title
111
        ];
112
113
        return str_replace(array_keys($array), array_values($array), $stubContent);
114
    }
115
116
    public function renderData()
117
    {
118
        $stubContent = $this->getDataStubContent();
119
120
        $array = [
121
            '{{ key }}' => $this->parseRelationalKey($this->key),
122
            '{{ model }}' => $this->model,
123
            '{{ height }}' => $this->height,
124
            '{{ width }}' => $this->width,
125
            '{{ style }}' => $this->style,
126
            '{{ alt }}' => $this->alt,
127
            '{{ badgeType }}' => $this->badgeType,
128
        ];
129
130
        return str_replace(array_keys($array), array_values($array), $stubContent);
131
    }
132
133
    private function getDataStubContent()
134
    {
135
        return file_get_contents(__DIR__.'/stubs/'.$this->stub);
136
    }
137
138
    private function getTitleStubContent()
139
    {
140
        if ($this->isRelational()){
141
            return file_get_contents(__DIR__.'/stubs/titles/relational.stub');
142
        }
143
144
        return file_get_contents(__DIR__.'/stubs/titles/normal.stub');
145
    }
146
147
    private function isRelational()
148
    {
149
        return \Str::contains($this->key, '.');
150
    }
151
152
    private function parseRelationalKey($key){
153
        return str_replace('.', '->', $key);
154
    }
155
156
}
157