]> git.openstreetmap.org Git - nominatim.git/blob - lib/DebugHtml.php
add PHPCS Squiz.Arrays.ArrayDeclaration.KeyNotAligned rule
[nominatim.git] / lib / DebugHtml.php
1 <?php
2
3 namespace Nominatim;
4
5 class Debug
6 {
7     public static function newFunction($sHeading)
8     {
9         echo "<pre><h2>Debug output for $sHeading</h2></pre>\n";
10     }
11
12     public static function newSection($sHeading)
13     {
14         echo "<hr><pre><h3>$sHeading</h3></pre>\n";
15     }
16
17     public static function printVar($sHeading, $mVar)
18     {
19         echo '<pre><b>'.$sHeading. ':</b>  ';
20         Debug::outputVar($mVar, str_repeat(' ', strlen($sHeading) + 3));
21         echo "</pre>\n";
22     }
23
24     public static function fmtArrayVals($aArr)
25     {
26         return array('__debug_format' => 'array_vals', 'data' => $aArr);
27     }
28
29     public static function printDebugArray($sHeading, $oVar)
30     {
31
32         if ($oVar === null) {
33             Debug::printVar($sHeading, 'null');
34         } else {
35             Debug::printVar($sHeading, $oVar->debugInfo());
36         }
37     }
38
39     public static function printDebugTable($sHeading, $aVar)
40     {
41         echo '<b>'.$sHeading.":</b>\n";
42         echo "<table border='1'>\n";
43         if (!empty($aVar)) {
44             echo "  <tr>\n";
45             $aKeys = array();
46             $aInfo = reset($aVar);
47             if (!is_array($aInfo)) {
48                 $aInfo = $aInfo->debugInfo();
49             }
50             foreach ($aInfo as $sKey => $mVal) {
51                 echo '    <th><small>'.$sKey.'</small></th>'."\n";
52                 $aKeys[] = $sKey;
53             }
54             echo "  </tr>\n";
55             foreach ($aVar as $oRow) {
56                 $aInfo = $oRow;
57                 if (!is_array($oRow)) {
58                     $aInfo = $oRow->debugInfo();
59                 }
60                 echo "  <tr>\n";
61                 foreach ($aKeys as $sKey) {
62                     echo '    <td><pre>';
63                     if (isset($aInfo[$sKey])) {
64                         Debug::outputVar($aInfo[$sKey], '');
65                     }
66                     echo '</pre></td>'."\n";
67                 }
68                 echo "  </tr>\n";
69             }
70         }
71         echo "</table>\n";
72     }
73
74     public static function printGroupTable($sHeading, $aVar)
75     {
76         echo '<b>'.$sHeading.":</b>\n";
77         echo "<table border='1'>\n";
78         if (!empty($aVar)) {
79             echo "  <tr>\n";
80             echo '    <th><small>Group</small></th>'."\n";
81             $aKeys = array();
82             $aInfo = reset($aVar)[0];
83             if (!is_array($aInfo)) {
84                 $aInfo = $aInfo->debugInfo();
85             }
86             foreach ($aInfo as $sKey => $mVal) {
87                 echo '    <th><small>'.$sKey.'</small></th>'."\n";
88                 $aKeys[] = $sKey;
89             }
90             echo "  </tr>\n";
91             foreach ($aVar as $sGrpKey => $aGroup) {
92                 foreach ($aGroup as $oRow) {
93                     $aInfo = $oRow;
94                     if (!is_array($oRow)) {
95                         $aInfo = $oRow->debugInfo();
96                     }
97                     echo "  <tr>\n";
98                     echo '    <td><pre>'.$sGrpKey.'</pre></td>'."\n";
99                     foreach ($aKeys as $sKey) {
100                         echo '    <td><pre>';
101                         if (!empty($aInfo[$sKey])) {
102                             Debug::outputVar($aInfo[$sKey], '');
103                         }
104                         echo '</pre></td>'."\n";
105                     }
106                     echo "  </tr>\n";
107                 }
108             }
109         }
110         echo "</table>\n";
111     }
112
113     public static function printSQL($sSQL)
114     {
115         echo '<p><tt><font color="#aaa">'.$sSQL.'</font></tt></p>'."\n";
116     }
117
118     private static function outputVar($mVar, $sPreNL)
119     {
120         if (is_array($mVar) && !isset($mVar['__debug_format'])) {
121             $sPre = '';
122             foreach ($mVar as $mKey => $aValue) {
123                 echo $sPre;
124                 $iKeyLen = Debug::outputSimpleVar($mKey);
125                 echo ' => ';
126                 Debug::outputVar(
127                     $aValue,
128                     $sPreNL.str_repeat(' ', $iKeyLen + 4)
129                 );
130                 $sPre = "\n".$sPreNL;
131             }
132         } elseif (is_array($mVar) && isset($mVar['__debug_format'])) {
133             if (!empty($mVar['data'])) {
134                 $sPre = '';
135                 foreach ($mVar['data'] as $mValue) {
136                     echo $sPre;
137                     Debug::outputSimpleVar($mValue);
138                     $sPre = ', ';
139                 }
140             }
141         } else {
142             Debug::outputSimpleVar($mVar);
143         }
144     }
145
146     private static function outputSimpleVar($mVar)
147     {
148         if (is_bool($mVar)) {
149             echo '<i>'.($mVar ? 'True' : 'False').'</i>';
150             return $mVar ? 4 : 5;
151         }
152
153         if (is_string($mVar)) {
154             echo "'$mVar'";
155             return strlen($mVar) + 2;
156         }
157
158         echo (string)$mVar;
159         return strlen((string)$mVar);
160     }
161 }