Completed
Pull Request — master (#313)
by
unknown
13:07
created

LogEntry::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 1
rs 9.9666
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
21
    /** @var string */
22
    public $env;
23
24
    /** @var string */
25
    public $level;
26
27
    /** @var \Carbon\Carbon */
28
    public $datetime;
29
30
    /** @var string */
31
    public $header;
32
33
    /** @var string */
34
    public $context;
35
36
    /** @var string */
37
    public $stack;
38
39
    /* -----------------------------------------------------------------
40
     |  Constructor
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Construct the log entry instance.
46
     *
47
     * @param  string  $level
48 124
     * @param  string  $header
49
     * @param  string  $stack
50 124
     */
51 124
    public function __construct($level, $header, $stack)
52 124
    {
53 124
        $this->setLevel($level);
54
        $this->setHeaderAndContext($header);
55
        $this->setStack($stack);
56
    }
57
58
    /* -----------------------------------------------------------------
59
     |  Getters & Setters
60
     | -----------------------------------------------------------------
61
     */
62
63
    /**
64
     * Set the entry level.
65
     *
66
     * @param  string  $level
67 124
     *
68
     * @return self
69 124
     */
70
    private function setLevel($level)
71 124
    {
72
        $this->level = $level;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Set the entry header.
79
     *
80
     * @param  string  $header
81 124
     *
82
     * @return self
83 124
     */
84
    private function setHeaderAndContext($header)
85 124
    {
86
        $this->setDatetime($this->extractDatetime($header));
87 124
88 124
        $header = $this->cleanHeader($header);
89 124
90
        if (preg_match('/^[a-z]+.[A-Z]+:/', $header, $out)) {
91
            $this->setEnv($out[0]);
92 124
            $header = trim(str_replace($out[0], '', $header));
93
        }
94 124
95
        $this->header = $header;
96
97
        $this->context = $this->extractContext($header);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->extractContext($header) of type object is incompatible with the declared type string of property $context.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
99
        return $this;
100
    }
101
102
    /**
103
     * Set entry environment.
104 124
     *
105
     * @param  string  $env
106 124
     *
107
     * @return self
108 124
     */
109
    private function setEnv($env)
110
    {
111
        $this->env = head(explode('.', $env));
112
113
        return $this;
114
    }
115
116
    /**
117
     * Set the entry date time.
118 124
     *
119
     * @param  string  $datetime
120 124
     *
121
     * @return \Arcanedev\LogViewer\Entities\LogEntry
122 124
     */
123
    private function setDatetime($datetime)
124
    {
125
        $this->datetime = Carbon::createFromFormat('Y-m-d H:i:s', $datetime);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Set the entry stack.
132 124
     *
133
     * @param  string  $stack
134 124
     *
135
     * @return self
136 124
     */
137
    private function setStack($stack)
138
    {
139
        $this->stack = $stack;
140
141
        return $this;
142
    }
143
144 12
    /**
145
     * Get translated level name with icon.
146 12
     *
147
     * @return string
148
     */
149
    public function level()
150
    {
151
        return $this->icon()->toHtml().' '.$this->name();
152
    }
153
154 12
    /**
155
     * Get translated level name.
156 12
     *
157
     * @return string
158
     */
159
    public function name()
160
    {
161
        return log_levels()->get($this->level);
162
    }
163
164 12
    /**
165
     * Get level icon.
166 12
     *
167
     * @return \Illuminate\Support\HtmlString
168
     */
169
    public function icon()
170
    {
171
        return log_styler()->icon($this->level);
172
    }
173
174 14
    /**
175
     * Get the entry stack.
176 14
     *
177
     * @return string
178
     */
179
    public function stack()
180
    {
181
        return trim(htmlentities($this->stack));
182
    }
183
184
    /* -----------------------------------------------------------------
185
     |  Check Methods
186
     | -----------------------------------------------------------------
187
     */
188
189
    /**
190
     * Check if same log level.
191 18
     *
192
     * @param  string  $level
193 18
     *
194
     * @return bool
195
     */
196
    public function isSameLevel($level)
197
    {
198
        return $this->level === $level;
199
    }
200
201
    /* -----------------------------------------------------------------
202
     |  Convert Methods
203
     | -----------------------------------------------------------------
204
     */
205
206 4
    /**
207
     * Get the log entry as an array.
208
     *
209 4
     * @return array
210 4
     */
211 4
    public function toArray()
212 4
    {
213
        return [
214
            'level'    => $this->level,
215
            'datetime' => $this->datetime->format('Y-m-d H:i:s'),
216
            'header'   => $this->header,
217
            'stack'    => $this->stack
218
        ];
219
    }
220
221
    /**
222
     * Convert the log entry to its JSON representation.
223 2
     *
224
     * @param  int  $options
225 2
     *
226
     * @return string
227
     */
228
    public function toJson($options = 0)
229
    {
230
        return json_encode($this->toArray(), $options);
231
    }
232
233 2
    /**
234
     * Serialize the log entry object to json data.
235 2
     *
236
     * @return array
237
     */
238
    public function jsonSerialize()
239
    {
240
        return $this->toArray();
241
    }
242
243
    /* -----------------------------------------------------------------
244
     |  Check Methods
245
     | -----------------------------------------------------------------
246
     */
247
248 12
    /**
249
     * Check if the entry has a context.
250 12
     *
251
     * @return bool
252
     */
253
    public function hasContext()
254
    {
255
        return !is_null($this->context);
256
    }
257
    
258
    /**
259
     * Check if the entry has a stack.
260
     *
261
     * @return bool
262
     */
263
    public function hasStack()
264
    {
265 124
        return $this->stack !== "\n";
266
    }
267 124
268
    /* -----------------------------------------------------------------
269
     |  Other Methods
270
     | -----------------------------------------------------------------
271
     */
272
273
    /**
274
     * Clean the entry header.
275
     *
276
     * @param  string  $header
277 124
     *
278
     * @return string
279 124
     */
280
    private function cleanHeader($header)
281
    {
282
        return preg_replace('/\['.REGEX_DATETIME_PATTERN.'\][ ]/', '', $header);
283
    }
284
285
    /**
286
     * Extract context from the header.
287
     *
288
     * @param  string  $header
289
     *
290
     * @return object
291
     */
292
    private function extractContext($header)
293
    {
294
        // Regex from https://stackoverflow.com/a/21995025
295
        $pattern = '/\{(?:[^{}]|(?R))*\}/x';
296
        $context = null;
297
        preg_match_all($pattern, $header, $matches);
298
        if(!empty($matches) && !empty($matches[0])){
299
            $context = json_decode($matches[0][0]);
300
            if(!is_null($context)){
301
                $this->header = str_replace($matches[0][0], '', $this->header);
302
            }
303
        }
304
        return $context;
305
    }
306
307
    /**
308
     * Extract datetime from the header.
309
     *
310
     * @param  string  $header
311
     *
312
     * @return string
313
     */
314
    private function extractDatetime($header)
315
    {
316
        return preg_replace('/^\[('.REGEX_DATETIME_PATTERN.')\].*/', '$1', $header);
317
    }
318
}
319