summaryrefslogtreecommitdiffstats
path: root/src/Utils/Error.php
blob: a9c0814fcfcde4b8a85091c580bcc178298e2177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

/**
 * Error related utilities.
 *
 * @package    SqlParser
 * @subpackage Utils
 */
namespace SqlParser\Utils;

use SqlParser\Lexer;
use SqlParser\Parser;

/**
 * Error related utilities.
 *
 * @category   Exceptions
 * @package    SqlParser
 * @subpackage Utils
 * @author     Dan Ungureanu <udan1107@gmail.com>
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
 */
class Error
{

    /**
     * Gets the errors of a lexer and a parser.
     *
     * @param array $objs Objects from where the errors will be extracted.
     *
     * @return array Each element of the array represents an error.
     *                    `$err[0]` holds the error message.
     *                    `$err[1]` holds the error code.
     *                    `$err[2]` holds the string that caused the issue.
     *                    `$err[3]` holds the position of the string.
     *                    (i.e. `array($msg, $code, $str, $pos)`)
     */
    public static function get($objs)
    {
        $ret = array();

        foreach ($objs as $obj) {
            if ($obj instanceof Lexer) {
                foreach ($obj->errors as $err) {
                    $ret[] = array(
                        $err->getMessage(),
                        $err->getCode(),
                        $err->ch,
                        $err->pos
                    );
                }
            } elseif ($obj instanceof Parser) {
                foreach ($obj->errors as $err) {
                    $ret[] = array(
                        $err->getMessage(),
                        $err->getCode(),
                        $err->token->token,
                        $err->token->position
                    );
                }
            }
        }

        return $ret;
    }

    /**
     * Formats the specified errors
     *
     * @param  array  $errors The errors to be formatted.
     * @param  string $format The format of an error.
     *                         '$1$d' is replaced by the position of this error.
     *                         '$2$s' is replaced by the error message.
     *                         '$3$d' is replaced by the error code.
     *                         '$4$s' is replaced by the string that caused the
     *                                issue.
     *                         '$5$d' is replaced by the position of the string.
     * @return array
     */
    public static function format(
        $errors,
        $format = '#%1$d: %2$s (near "%4$s" at position %5$d)'
    ) {
        $ret = array();

        $i = 0;
        foreach ($errors as $key => $err) {
            $ret[$key] = sprintf(
                $format,
                ++$i,
                $err[0],
                $err[1],
                $err[2],
                $err[3]
            );
        }

        return $ret;
    }
}