Completed
Push — master ( 60ac2f...695cd7 )
by Denis
01:32
created

Render::attributesToParts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
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->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 (is_null($this->element->layout) === false) {
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
    protected function generateClasses(): string
86
    {
87
        $string = $this->element->classes->implode(' ');
88
89
        return $string;
90
    }
91
92
    protected function generateStyles(): string
93
    {
94
        $string = $this->element->styles->pipe(function ($styles) {
95
            $attribute = '';
96
            foreach ($styles as $key => $value) {
97
                $attribute .= $key . ':' . $value . ';';
98
            }
99
100
            return $attribute;
101
        });
102
103
        return $string;
104
    }
105
106 View Code Duplication
    protected function generateAttributes(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
107
    {
108
        $string = '';
109
110
        foreach ($this->element->attributes as $key => $value) {
111
            if (is_null($value) || (is_bool($value) && $value === false)) {
112
                continue;
113
            }
114
115
            $string .= ' ' . $key . '="' . (is_bool($value) ? $key : $value) . '"';
116
        }
117
118
        return $string;
119
    }
120
121 View Code Duplication
    protected function generateParts(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
122
    {
123
        $attributes = '';
124
125
        foreach ($this->element->parts as $key => $value) {
126
            if (empty($value) || (is_bool($value) && $value === false)) {
127
                continue;
128
            }
129
130
            $attributes .= ' ' . $key . '="' . (is_bool($value) ? $key : $value) . '"';
131
        }
132
133
        return $attributes;
134
    }
135
}