| 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 |
foreach ($group['fields'] as $field_key => $field_value) { |
| 52 |
$output[$field_key] = $this->query_string($field_value); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $output; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Parse Query String for {} run query on them |
| 61 |
* |
| 62 |
* @param string $query |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function query_string($query) |
| 67 |
{ |
| 68 |
|
| 69 |
$output = preg_replace_callback('/{(.*?)}/', array($this, 'query_matches'), $query); |
| 70 |
$output = $this->handle_custom_methods($output); |
| 71 |
|
| 72 |
return $output; |
| 73 |
} |
| 74 |
|
| 75 |
public function query_matches($matches) |
| 76 |
{ |
| 77 |
if (isset($matches[0]) && isset($matches[1])) { |
| 78 |
return $this->query($matches[1]); |
| 79 |
} |
| 80 |
|
| 81 |
return ''; |
| 82 |
} |
| 83 |
|
| 84 |
abstract public function query($query); |
| 85 |
|
| 86 |
public function file() |
| 87 |
{ |
| 88 |
return $this->file; |
| 89 |
} |
| 90 |
|
| 91 |
public function handle_custom_methods($input) |
| 92 |
{ |
| 93 |
// m: Multiline modifier |
| 94 |
// s: matches all characters including newlines |
| 95 |
// U: non-greedy matching |
| 96 |
$input = preg_replace_callback('/\[([\w]+)\((.*?)\)\]/msU', function ($matches) { |
| 97 |
|
| 98 |
$method = $matches[1]; |
| 99 |
|
| 100 |
$result = []; |
| 101 |
$args = []; |
| 102 |
|
| 103 |
// Dont split comma's if they are inside a double quote |
| 104 |
if (preg_match_all('/(?:".*?"|[^",\s]+)(?=\s*,|\s*$)/s', $matches[2], $result) > 0) { |
| 105 |
$args = $result[0]; |
| 106 |
foreach ($args as &$arg) { |
| 107 |
|
| 108 |
// Strip commas from start and end of string |
| 109 |
$arg = preg_replace('/^(\'(.*)\'|"(.*)")$/s', '$2$3', $arg); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (is_callable($method)) { |
| 114 |
return call_user_func_array($method, $args); |
| 115 |
} |
| 116 |
|
| 117 |
return $matches[0]; |
| 118 |
}, $input); |
| 119 |
return $input; |
| 120 |
} |
| 121 |
} |
| 122 |
|