1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Command\ImportConvertValueCommand |
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 Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2019 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-cli-simple |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Command; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use TechDivision\Import\Utils\CommandNames; |
27
|
|
|
use TechDivision\Import\Configuration\ConfigurationInterface; |
28
|
|
|
use TechDivision\Import\Serializers\ValueCsvSerializer; |
29
|
|
|
use TechDivision\Import\Utils\InputArgumentKeysInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The command to simulate converting a file. |
33
|
|
|
* |
34
|
|
|
* @author Tim Wagner <[email protected]> |
35
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
36
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
37
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
38
|
|
|
* @link http://www.techdivision.com |
39
|
|
|
*/ |
40
|
|
|
class ImportConvertValueCommand extends AbstractSimpleImportCommand |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Configures the current command. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
48
|
|
|
*/ |
49
|
|
|
protected function configure() |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
// initialize the command with the required/optional options |
53
|
|
|
$this->setName(CommandNames::IMPORT_CONVERT_VALUE) |
54
|
|
|
->setDescription('Converts the value to the format expected by the given column') |
55
|
|
|
->addArgument(InputArgumentKeysInterface::ENTITY_TYPE_CODE, InputArgument::REQUIRED, 'The entity type code to use, e. g. catalog_product') |
56
|
|
|
->addArgument(InputArgumentKeysInterface::COLUMN, InputArgument::REQUIRED, 'The column name to convert the value for') |
57
|
|
|
->addArgument(InputArgumentKeysInterface::VALUES, InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The value to convert'); |
58
|
|
|
|
59
|
|
|
// invoke the parent method |
60
|
|
|
parent::configure(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Finally executes the simple command. |
65
|
|
|
* |
66
|
|
|
* @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance |
67
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
68
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
protected function executeSimpleCommand( |
73
|
|
|
ConfigurationInterface $configuration, |
74
|
|
|
InputInterface $input, |
75
|
|
|
OutputInterface $output |
76
|
|
|
) { |
77
|
|
|
|
78
|
|
|
// initialize the default CSV serializer |
79
|
|
|
$serializer = new ValueCsvSerializer(); |
80
|
|
|
$serializer->init($configuration); |
81
|
|
|
|
82
|
|
|
// initialize the array for the values that has to be serialized |
83
|
|
|
$serialize = array(); |
84
|
|
|
|
85
|
|
|
// load the values that has to be serialized |
86
|
|
|
$values = $input->getArgument(InputArgumentKeysInterface::VALUES); |
87
|
|
|
|
88
|
|
|
// simulate custom column handling |
89
|
|
|
switch ($input->getArgument(InputArgumentKeysInterface::COLUMN)) { |
90
|
|
|
case 'categories': |
91
|
|
|
// serialize the categories and use the default delimiter |
92
|
|
|
$serialize[] = $serializer->serialize($values); |
|
|
|
|
93
|
|
|
break; |
94
|
|
|
|
95
|
|
|
case 'path': |
96
|
|
|
// categories use a slash (/) as delimiter for the first level of serialization |
97
|
|
|
$delimiter = '/'; |
98
|
|
|
// load the enclosure from the configuration |
99
|
|
|
$enclosure = $configuration->getEnclosure(); |
100
|
|
|
// iterate over the values and serialize them |
101
|
|
|
for ($i = 0; $i < sizeof($values); $i++) { |
|
|
|
|
102
|
|
|
// serialize the value and use a slash (/) as delimiter |
103
|
|
|
$val = $serializer->serialize(array($values[$i]), $delimiter); |
104
|
|
|
|
105
|
|
|
// clean-up (means to remove surrounding + double quotes) |
106
|
|
|
// because we've no delimiter within the value |
107
|
|
|
if (strstr($val, $delimiter) === false) { |
108
|
|
|
$val = preg_replace("/^(\'(.*)\'|${enclosure}(.*)${enclosure})$/", '$2$3', $val); |
109
|
|
|
$val = str_replace('""', '"', $val); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// append the cleaned value |
113
|
|
|
$values[$i] = $val; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// implode the category's to get the complete path |
117
|
|
|
$serialize[] = implode($delimiter, $values); |
|
|
|
|
118
|
|
|
break; |
119
|
|
|
|
120
|
|
|
default: |
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// second serialization that simulates the framework parsing the CSV file |
125
|
|
|
$output->write($serializer->serialize($serialize)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|