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->beforeCommonPrepareToRender(); |
32
|
|
|
|
33
|
|
|
$this->element->beforeElementPrepareToRender(); |
34
|
|
|
|
35
|
|
|
$this->element->parts->put('class', $this->generateClasses()); |
36
|
|
|
|
37
|
|
|
$this->element->parts->put('style', $this->generateStyles()); |
38
|
|
|
|
39
|
|
|
foreach ($this->element->attributes as $key => $value) { |
40
|
|
|
$this->element->parts->put($key, $value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->element->afterCommonPrepareToRender(); |
44
|
|
|
|
45
|
|
|
$this->element->afterElementPrepareToRender(); |
46
|
|
|
|
47
|
|
|
return $this->render(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function render(): string |
51
|
|
|
{ |
52
|
|
|
$attributes = $this->generateParts(); |
53
|
|
|
|
54
|
|
|
$tag = $this->element->getTagHtml(); |
55
|
|
|
|
56
|
|
|
$tag = str_replace('**attributes**', $attributes, $tag); |
57
|
|
|
|
58
|
|
|
if ($this->element->layout !== null) { |
59
|
|
|
$views = [ |
60
|
|
|
'fb::' . $this->element->layout->getViewsDirPath() . '.' . $this->element->getClassName(), |
61
|
|
|
'fb::' . $this->element->layout->getViewsDirPath() . '.base', |
62
|
|
|
]; |
63
|
|
|
foreach ($views as $view) { |
64
|
|
|
if (view()->exists($view) && $this->element->onlyTagRender === false) { |
65
|
|
|
$data = [ |
66
|
|
|
'id' => $this->element->parts->get('id'), |
67
|
|
|
'help' => $this->element->help, |
68
|
|
|
'label' => $this->element->label, |
69
|
|
|
'tag' => $tag, |
70
|
|
|
'value' => $this->element->value, |
71
|
|
|
'class' => $this->generateClasses(), |
72
|
|
|
'style' => $this->generateStyles(), |
73
|
|
|
'attributes' => $this->generateAttributes(), |
74
|
|
|
'fieldName' => $this->element->attributes->get('name'), |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
return view($view, $data); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $tag; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Получить строку классов для добавления в атрибут class |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function generateClasses(): string |
91
|
|
|
{ |
92
|
|
|
$string = $this->element->classes->implode(' '); |
93
|
|
|
|
94
|
|
|
return $string; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Получить строку стилей для добавления в атрибут style |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function generateStyles(): string |
103
|
|
|
{ |
104
|
|
|
$string = $this->element->styles->pipe(function ($styles) { |
105
|
|
|
$attribute = ''; |
106
|
|
|
foreach ($styles as $key => $value) { |
107
|
|
|
$attribute .= $key . ':' . $value . ';'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $attribute; |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
return $string; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Получить строку атрибутов |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function generateAttributes(): string |
122
|
|
|
{ |
123
|
|
|
return $this->makeString($this->element->attributes); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Получить строку атрибутов с добавлением классов и стилей |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function generateParts(): string |
132
|
|
|
{ |
133
|
|
|
$string = $this->makeString($this->element->parts); |
134
|
|
|
|
135
|
|
|
return $string; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $collection |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
private function makeString($collection): string |
143
|
|
|
{ |
144
|
|
|
$string = ''; |
145
|
|
|
|
146
|
|
|
foreach ($collection as $key => $value) { |
147
|
|
|
if (empty($value) || (is_bool($value) && $value === false)) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$string .= ' ' . $key . '="' . (is_bool($value) ? $key : $value) . '"'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $string; |
155
|
|
|
} |
156
|
|
|
} |