| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
abstract class AbstractMapper |
| 6 |
{ |
| 7 |
protected $filters; |
| 8 |
protected $record = []; |
| 9 |
|
| 10 |
public function get_value($column, $template_data = null) |
| 11 |
{ |
| 12 |
if (is_null($template_data)) { |
| 13 |
$template_data = $this->record(); |
| 14 |
} |
| 15 |
|
| 16 |
$parts = explode('.', $column); |
| 17 |
if (count($parts) == 2) { |
| 18 |
|
| 19 |
// handle looped data |
| 20 |
if (isset($template_data[$parts[0]], $template_data[$parts[0]][0], $template_data[$parts[0]][0][$parts[1]])) { |
| 21 |
return array_reduce($template_data[$parts[0]], function ($carry, $item) use ($parts) { |
| 22 |
if (isset($item[$parts[1]])) { |
| 23 |
$carry[] = $item[$parts[1]]; |
| 24 |
} |
| 25 |
return $carry; |
| 26 |
}, []); |
| 27 |
} |
| 28 |
|
| 29 |
// handled grouped data or single |
| 30 |
return isset($template_data[$parts[0]], $template_data[$parts[0]][$parts[1]]) ? $template_data[$parts[0]][$parts[1]] : ''; |
| 31 |
} |
| 32 |
|
| 33 |
return isset($template_data[$column]) ? $template_data[$column] : ''; |
| 34 |
} |
| 35 |
|
| 36 |
public function set_filters($filters = []) |
| 37 |
{ |
| 38 |
$this->filters = $filters; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Filter record |
| 43 |
* |
| 44 |
* @param array $post |
| 45 |
* @return boolean |
| 46 |
*/ |
| 47 |
public function filter() |
| 48 |
{ |
| 49 |
$result = false; |
| 50 |
|
| 51 |
if (empty($this->filters)) { |
| 52 |
return $result; |
| 53 |
} |
| 54 |
|
| 55 |
foreach ($this->filters as $group) { |
| 56 |
|
| 57 |
$result = true; |
| 58 |
|
| 59 |
if (empty($group)) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
foreach ($group as $row) { |
| 64 |
|
| 65 |
$left = $this->get_value($row['left']); |
| 66 |
$right = $row['right']; |
| 67 |
$right_parts = array_map('trim', explode(',', $right)); |
| 68 |
|
| 69 |
switch ($row['condition']) { |
| 70 |
case 'equal': |
| 71 |
if ($left != $right) { |
| 72 |
$result = false; |
| 73 |
} |
| 74 |
break; |
| 75 |
case 'contains': |
| 76 |
if (stripos($left, $right) === false) { |
| 77 |
$result = false; |
| 78 |
} |
| 79 |
break; |
| 80 |
case 'in': |
| 81 |
if (!in_array($left, $right_parts)) { |
| 82 |
$result = false; |
| 83 |
} |
| 84 |
break; |
| 85 |
case 'not-equal': |
| 86 |
if ($left == $right) { |
| 87 |
$result = false; |
| 88 |
} |
| 89 |
break; |
| 90 |
case 'not-contains': |
| 91 |
if (stripos($left, $right) !== false) { |
| 92 |
$result = false; |
| 93 |
} |
| 94 |
break; |
| 95 |
case 'not-in': |
| 96 |
if (in_array($left, $right_parts)) { |
| 97 |
$result = false; |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ($result) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
return $result; |
| 110 |
} |
| 111 |
|
| 112 |
public function record() |
| 113 |
{ |
| 114 |
return $this->record; |
| 115 |
} |
| 116 |
} |
| 117 |
|