| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Parser; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\FileInterface; |
| 6 |
|
| 7 |
abstract class AbstractParser |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var \ImportWP\Common\Importer\FileInterface $file |
| 11 |
*/ |
| 12 |
protected $file; |
| 13 |
|
| 14 |
protected $record_index; |
| 15 |
protected $record; |
| 16 |
|
| 17 |
/** |
| 18 |
* Parser constructor. |
| 19 |
* |
| 20 |
* @param \ImportWP\Common\Importer\FileInterface $file |
| 21 |
*/ |
| 22 |
public function __construct(FileInterface $file) |
| 23 |
{ |
| 24 |
$this->file = $file; |
| 25 |
} |
| 26 |
|
| 27 |
public function getRecord($record_index = 0) |
| 28 |
{ |
| 29 |
|
| 30 |
if ($this->record_index !== $record_index) { |
| 31 |
$this->record_index = $record_index; |
| 32 |
$this->record = $this->file->getRecord($this->getRecordIndex()); |
| 33 |
$this->onRecordLoaded(); |
| 34 |
} |
| 35 |
|
| 36 |
return $this; |
| 37 |
} |
| 38 |
|
| 39 |
public function getRecordIndex() |
| 40 |
{ |
| 41 |
return $this->record_index; |
| 42 |
} |
| 43 |
|
| 44 |
abstract protected function onRecordLoaded(); |
| 45 |
|
| 46 |
public function queryGroup($group) |
| 47 |
{ |
| 48 |
$output = []; |
| 49 |
|
| 50 |
if (isset($group['fields'])) { |
| 51 |
|
| 52 |
// find files matching ._mapped._index |
| 53 |
|
| 54 |
$maps = []; |
| 55 |
foreach ($group['fields'] as $field_key => $field_value) { |
| 56 |
|
| 57 |
$matches = []; |
| 58 |
if ( |
| 59 |
// post.post_name._mapped._index |
| 60 |
// custom_fields.0.value._mapped._index |
| 61 |
preg_match('/^(.*?)\._mapped\._index$/', $field_key, $matches) === 1 && |
| 62 |
// ignore custom_fields.0._mapped._index old style FieldMapper |
| 63 |
preg_match('/^custom_fields\.(?:[0-9]+)\._mapped\._index$/', $field_key) === 0 && |
| 64 |
intval($field_value) > 0 |
| 65 |
) { |
| 66 |
$maps[$matches[1]] = $field_value; |
| 67 |
} else { |
| 68 |
$output[$field_key] = $this->query_string($field_value); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if (!empty($maps)) { |
| 74 |
foreach ($maps as $field_key => $field_rows) { |
| 75 |
|
| 76 |
$data = []; |
| 77 |
$delimiter = false; |
| 78 |
|
| 79 |
if (isset($output[$field_key . '._mapped._delimiter']) && !empty($output[$field_key . '._mapped._delimiter'])) { |
| 80 |
|
| 81 |
// get delimiter from FieldMap delimiter field, this has priority |
| 82 |
$delimiter = $output[$field_key . '._mapped._delimiter']; |
| 83 |
} else { |
| 84 |
|
| 85 |
// get delimiter from parent section settings. e.g. taxonomies and attachments |
| 86 |
$lastPos = strrpos($field_key, '.'); |
| 87 |
if ($lastPos !== false) { |
| 88 |
$tmp = substr($field_key, 0, $lastPos); |
| 89 |
if (isset($output[$tmp . '.settings._delimiter'])) { |
| 90 |
$delimiter = !empty($output[$tmp . '.settings._delimiter']) ? $output[$tmp . '.settings._delimiter'] : ','; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
foreach ($output as $item_key => $item_value) { |
| 96 |
$matches = []; |
| 97 |
if (preg_match('/^' . $field_key . '\._mapped\.([0-9]+)\.(.*?)$/', $item_key, $matches) === 1) { |
| 98 |
|
| 99 |
// row is to high |
| 100 |
if (intval($matches[1]) >= intval($field_rows)) { |
| 101 |
|
| 102 |
unset($output[$matches[0]]); |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
if (!isset($data[$matches[1]])) { |
| 107 |
$data[$matches[1]] = []; |
| 108 |
} |
| 109 |
|
| 110 |
$data[$matches[1]][$matches[2]] = $item_value; |
| 111 |
|
| 112 |
unset($output[$matches[0]]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if (!empty($delimiter)) { |
| 117 |
|
| 118 |
$field_parts = explode($delimiter, $output[$field_key]); |
| 119 |
foreach ($field_parts as $k => $field_part) { |
| 120 |
$field_parts[$k] = $this->map_field_data($field_part, array_values($data)); |
| 121 |
} |
| 122 |
$output[$field_key] = implode($delimiter, $field_parts); |
| 123 |
} else { |
| 124 |
$output[$field_key] = $this->map_field_data($output[$field_key], array_values($data)); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return $output; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Parse Query String for {} run query on them |
| 134 |
* |
| 135 |
* @param string $query |
| 136 |
* |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
public function query_string($query) |
| 140 |
{ |
| 141 |
|
| 142 |
$output = preg_replace_callback('/{(.*?)}/', array($this, 'query_matches'), $query); |
| 143 |
$output = $this->handle_custom_methods($output); |
| 144 |
|
| 145 |
return $output; |
| 146 |
} |
| 147 |
|
| 148 |
public function query_matches($matches) |
| 149 |
{ |
| 150 |
if (isset($matches[0]) && isset($matches[1])) { |
| 151 |
return $this->query($matches[1]); |
| 152 |
} |
| 153 |
|
| 154 |
return ''; |
| 155 |
} |
| 156 |
|
| 157 |
abstract public function query($query); |
| 158 |
|
| 159 |
public function file() |
| 160 |
{ |
| 161 |
return $this->file; |
| 162 |
} |
| 163 |
|
| 164 |
public function handle_custom_methods($input) |
| 165 |
{ |
| 166 |
// m: Multiline modifier |
| 167 |
// s: matches all characters including newlines |
| 168 |
$input = preg_replace_callback('/\[([\w]+)\((.*?)\)\]/ms', function ($matches) { |
| 169 |
|
| 170 |
$method = $matches[1]; |
| 171 |
|
| 172 |
$result = []; |
| 173 |
$args = []; |
| 174 |
|
| 175 |
// Dont split comma's if they are inside a double quote |
| 176 |
if (preg_match_all('/(?:".*?"|[^",\s]+)(?=\s*,|\s*$)/s', $matches[2], $result) > 0) { |
| 177 |
$args = $result[0]; |
| 178 |
foreach ($args as &$arg) { |
| 179 |
|
| 180 |
// Strip commas from start and end of string |
| 181 |
$arg = preg_replace('/^(\'(.*)\'|"(.*)")$/s', '$2$3', $arg); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if (is_callable($method)) { |
| 186 |
return call_user_func_array($method, $args); |
| 187 |
} |
| 188 |
|
| 189 |
return $matches[0]; |
| 190 |
}, $input); |
| 191 |
return $input; |
| 192 |
} |
| 193 |
|
| 194 |
public function map_field_data($input, $map) |
| 195 |
{ |
| 196 |
foreach ($map as $map_data) { |
| 197 |
|
| 198 |
$condition = isset($map_data['_condition']) && !empty($map_data['_condition']) ? $map_data['_condition'] : 'equal'; |
| 199 |
$key = isset($map_data['key']) ? $map_data['key'] : ''; |
| 200 |
$output = isset($map_data['value']) ? $map_data['value'] : ''; |
| 201 |
|
| 202 |
switch ($condition) { |
| 203 |
|
| 204 |
case 'gt': |
| 205 |
$left = intval($input); |
| 206 |
$right = intval($key); |
| 207 |
if ($left > $right) { |
| 208 |
return $output; |
| 209 |
} |
| 210 |
break; |
| 211 |
case 'gte': |
| 212 |
$left = intval($input); |
| 213 |
$right = intval($key); |
| 214 |
if ($left >= $right) { |
| 215 |
return $output; |
| 216 |
} |
| 217 |
break; |
| 218 |
case 'lt': |
| 219 |
$left = intval($input); |
| 220 |
$right = intval($key); |
| 221 |
if ($left < $right) { |
| 222 |
return $output; |
| 223 |
} |
| 224 |
break; |
| 225 |
case 'lte': |
| 226 |
$left = intval($input); |
| 227 |
$right = intval($key); |
| 228 |
if ($left <= $right) { |
| 229 |
return $output; |
| 230 |
} |
| 231 |
break; |
| 232 |
case 'contains': |
| 233 |
if (stripos($input, trim($key)) !== false) { |
| 234 |
return $output; |
| 235 |
} |
| 236 |
break; |
| 237 |
case 'in': |
| 238 |
if (in_array($input, explode(',', $key))) { |
| 239 |
return $output; |
| 240 |
} |
| 241 |
break; |
| 242 |
case 'not-equal': |
| 243 |
if (trim($key) !== trim($input)) { |
| 244 |
return $output; |
| 245 |
} |
| 246 |
break; |
| 247 |
case 'not-contains': |
| 248 |
if (stripos($input, trim($key)) === false) { |
| 249 |
return $output; |
| 250 |
} |
| 251 |
break; |
| 252 |
case 'not-in': |
| 253 |
if (!in_array($input, explode(',', $key))) { |
| 254 |
return $output; |
| 255 |
} |
| 256 |
break; |
| 257 |
default: |
| 258 |
if (trim($key) === trim($input)) { |
| 259 |
return $output; |
| 260 |
} |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
return $input; |
| 265 |
} |
| 266 |
} |
| 267 |
|