Completed
Push — master ( 6a33ec...f7065a )
by Denis
01:24
created

Render::generateAttributes()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 0
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
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->generateAttributes();
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)),
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
                    ];
65
66
                    return view($view, $data);
67
                }
68
            }
69
        }
70
71
        return $tag;
72
    }
73
74
    protected function generateAttributes(): string
75
    {
76
        $attributes = '';
77
78
        foreach ($this->element->parts as $key => $value) {
79
            if (is_null($value) || (is_bool($value) && $value === false)) {
80
                continue;
81
            }
82
83
            $attributes .= ' ' . $key . '="' . (is_bool($value) ? $key : $value) . '"';
84
        }
85
86
        return $attributes;
87
    }
88
89
    protected function stylesToParts(): void
90
    {
91
        if ($this->element->styles->isNotEmpty()) {
92
            $this->element->parts->put('style', $this->element->styles->pipe(function ($styles) {
93
                $styleAttr = '';
94
                foreach ($styles as $key => $value) {
95
                    $styleAttr .= $key . ':' . $value . ';';
96
                }
97
98
                return $styleAttr;
99
            }));
100
        }
101
    }
102
103
    protected function classesToParts(): void
104
    {
105
        if ($this->element->classes->isNotEmpty()) {
106
            $this->element->parts->put('class', $this->element->classes->implode(' '));
107
        }
108
    }
109
110
    public function attributesToParts(): void
111
    {
112
        foreach ($this->element->attributes as $key => $value) {
113
            $this->element->parts->put($key, $value);
114
        }
115
    }
116
}