Completed
Push — master ( c258b5...8956cf )
by Michal
03:45
created

CLI::parseHighlight()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 11
nc 4
nop 0
crap 12
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 parseHighlight()
41
    {
42
        $longopts = array('help', 'query:', 'format:');
43
        $params = getopt(
44
            'hq:f:', $longopts
45
        );
46
        $this->mergeLongOpts($params, $longopts);
47
        if (! isset($params['f'])) {
48
            $params['f'] = 'cli';
49
        }
50
        if (! in_array($params['f'], array('html', 'cli', 'text'))) {
51
            echo "ERROR: Invalid value for format!\n";
52
            return false;
53
        }
54
        return $params;
55
    }
56
57
    public function runHighlight()
58
    {
59
        $params = $this->parseHighlight();
60
        if ($params === false) {
61
            return 1;
62
        }
63
        if (isset($params['h'])) {
64
            $this->usageHighlight();
65
            return 0;
66
        }
67
        if (isset($params['q'])) {
68
            echo Formatter::format(
69
                $params['q'], array('type' => $params['f'])
70
            );
71
            echo "\n";
72
            return 0;
73
        }
74
        echo "ERROR: Missing parameters!\n";
75
        $this->usageHighlight();
76
        return 1;
77
    }
78
79
    public function usageLint()
80
    {
81
        echo "Usage: lint-query --query SQL\n";
82
    }
83
84
    public function parseLint()
85
    {
86
        $longopts = array('help', 'query:');
87
        $params = getopt(
88
            'hq:', $longopts
89
        );
90
        $this->mergeLongOpts($params, $longopts);
91
        return $params;
92
    }
93
94
    public function runLint()
95
    {
96
        $params = $this->parseLint();
97
        if ($params === false) {
98
            return 1;
99
        }
100
        if (isset($params['h'])) {
101
            $this->usageLint();
102
            return 0;
103
        }
104
        if (isset($params['q'])) {
105
            $lexer = new Lexer($params['q'], false);
106
            $parser = new Parser($lexer->list);
107
            $errors = Error::get(array($lexer, $parser));
108
            if (count($errors) == 0) {
109
                return 0;
110
            }
111
            $output = Error::format($errors);
112
            echo implode("\n", $output);
113
            echo "\n";
114
            return 10;
115
        }
116
        echo "ERROR: Missing parameters!\n";
117
        $this->usageLint();
118
        return 1;
119
    }
120
}
121