Completed
Push — master ( 76ebaf...c69b97 )
by ARCANEDEV
11:16
created

LogParser::populateEntries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
1
<?php namespace Arcanedev\LogViewer\Helpers;
2
3
use Arcanedev\LogViewer\Utilities\LogLevels;
4
5
/**
6
 * Class     LogParser
7
 *
8
 * @package  Arcanedev\LogViewer\Helpers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class LogParser
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Parsed data.
19
     *
20
     * @var array
21
     */
22
    protected static $parsed = [];
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Main Functions
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Parse file content.
30
     *
31
     * @param  string  $raw
32
     *
33
     * @return array
34
     */
35
    public static function parse($raw)
36
    {
37
        self::$parsed          = [];
38
        list($headings, $data) = self::parseRawData($raw);
39
40
        // @codeCoverageIgnoreStart
41
        if ( ! is_array($headings)) {
42
            return self::$parsed;
43
        }
44
        // @codeCoverageIgnoreEnd
45
46
        foreach ($headings as $heading) {
47
            for ($i = 0, $j = count($heading); $i < $j; $i++) {
48
                self::populateEntries($heading, $data, $i);
49
            }
50
        };
51
52
        unset($headings, $data);
53
54
        return array_reverse(self::$parsed);
55
    }
56
57
    /* ------------------------------------------------------------------------------------------------
58
     |  Other Functions
59
     | ------------------------------------------------------------------------------------------------
60
     */
61
    /**
62
     * Parse raw data.
63
     *
64
     * @param  string  $raw
65
     *
66
     * @return array
67
     */
68
    private static function parseRawData($raw)
69
    {
70
        $pattern = '/\['.REGEX_DATE_PATTERN.' '.REGEX_TIME_PATTERN.'\].*/';
71
        preg_match_all($pattern, $raw, $headings);
72
        $data    = preg_split($pattern, $raw);
73
74
        if ($data[0] < 1) {
75
            $trash = array_shift($data);
76
            unset($trash);
77
        }
78
79
        return [$headings, $data];
80
    }
81
82
    /**
83
     * Populate entries.
84
     *
85
     * @param  array  $heading
86
     * @param  array  $data
87
     * @param  int    $key
88
     */
89
    private static function populateEntries($heading, $data, $key)
90
    {
91
        foreach (LogLevels::all() as $level) {
92
            if (self::hasLogLevel($heading[$key], $level)) {
93
                self::$parsed[] = [
94
                    'level'  => $level,
95
                    'header' => $heading[$key],
96
                    'stack'  => $data[$key]
97
                ];
98
            }
99
        }
100
    }
101
102
    /**
103
     * Check if header has a log level.
104
     *
105
     * @param  string  $heading
106
     * @param  string  $level
107
     *
108
     * @return bool
109
     */
110
    private static function hasLogLevel($heading, $level)
111
    {
112
        return str_contains(strtolower($heading), strtolower('.' . $level));
113
    }
114
}
115