Completed
Push — master ( 4605fd...f7c259 )
by ARCANEDEV
04:49
created

LogEntry::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanedev\LogViewer\Entities;
2
3
use Carbon\Carbon;
4
use Illuminate\Contracts\Support\Arrayable;
5
use Illuminate\Contracts\Support\Jsonable;
6
use JsonSerializable;
7
8
/**
9
 * Class     LogEntry
10
 *
11
 * @package  Arcanedev\LogViewer\Entities
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class LogEntry implements Arrayable, Jsonable, JsonSerializable
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /** @var string */
21
    public $env;
22
23
    /** @var string */
24
    public $level;
25
26
    /** @var \Carbon\Carbon */
27
    public $datetime;
28
29
    /** @var string */
30
    public $header;
31
32
    /** @var string */
33
    public $stack;
34
35
    /* ------------------------------------------------------------------------------------------------
36
     |  Constructor
37
     | ------------------------------------------------------------------------------------------------
38
     */
39
    /**
40
     * Construct the log entry instance.
41
     *
42
     * @param  string  $level
43
     * @param  string  $header
44
     * @param  string  $stack
45
     */
46 864
    public function __construct($level, $header, $stack)
47
    {
48 864
        $this->setLevel($level);
49 864
        $this->setHeader($header);
50 864
        $this->setStack($stack);
51 864
    }
52
53
    /* ------------------------------------------------------------------------------------------------
54
     |  Getters & Setters
55
     | ------------------------------------------------------------------------------------------------
56
     */
57
    /**
58
     * Set the entry level.
59
     *
60
     * @param  string  $level
61
     *
62
     * @return self
63
     */
64 864
    private function setLevel($level)
65
    {
66 864
        $this->level = $level;
67
68 864
        return $this;
69
    }
70
71
    /**
72
     * Set the entry header.
73
     *
74
     * @param  string  $header
75
     *
76
     * @return self
77
     */
78 864
    private function setHeader($header)
79
    {
80 864
        $this->setDatetime($this->extractDatetime($header));
81
82 864
        $header = $this->cleanHeader($header);
83
84 864
        if (preg_match('/^[a-z]+.[A-Z]+:/', $header, $out)) {
85 864
            $this->setEnv($out[0]);
86 864
            $header = trim(str_replace($out[0], '', $header));
87 648
        }
88
89 864
        $this->header = $header;
90
91 864
        return $this;
92
    }
93
94
    /**
95
     * Set entry environment.
96
     *
97
     * @param  string  $env
98
     *
99
     * @return self
100
     */
101 864
    private function setEnv($env)
102
    {
103 864
        $this->env = head(explode('.', $env));
104
105 864
        return $this;
106
    }
107
108
    /**
109
     * Set the entry date time.
110
     *
111
     * @param  string  $datetime
112
     *
113
     * @return \Arcanedev\LogViewer\Entities\LogEntry
114
     */
115 864
    private function setDatetime($datetime)
116
    {
117 864
        $this->datetime = Carbon::createFromFormat('Y-m-d H:i:s', $datetime);
118 864
119
        return $this;
120 648
    }
121
122 864
    /**
123
     * Set the entry stack.
124
     *
125
     * @param  string  $stack
126
     *
127
     * @return self
128
     */
129
    private function setStack($stack)
130
    {
131
        $this->stack = $stack;
132 864
133
        return $this;
134 864
    }
135
136 864
    /**
137
     * Get translated level name with icon.
138
     *
139
     * @return string
140
     */
141
    public function level()
142
    {
143
        return $this->icon().' '.$this->name();
144 24
    }
145
146 24
    /**
147
     * Get translated level name.
148
     *
149
     * @return string
150
     */
151
    public function name()
152
    {
153
        return log_levels()->get($this->level);
154 24
    }
155
156 24
    /**
157
     * Get level icon.
158
     *
159
     * @return string
160
     */
161
    public function icon()
162
    {
163
        return log_styler()->icon($this->level);
164 24
    }
165
166 24
    /**
167
     * Get the entry stack.
168
     *
169
     * @return string
170
     */
171
    public function stack()
172
    {
173
        return nl2br(htmlentities($this->stack), false);
174
    }
175
176
    /* ------------------------------------------------------------------------------------------------
177
     |  Check Functions
178
     | ------------------------------------------------------------------------------------------------
179
     */
180 96
    /**
181
     * Check if same log level.
182 96
     *
183
     * @param  string  $level
184
     *
185
     * @return bool
186
     */
187
    public function isSameLevel($level)
188
    {
189
        return $this->level === $level;
190
    }
191
192
    /* ------------------------------------------------------------------------------------------------
193
     |  Convert Functions
194 24
     | ------------------------------------------------------------------------------------------------
195
     */
196
    /**
197 24
     * Get the log entry as an array.
198 24
     *
199 24
     * @return array
200 24
     */
201 18
    public function toArray()
202
    {
203
        return [
204
            'level'     => $this->level,
205
            'datetime'  => $this->datetime->format('Y-m-d H:i:s'),
206
            'header'    => $this->header,
207
            'stack'     => $this->stack
208
        ];
209
    }
210
211 12
    /**
212
     * Convert the log entry to its JSON representation.
213 12
     *
214
     * @param  int  $options
215
     *
216
     * @return string
217
     */
218
    public function toJson($options = 0)
219
    {
220
        return json_encode($this->toArray(), $options);
221 12
    }
222
223 12
    /**
224
     * Serialize the log entry object to json data.
225
     *
226
     * @return array
227
     */
228
    public function jsonSerialize()
229
    {
230
        return $this->toArray();
231
    }
232
233
    /* ------------------------------------------------------------------------------------------------
234
     |  Check Functions
235 24
     | ------------------------------------------------------------------------------------------------
236
     */
237 24
    /**
238
     * Check if the entry has a stack.
239
     *
240
     * @return bool
241
     */
242
    public function hasStack()
243
    {
244
        return $this->stack !== "\n";
245
    }
246
247
    /* ------------------------------------------------------------------------------------------------
248
     |  Other Functions
249
     | ------------------------------------------------------------------------------------------------
250
     */
251 864
    /**
252
     * Clean the entry header.
253 864
     *
254
     * @param  string  $header
255
     *
256
     * @return string
257
     */
258
    private function cleanHeader($header)
259
    {
260
        return preg_replace('/\[' . REGEX_DATETIME_PATTERN . '\][ ]/', '', $header);
261
    }
262
263 864
    /**
264
     * Extract datetime from the header.
265 864
     *
266
     * @param  string  $header
267
     *
268
     * @return string
269
     */
270
    private function extractDatetime($header)
271
    {
272
        return preg_replace('/^\[(' . REGEX_DATETIME_PATTERN . ')\].*/', '$1', $header);
273
    }
274
}
275