1<?php 2 3namespace plugin\struct\meta; 4 5/** 6 * Class ConfigParser 7 * 8 * Utilities to parse the configuration syntax into an array 9 * 10 * @package plugin\struct\meta 11 */ 12class ConfigParser { 13 14 15 protected $config = array(); 16 17 /** 18 * Parser constructor. 19 * 20 * parses the given configuration lines 21 * 22 * @param $lines 23 */ 24 public function __construct($lines) { 25 /** @var \helper_plugin_struct_config $helper */ 26 $helper = plugin_load('helper','struct_config'); 27 $this->config = array( 28 'limit' => 0, 29 'dynfilters' => false, 30 'summarize' => false, 31 'rownumbers' => false, 32 'sepbyheaders' => false, 33 'headers' => array(), 34 'widths' => array(), 35 'filter' => array(), 36 'schemas' => array() 37 ); 38 // parse info 39 foreach($lines as $line) { 40 list($key, $val) = $this->splitLine($line); 41 if(!$key) continue; 42 43 $logic = 'OR'; 44 // handle line commands (we allow various aliases here) 45 switch($key) { 46 case 'from': 47 case 'schema': 48 case 'tables': 49 $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val)); 50 break; 51 case 'select': 52 case 'cols': 53 case 'field': 54 case 'col': 55 $this->config['cols'] = $this->parseValues($val); 56 break; 57 case 'title': 58 $this->config['title'] = $val; 59 break; 60 case 'head': 61 case 'header': 62 case 'headers': 63 $this->config['headers'] = $this->parseValues($val); 64 break; 65 case 'align': 66 $this->config['align'] = $this->parseAlignments($val); 67 break; 68 case 'widths': 69 $this->config['width'] = $this->parseValues($val); 70 break; 71 case 'min': 72 $this->config['min'] = abs((int) $val); 73 break; 74 case 'limit': 75 case 'max': 76 $this->config['limit'] = abs((int) $val); 77 break; 78 case 'order': 79 case 'sort': 80 // FIXME multiple values!? 81 $this->config['sort'] = $helper->parseSort($val); 82 break; 83 case 'where': 84 case 'filter': 85 case 'filterand': 86 /** @noinspection PhpMissingBreakStatementInspection */ 87 case 'and': 88 $logic = 'AND'; 89 case 'filteror': 90 case 'or': 91 $flt = $helper->parseFilterLine($logic, $val); 92 if ($flt) { 93 $this->config['filter'][] = $flt; 94 } 95 break; 96 case 'page': 97 case 'target': 98 $this->config['page'] = cleanID($val); 99 break; 100 case 'dynfilters': 101 $this->config['dynfilters'] = (bool) $val; 102 break; 103 case 'rownumbers': 104 $this->config['rownumbers'] = (bool) $val; 105 break; 106 case 'summarize': 107 $this->config['summarize'] = (bool) $val; 108 break; 109 case 'sepbyheaders': 110 $this->config['sepbyheaders'] = (bool) $val; 111 break; 112 default: 113 throw new StructException("unknown option '%s'", hsc($val)); 114 } 115 } 116 117 // fill up headers - a NULL signifies that the column label is wanted 118 $this->config['headers'] = (array) $this->config['headers']; 119 $cnth = count($this->config['headers']); 120 $cntf = count($this->config['cols']); 121 for($i = $cnth; $i < $cntf; $i++) { 122 $this->config['headers'][] = null; 123 } 124 } 125 126 /** 127 * Get the parsed configuration 128 * 129 * @return array 130 */ 131 public function getConfig() { 132 return $this->config; 133 } 134 135 /** 136 * Splits the given line into key and value 137 * 138 * @param $line 139 * @return bool|array returns false for empty lines 140 */ 141 protected function splitLine($line) { 142 // ignore comments 143 $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line); 144 $line = str_replace('\\#', '#', $line); 145 $line = trim($line); 146 if(empty($line)) return false; 147 148 $line = preg_split('/\s*:\s*/', $line, 2); 149 $line[0] = strtolower($line[0]); 150 151 return $line; 152 } 153 154 /** 155 * parses schema config and aliases 156 * 157 * @param $val 158 * @return array 159 */ 160 protected function parseSchema($val){ 161 $schemas = array(); 162 $parts = explode(',', $val); 163 foreach($parts as $part) { 164 list($table, $alias) = explode(' ', $part); 165 $table = trim($table); 166 $alias = trim($alias); 167 if(!$table) continue; 168 169 $schemas[] = array($table, $alias,); 170 } 171 return $schemas; 172 } 173 174 /** 175 * Parse alignment data 176 * 177 * @param string $val 178 * @return string[] 179 */ 180 protected function parseAlignments($val) { 181 $cols = explode(',', $val); 182 $data = array(); 183 foreach($cols as $col) { 184 $col = trim(strtolower($col)); 185 if($col[0] == 'c') { 186 $align = 'center'; 187 } elseif($col[0] == 'r') { 188 $align = 'right'; 189 } else { 190 $align = 'left'; 191 } 192 $data[] = $align; 193 } 194 195 return $data; 196 } 197 198 /** 199 * Split values at the commas, 200 * - Wrap with quotes to escape comma, quotes escaped by two quotes 201 * - Within quotes spaces are stored. 202 * 203 * @param string $line 204 * @return array 205 */ 206 protected function parseValues($line) { 207 $values = array(); 208 $inQuote = false; 209 $escapedQuote = false; 210 $value = ''; 211 $len = strlen($line); 212 for($i = 0; $i < $len; $i++) { 213 if($line{$i} == '"') { 214 if($inQuote) { 215 if($escapedQuote) { 216 $value .= '"'; 217 $escapedQuote = false; 218 continue; 219 } 220 if($line{$i + 1} == '"') { 221 $escapedQuote = true; 222 continue; 223 } 224 array_push($values, $value); 225 $inQuote = false; 226 $value = ''; 227 continue; 228 } else { 229 $inQuote = true; 230 $value = ''; //don't store stuff before the opening quote 231 continue; 232 } 233 } else if($line{$i} == ',') { 234 if($inQuote) { 235 $value .= ','; 236 continue; 237 } else { 238 if(strlen($value) < 1) { 239 continue; 240 } 241 array_push($values, trim($value)); 242 $value = ''; 243 continue; 244 } 245 } 246 $value .= $line{$i}; 247 } 248 if(strlen($value) > 0) { 249 array_push($values, trim($value)); 250 } 251 return $values; 252 } 253 254} 255