Completed
Push — master ( 0411a3...0f768a )
by Alex
03:32
created

Simple::setRecordTransformer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\Gui\ListBuilder;
3
4
use Mezon\Gui\WidgetsRegistry\BootstrapWidgets;
5
use Mezon\TemplateEngine\TemplateEngine;
6
use Mezon\Transport\Request;
7
8
/**
9
 * Class Simple
10
 *
11
 * @package GUI
12
 * @subpackage ListBuilder
13
 * @author Dodonov A.A.
14
 * @version v.1.0 (2020/12/19)
15
 * @copyright Copyright (c) 2020, aeon.org
16
 */
17
18
/**
19
 * Class constructs grids
20
 */
21
class Simple extends Base
22
{
23
24
    /**
25
     * Method compiles listing items cells
26
     *
27
     * @return string Compiled row
28
     */
29
    private function listingItemsCells(): string
30
    {
31
        $content = '';
32
33
        foreach (array_keys($this->fields) as $name) {
34
            if ($name == 'domain_id') {
35
                continue;
36
            }
37
            if ($name == 'id') {
38
                $content .= BootstrapWidgets::get('listing-row-centered-cell');
39
            } else {
40
                $content .= BootstrapWidgets::get('listing-row-cell');
41
            }
42
            $content = str_replace('{name}', '{' . $name . '}', $content);
43
        }
44
45
        return $content;
46
    }
47
48
    /**
49
     * Method transforms database record
50
     *
51
     * @param array $record
52
     *            Transforming record
53
     * @return object Transformed record
54
     */
55
    private function transformRecord(object $record): object
56
    {
57
        // here we assume that we get from service
58
        // already transformed
59
        // and here we provide only additional transformations
60
        if (is_callable($this->recordTransformer)) {
61
            $record = call_user_func($this->recordTransformer, $record);
62
        }
63
64
        return $record;
65
    }
66
67
    /**
68
     * Method compiles header cells
69
     *
70
     * @return string Compiled header
71
     */
72
    private function listingHeaderCells(): string
73
    {
74
        $content = '';
75
76
        foreach ($this->fields as $name => $data) {
77
            if ($name == 'domain_id') {
78
                continue;
79
            }
80
81
            $idStyle = $name == 'id' ? 'style="text-align: center; width:5%;"' : '';
82
83
            $content .= BootstrapWidgets::get('listing-header-cell');
84
            $content = str_replace([
85
                '{id-style}',
86
                '{title}'
87
            ], [
88
                $idStyle,
89
                $data['title']
90
            ], $content);
91
        }
92
93
        return $content;
94
    }
95
96
    /**
97
     * Method compiles listing header
98
     *
99
     * @return string Compiled header
100
     */
101
    private function simpleListingHeader(): string
102
    {
103
        $content = BootstrapWidgets::get('simple-listing-header');
104
105
        $content = str_replace([
106
            '{list-description}',
107
            '{list-title}'
108
        ], [
109
            Request::getParam('list-description', $this->listDescription),
110
            $this->listTitle
111
        ], $content);
112
113
        return str_replace('{cells}', $this->listingHeaderCells(), $content);
114
    }
115
116
    /**
117
     * Method compiles listing items
118
     *
119
     * @param array $records
120
     *            List of records
121
     * @return string Compiled simple list
122
     */
123
    private function simpleListingItems(array $records): string
124
    {
125
        $content = '';
126
127
        foreach ($records as $record) {
128
            $content .= str_replace('{items}', $this->listingItemsCells(), BootstrapWidgets::get('listing-row'));
129
130
            $record = $this->transformRecord($record);
131
132
            $record = $this->listBuilderAdapter->preprocessListItem($record);
133
134
            $content = TemplateEngine::printRecord($content, $record);
135
        }
136
137
        return $content;
138
    }
139
140
    /**
141
     * Method compiles simple_listing form
142
     *
143
     * @return string Compiled simple listing form
144
     */
145
    public function listingForm(): string
146
    {
147
        $records = $this->listBuilderAdapter->all();
148
149
        if (! empty($records)) {
150
            $header = $this->simpleListingHeader();
151
152
            $items = $this->simpleListingItems($records);
153
154
            // they are the same with full feature listing
155
            $footer = BootstrapWidgets::get('listing-footer');
156
157
            return $header . $items . $footer;
158
        } else {
159
            return $this->getNoItemsContent();
160
        }
161
    }
162
163
    /**
164
     * Method returns fields of the list
165
     *
166
     * @return array fields list
167
     */
168
    public function getFields(): array
169
    {
170
        return $this->fields;
171
    }
172
}
173