Add documentation on installing via composer
[php-sqllint.git] / src / phpsqllint / Renderer / Text.php
1 <?php
2 /**
3  * Part of php-sqllint
4  *
5  * PHP version 5
6  *
7  * @category Tools
8  * @package  PHP-SQLlint
9  * @author   Christian Weiske <cweiske@cweiske.de>
10  * @license  http://www.gnu.org/licenses/agpl.html GNU AGPL v3
11  * @link     http://cweiske.de/php-sqllint.htm
12  */
13 namespace phpsqllint;
14
15 /**
16  * Textual output, easily readable by humans.
17  *
18  * @category Tools
19  * @package  PHP-SQLlint
20  * @author   Christian Weiske <cweiske@cweiske.de>
21  * @license  http://www.gnu.org/licenses/agpl.html GNU AGPL v3
22  * @link     http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
23  */
24 class Renderer_Text implements Renderer
25 {
26     protected $fileshown = false;
27     protected $filename = null;
28
29     /**
30      * Begin syntax check output rendering
31      *
32      * @param string $filename Path to the SQL file
33      *
34      * @return void
35      */
36     public function startRendering($filename)
37     {
38         $this->filename  = $filename;
39         $this->fileshown = false;
40     }
41
42
43     protected function showFile()
44     {
45         if ($this->fileshown) {
46             return;
47         }
48
49         echo "Checking SQL syntax of " . $this->filename . "\n";
50         $this->fileshown = true;
51     }
52
53     /**
54      * Show the error to the user.
55      *
56      * @param string  $msg   Error message
57      * @param string  $token Character which caused the error
58      * @param integer $line  Line at which the error occured
59      * @param integer $col   Column at which the error occured
60      *
61      * @return void
62      */
63     public function displayError($msg, $token, $line, $col)
64     {
65         $this->showFile();
66         echo ' Line ' . $line
67             . ', col ' . $col
68             . ' at "' . $this->niceToken($token) . '":'
69             . ' ' . $msg
70             . "\n";
71     }
72
73     /**
74      * Finish syntax check output rendering; no syntax errors found
75      *
76      * @return void
77      */
78     public function finishOk()
79     {
80         if ($this->fileshown) {
81             echo " OK\n";
82         }
83     }
84
85     /**
86      * Convert the token string to a readable one, especially special
87      * characters like newline and tabs
88      *
89      * @param string $str String with possibly special characters
90      *
91      * @return string Escaped string
92      */
93     protected function niceToken($str)
94     {
95         return str_replace(
96             ["\n", "\r", "\t"],
97             ['\n', '\r', '\t'],
98             $str
99         );
100     }
101 }
102 ?>