Base::setRecordTransformer()   A
last analyzed

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
6
class Base
7
{
8
9
    /**
10
     * Fields
11
     *
12
     * @var array
13
     */
14
    private $fields = [];
15
16
    /**
17
     * Service logic adapter
18
     *
19
     * @var ListBuilderAdapter
20
     */
21
    private $listBuilderAdapter;
22
23
    /**
24
     * List item transformation callback
25
     *
26
     * @var array
27
     */
28
    private $recordTransformer = [];
29
30
    /**
31
     * List title
32
     *
33
     * @var string
34
     */
35
    public $listTitle = 'Список записей';
36
37
    /**
38
     * List description
39
     *
40
     * @var string
41
     */
42
    public $listDescription = 'Выберите необходимое действие';
43
44
    /**
45
     * Name of the template for the empty records list
46
     *
47
     * @var string
48
     */
49
    protected $noItemsTemplateName = 'listing-no-items';
50
51
    /**
52
     * No items block name
53
     *
54
     * @var string
55
     */
56
    private $noItemsView = '';
57
58
    /**
59
     * Constructor
60
     *
61
     * @param array $fields
62
     *            List of fields
63
     * @param ListBuilderAdapter $listBuilderAdapter
64
     *            Adapter for the data source
65
     */
66
    public function __construct(array $fields, ListBuilderAdapter $listBuilderAdapter)
67
    {
68
        $transformedFields = [];
69
70
        foreach ($fields as $i => $field) {
71
            $key = is_array($field) ? $i : $field;
72
            $transformedFields[$key] = is_array($field) ? $field : [
73
                'title' => $field
74
            ];
75
        }
76
77
        $this->fields = $transformedFields;
78
79
        $this->listBuilderAdapter = $listBuilderAdapter;
80
    }
81
82
    /**
83
     * Method sets field $noItemsView
84
     *
85
     * @param string $noItemsView
86
     */
87
    public function setNoItemsView(string $noItemsView): void
88
    {
89
        $this->noItemsView = $noItemsView;
90
    }
91
92
    /**
93
     * Method returns content for the case when no items were found
94
     *
95
     * @return string content for the case when no items were found
96
     */
97
    protected function getNoItemsContent(): string
98
    {
99
        return str_replace([
100
            '{list-description}',
101
            '{list-title}',
102
            '{extra-message}'
103
        ], [
104
            'Ни одной записи не найдено',
105
            $this->listTitle,
106
            $this->noItemsView
107
        ], BootstrapWidgets::get($this->noItemsTemplateName));
108
    }
109
110
    /**
111
     * Setting record transformer
112
     *
113
     * @param mixed $recordTransformer
114
     *            callable record transformer
115
     * @codeCoverageIgnore
116
     */
117
    public function setRecordTransformer($recordTransformer): void
118
    {
119
        $this->recordTransformer = $recordTransformer;
120
    }
121
122
    /**
123
     * Method returns fields
124
     *
125
     * @return array fields
126
     */
127
    public function getFields(): array
128
    {
129
        return $this->fields;
130
    }
131
132
    /**
133
     * Method returns list builder adapter
134
     *
135
     * @return ListBuilderAdapter
136
     */
137
    protected function getListBuilderAdapter(): ListBuilderAdapter
138
    {
139
        return $this->listBuilderAdapter;
140
    }
141
142
    /**
143
     * Method transforms database record
144
     *
145
     * @param object $record
146
     *            Transforming record
147
     * @return object Transformed record
148
     */
149
    protected function transformRecord(object $record): object
150
    {
151
        // here we assume that we get from service
152
        // already transformed
153
        // and here we provide only additional transformations
154
        if (is_callable($this->recordTransformer)) {
155
            $record = call_user_func($this->recordTransformer, $record);
156
        }
157
158
        return $record;
159
    }
160
} 
161