Completed
Push — master ( adb8c3...d72efb )
by ARCANEDEV
10s
created

AbstractTable::footer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php namespace Arcanedev\LogViewer\Tables;
2
3
use Arcanedev\LogViewer\Contracts\Table as TableContract;
4
use Arcanedev\LogViewer\Contracts\Utilities\LogLevels as LogLevelsContract;
5
6
/**
7
 * Class     AbstractTable
8
 *
9
 * @package  Arcanedev\LogViewer\Bases
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class AbstractTable implements TableContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /** @var array  */
19
    private $header  = [];
20
21
    /** @var array  */
22
    private $rows    = [];
23
24
    /** @var array  */
25
    private $footer  = [];
26
27
    /** @var \Arcanedev\LogViewer\Contracts\Utilities\LogLevels */
28
    protected $levels;
29
30
    /** @var string|null */
31
    protected $locale;
32
33
    /** @var array */
34
    private $data = [];
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Constructor
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Create a table instance.
42
     *
43
     * @param  array                                               $data
44
     * @param  \Arcanedev\LogViewer\Contracts\Utilities\LogLevels  $levels
45
     * @param  string|null                                         $locale
46
     */
47
    public function __construct(array $data, LogLevelsContract $levels, $locale = null)
48
    {
49
        $this->setLevels($levels);
50
        $this->setLocale(is_null($locale) ? config('log-viewer.locale') : $locale);
51
        $this->setData($data);
52
        $this->init();
53
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Getters & Setters
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Set LogLevels instance.
61
     *
62
     * @param  \Arcanedev\LogViewer\Contracts\Utilities\LogLevels  $levels
63
     *
64
     * @return \Arcanedev\LogViewer\Tables\AbstractTable
65
     */
66
    protected function setLevels(LogLevelsContract $levels)
67
    {
68
        $this->levels = $levels;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Set table locale.
75
     *
76
     * @param  string|null  $locale
77
     *
78
     * @return \Arcanedev\LogViewer\Tables\AbstractTable
79
     */
80
    protected function setLocale($locale)
81
    {
82
        if (is_null($locale) || $locale === 'auto') {
83
            $locale = app()->getLocale();
84
        }
85
86
        $this->locale = $locale;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Get table header.
93
     *
94
     * @return array
95
     */
96
    public function header()
97
    {
98
        return $this->header;
99
    }
100
101
    /**
102
     * Get table rows.
103
     *
104
     * @return array
105
     */
106
    public function rows()
107
    {
108
        return $this->rows;
109
    }
110
111
    /**
112
     * Get table footer.
113
     *
114
     * @return array
115
     */
116
    public function footer()
117
    {
118
        return $this->footer;
119
    }
120
121
    /**
122
     * Get raw data.
123
     *
124
     * @return array
125
     */
126
    public function data()
127
    {
128
        return $this->data;
129
    }
130
131
    /**
132
     * Set table data.
133
     *
134
     * @param  array  $data
135
     *
136
     * @return self
137
     */
138
    private function setData(array $data)
139
    {
140
        $this->data = $data;
141
142
        return $this;
143
    }
144
145
    /* ------------------------------------------------------------------------------------------------
146
     |  Main Functions
147
     | ------------------------------------------------------------------------------------------------
148
     */
149
    /**
150
     * Prepare the table.
151
     */
152
    private function init()
153
    {
154
        $this->header = $this->prepareHeader($this->data);
155
        $this->rows   = $this->prepareRows($this->data);
156
        $this->footer = $this->prepareFooter($this->data);
157
    }
158
159
    /**
160
     * Prepare table header.
161
     *
162
     * @param  array  $data
163
     *
164
     * @return array
165
     */
166
    abstract protected function prepareHeader(array $data);
167
168
    /**
169
     * Prepare table rows.
170
     *
171
     * @param  array  $data
172
     *
173
     * @return array
174
     */
175
    abstract protected function prepareRows(array $data);
176
177
    /**
178
     * Prepare table footer.
179
     *
180
     * @param  array  $data
181
     *
182
     * @return array
183
     */
184
    abstract protected function prepareFooter(array $data);
185
186
    /* ------------------------------------------------------------------------------------------------
187
     |  Other Functions
188
     | ------------------------------------------------------------------------------------------------
189
     */
190
    /**
191
     * Translate.
192
     *
193
     * @param  string $key
194
     *
195
     * @return string
196
     */
197
    protected function translate($key)
198
    {
199
        /** @var \Illuminate\Translation\Translator $translator */
200
        $translator = trans();
201
202
        return $translator->get('log-viewer::' . $key,  [], $this->locale);
203
    }
204
205
    /**
206
     * Get log level color.
207
     *
208
     * @param  string  $level
209
     *
210
     * @return string
211
     */
212
    protected function color($level)
213
    {
214
        return log_styler()->color($level);
215
    }
216
}
217