|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Serializers\ValueCsvSerializer |
|
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 2016 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\Serializers; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Serializer to serialize/unserialize simple column values. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/techdivision/import |
|
30
|
|
|
* @link http://www.techdivision.com |
|
31
|
|
|
*/ |
|
32
|
|
|
class ValueCsvSerializer extends AbstractCsvSerializer |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The delimiter to override the one from the configuration with. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $delimiter; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The delimiter to use instead the one from the configuration. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $delimiter The delimiter |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
5 |
|
protected function setDelimiter($delimiter) |
|
50
|
|
|
{ |
|
51
|
5 |
|
$this->delimiter = $delimiter; |
|
52
|
5 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the delimiter to use instead the one from the configuration. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string The delimiter |
|
58
|
|
|
*/ |
|
59
|
6 |
|
protected function getDelimiter() |
|
60
|
|
|
{ |
|
61
|
6 |
|
return $this->delimiter; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Serializes the elements of the passed array by using the |
|
66
|
|
|
* instances CSV configuration options. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array|null $unserialized The serialized data |
|
69
|
|
|
* |
|
70
|
|
|
* @return string|null The serialized array |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function serialize(array $unserialized = null) |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
// do nothing, if the passed value is empty or NULL |
|
76
|
3 |
|
if ($unserialized === null || $unserialized === '') { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// load the global configuration |
|
81
|
3 |
|
$configuration = $this->getConfiguration(); |
|
82
|
|
|
|
|
83
|
|
|
// initializet delimiter, enclosure and escape char |
|
84
|
3 |
|
$delimiter = $this->getDelimiter() ? $this->getDelimiter() : $configuration->getDelimiter(); |
|
85
|
3 |
|
$enclosure = $configuration->getEnclosure(); |
|
86
|
3 |
|
$escape = $configuration->getEscape(); |
|
87
|
|
|
|
|
88
|
|
|
// create hte callback method to enclose/escape the values |
|
89
|
3 |
|
$callback = function ($value) use ($enclosure, $escape) { |
|
90
|
2 |
|
return $enclosure . str_replace($escape, $escape . $escape, $value) . $enclosure; |
|
91
|
3 |
|
}; |
|
92
|
|
|
|
|
93
|
|
|
// implode and return the enclosed/escaped values |
|
94
|
3 |
|
return implode($delimiter, array_map($callback, $unserialized)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Unserializes the elements of the passed value by using the |
|
99
|
|
|
* instances CSV configuration options. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string|null $serialized The value to unserialize |
|
102
|
|
|
* |
|
103
|
|
|
* @return array|null The exploded values |
|
104
|
|
|
*/ |
|
105
|
4 |
|
public function unserialize($serialized = null) |
|
106
|
|
|
{ |
|
107
|
|
|
|
|
108
|
|
|
// do nothing, if the passed value is empty or NULL |
|
109
|
4 |
|
if ($serialized === null || $serialized === '') { |
|
110
|
1 |
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// load the global configuration |
|
114
|
3 |
|
$configuration = $this->getConfiguration(); |
|
115
|
|
|
|
|
116
|
|
|
// initializet delimiter, enclosure and escape char |
|
117
|
3 |
|
$delimiter = $this->getDelimiter() ? $this->getDelimiter() : $configuration->getDelimiter(); |
|
118
|
3 |
|
$enclosure = $configuration->getEnclosure(); |
|
119
|
3 |
|
$escape = $configuration->getEscape(); |
|
120
|
|
|
|
|
121
|
|
|
// parse and return the found data as array |
|
122
|
3 |
|
return str_getcsv($serialized, $delimiter, $enclosure, $escape); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Extracts the elements of the passed value by exploding them |
|
127
|
|
|
* with the also passed delimiter. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string|null $value The value to extract |
|
130
|
|
|
* @param string|null $delimiter The delimiter used to extrace the elements |
|
131
|
|
|
* |
|
132
|
|
|
* @return array|null The exploded values |
|
133
|
|
|
* @see \TechDivision\Import\Serializers\ValueCsvSerializer::unserialize() |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public function explode($value = null, $delimiter = null) |
|
136
|
|
|
{ |
|
137
|
|
|
|
|
138
|
|
|
// set the delimiter |
|
139
|
3 |
|
$this->setDelimiter($delimiter); |
|
140
|
|
|
|
|
141
|
|
|
// unserialize the value and return it |
|
142
|
3 |
|
return $this->unserialize($value); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Compacts the elements of the passed value by imploding them |
|
147
|
|
|
* with the also passed delimiter. |
|
148
|
|
|
* |
|
149
|
|
|
* @param array|null $value The values to compact |
|
150
|
|
|
* @param string|null $delimiter The delimiter use to implode the values |
|
151
|
|
|
* |
|
152
|
|
|
* @return string|null The compatected value |
|
153
|
|
|
* @see \TechDivision\Import\Serializers\ValueCsvSerializer::serialize() |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function implode(array $value = null, $delimiter = null) |
|
156
|
|
|
{ |
|
157
|
|
|
|
|
158
|
|
|
// set the delimiter |
|
159
|
2 |
|
$this->setDelimiter($delimiter); |
|
160
|
|
|
|
|
161
|
|
|
// serialize the value and return it |
|
162
|
2 |
|
return $this->serialize($value); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|