1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ngtfkx\Laradeck\FormBuilder; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ngtfkx\Laradeck\FormBuilder\Elements\AbstractElement; |
7
|
|
|
|
8
|
|
|
class Render |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var $element AbstractElement |
12
|
|
|
*/ |
13
|
|
|
protected $element; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Render constructor. |
17
|
|
|
* @param AbstractElement $element |
18
|
|
|
*/ |
19
|
|
|
public function __construct(AbstractElement $element) |
20
|
|
|
{ |
21
|
|
|
$this->element = $element; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Преобразовать в строку для вывода в HTML |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function __toString(): string |
30
|
|
|
{ |
31
|
|
|
$this->element->beforeToParts(); |
32
|
|
|
|
33
|
|
|
$this->classesToParts(); |
34
|
|
|
|
35
|
|
|
$this->stylesToParts(); |
36
|
|
|
|
37
|
|
|
$this->attributesToParts(); |
38
|
|
|
|
39
|
|
|
$this->element->afterToParts(); |
40
|
|
|
|
41
|
|
|
return $this->render(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function render(): string |
45
|
|
|
{ |
46
|
|
|
$attributes = $this->generateParts(); |
47
|
|
|
|
48
|
|
|
$tag = $this->element->getTagHtml(); |
49
|
|
|
|
50
|
|
|
$tag = str_replace('**attributes**', $attributes, $tag); |
51
|
|
|
|
52
|
|
|
if (is_null($this->element->layout) === false) { |
53
|
|
|
$views = [ |
54
|
|
|
'fb::' . $this->element->layout->getViewsDirPath() . '.' . strtolower(class_basename($this->element)), |
55
|
|
|
'fb::' . $this->element->layout->getViewsDirPath() . '.base', |
56
|
|
|
]; |
57
|
|
|
foreach ($views as $view){ |
58
|
|
|
if (view()->exists($view) && $this->element->onlyTagRender === false) { |
59
|
|
|
$data = [ |
60
|
|
|
'id' => $this->element->parts->get('id'), |
61
|
|
|
'help' => $this->element->help, |
62
|
|
|
'label' => $this->element->label, |
63
|
|
|
'tag' => $tag, |
64
|
|
|
'value' => $this->element->value, |
65
|
|
|
'class' => $this->generateClasses(), |
66
|
|
|
'style' => $this->generateStyles(), |
67
|
|
|
'attributes' => $this->generateAttributes(), |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
return view($view, $data); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $tag; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function generateClasses(): string |
79
|
|
|
{ |
80
|
|
|
$string = $this->element->classes->implode(' '); |
81
|
|
|
|
82
|
|
|
return $string; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function generateStyles(): string |
86
|
|
|
{ |
87
|
|
|
$string = ''; |
88
|
|
|
|
89
|
|
|
return $string; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
protected function generateAttributes(): string |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$string = ''; |
95
|
|
|
|
96
|
|
|
foreach ($this->element->attributes as $key => $value) { |
97
|
|
|
if (is_null($value) || (is_bool($value) && $value === false)) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$string .= ' ' . $key . '="' . (is_bool($value) ? $key : $value) . '"'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $string; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
protected function generateParts(): string |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$attributes = ''; |
110
|
|
|
|
111
|
|
|
foreach ($this->element->parts as $key => $value) { |
112
|
|
|
if (is_null($value) || (is_bool($value) && $value === false)) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$attributes .= ' ' . $key . '="' . (is_bool($value) ? $key : $value) . '"'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $attributes; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function stylesToParts(): void |
123
|
|
|
{ |
124
|
|
|
if ($this->element->styles->isNotEmpty()) { |
125
|
|
|
$this->element->parts->put('style', $this->element->styles->pipe(function ($styles) { |
126
|
|
|
$styleAttr = ''; |
127
|
|
|
foreach ($styles as $key => $value) { |
128
|
|
|
$styleAttr .= $key . ':' . $value . ';'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $styleAttr; |
132
|
|
|
})); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function classesToParts(): void |
137
|
|
|
{ |
138
|
|
|
if ($this->element->classes->isNotEmpty()) { |
139
|
|
|
$this->element->parts->put('class', $this->element->classes->implode(' ')); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function attributesToParts(): void |
144
|
|
|
{ |
145
|
|
|
foreach ($this->element->attributes as $key => $value) { |
146
|
|
|
$this->element->parts->put($key, $value); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.