Completed
Push — master ( 19af7f...765e83 )
by ARCANEDEV
14s queued 11s
created

AbstractTable::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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