1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Observers\CleanUpEmptyColumnsTrait |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Martin Eissenführer <[email protected]> |
15
|
|
|
* @copyright 2021 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Subjects\CleanUpColumnsSubjectInterface; |
24
|
|
|
use TechDivision\Import\Utils\ConfigurationKeys; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Observer that extracts the missing attribute option values from a customer CSV. |
28
|
|
|
* |
29
|
|
|
* @author Martin Eissenführer <[email protected]> |
30
|
|
|
* @copyright 2021 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/techdivision/import-converter-customer-attribute |
33
|
|
|
* @link http://www.techdivision.com |
34
|
|
|
*/ |
35
|
|
|
trait CleanUpEmptyColumnsTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* The array with the column keys that has to be cleaned up when their values are empty. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $cleanUpEmptyColumnKeys; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Remove all the empty values from the row and return the cleared row. |
46
|
|
|
* |
47
|
|
|
* @return array The cleared row |
48
|
|
|
*/ |
49
|
|
|
protected function clearRow() |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
// query whether or not the column keys has been initialized |
53
|
|
|
if ($this->cleanUpEmptyColumnKeys === null) { |
54
|
|
|
// initialize the array with the column keys that has to be cleaned-up |
55
|
|
|
$this->cleanUpEmptyColumnKeys = array(); |
56
|
|
|
|
57
|
|
|
// query whether or not column names that has to be cleaned up have been configured |
58
|
|
View Code Duplication |
if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS) |
|
|
|
|
59
|
|
|
&& ($this->getSubject() instanceof CleanUpColumnsSubjectInterface)) { |
|
|
|
|
60
|
|
|
// if yes, load the column names |
61
|
|
|
$cleanUpEmptyColumns = $this->getSubject()->getCleanUpColumns(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// translate the column names into column keys |
64
|
|
|
foreach ($cleanUpEmptyColumns as $cleanUpEmptyColumn) { |
65
|
|
|
if ($this->hasHeader($cleanUpEmptyColumn)) { |
|
|
|
|
66
|
|
|
$this->cleanUpEmptyColumnKeys[] = $this->getHeader($cleanUpEmptyColumn); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// remove all the empty values from the row, expected the columns has to be cleaned-up |
73
|
|
View Code Duplication |
foreach ($this->row as $key => $value) { |
|
|
|
|
74
|
|
|
// query whether or not the value is empty AND the column has NOT to be cleaned-up |
75
|
|
|
if (($value === null || $value === '') && in_array($key, $this->cleanUpEmptyColumnKeys) === false) { |
76
|
|
|
unset($this->row[$key]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// finally return the clean row |
81
|
|
|
return $this->row; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.