Completed
Push — master ( a684b1...82e6f8 )
by Michal
837:43 queued 773:20
created

CLI::runHighlight()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 21
ccs 0
cts 15
cp 0
crap 20
rs 9.0534
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * CLI interface
5
 *
6
 * @package    SqlParser
7
 * @subpackage Utils
8
 */
9
namespace SqlParser\Utils;
10
11
use SqlParser\Parser;
12
use SqlParser\Lexer;
13
14
/**
15
 * CLI interface
16
 *
17
 * @category   Exceptions
18
 * @package    SqlParser
19
 * @subpackage Utils
20
 * @author     Michal Čihař <[email protected]>
21
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
22
 */
23
class CLI
24
{
25
    public function mergeLongOpts(&$params, &$longopts)
26
    {
27
        foreach ($longopts as $value) {
28
            $value = rtrim($value, ':');
29
            if (isset($params[$value])) {
30
                $params[$value[0]] = $params[$value];
31
            }
32
        }
33
    }
34
35
    public function usageHighlight()
36
    {
37
        echo "Usage: highlight-query --query SQL [--format html|cli|text]\n";
38
    }
39
40
    public function getopt($opt, $long)
41
    {
42
        return getopt($opt, $long);
43
    }
44
45
    public function parseHighlight()
46
    {
47
        $longopts = array('help', 'query:', 'format:');
48
        $params = $this->getopt(
49
            'hq:f:', $longopts
50
        );
51
        $this->mergeLongOpts($params, $longopts);
52
        if (! isset($params['f'])) {
53
            $params['f'] = 'cli';
54
        }
55
        if (! in_array($params['f'], array('html', 'cli', 'text'))) {
56
            echo "ERROR: Invalid value for format!\n";
57
            return false;
58
        }
59
        return $params;
60
    }
61
62
    public function runHighlight()
63
    {
64
        $params = $this->parseHighlight();
65
        if ($params === false) {
66
            return 1;
67
        }
68
        if (isset($params['h'])) {
69
            $this->usageHighlight();
70
            return 0;
71
        }
72
        if (isset($params['q'])) {
73
            echo Formatter::format(
74
                $params['q'], array('type' => $params['f'])
75
            );
76
            echo "\n";
77
            return 0;
78
        }
79
        echo "ERROR: Missing parameters!\n";
80
        $this->usageHighlight();
81
        return 1;
82
    }
83
84
    public function usageLint()
85
    {
86
        echo "Usage: lint-query --query SQL\n";
87
    }
88
89
    public function parseLint()
90
    {
91
        $longopts = array('help', 'query:');
92
        $params = $this->getopt(
93
            'hq:', $longopts
94
        );
95
        $this->mergeLongOpts($params, $longopts);
96
        return $params;
97
    }
98
99
    public function runLint()
100
    {
101
        $params = $this->parseLint();
102
        if ($params === false) {
103
            return 1;
104
        }
105
        if (isset($params['h'])) {
106
            $this->usageLint();
107
            return 0;
108
        }
109
        if (isset($params['q'])) {
110
            $lexer = new Lexer($params['q'], false);
111
            $parser = new Parser($lexer->list);
112
            $errors = Error::get(array($lexer, $parser));
113
            if (count($errors) == 0) {
114
                return 0;
115
            }
116
            $output = Error::format($errors);
117
            echo implode("\n", $output);
118
            echo "\n";
119
            return 10;
120
        }
121
        echo "ERROR: Missing parameters!\n";
122
        $this->usageLint();
123
        return 1;
124
    }
125
}
126