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
|
3 |
|
protected function setDelimiter($delimiter) |
50
|
|
|
{ |
51
|
3 |
|
$this->delimiter = $delimiter; |
52
|
3 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the delimiter to use instead the one from the configuration. |
56
|
|
|
* |
57
|
|
|
* @return string The delimiter |
58
|
|
|
*/ |
59
|
9 |
|
protected function getDelimiter() |
60
|
|
|
{ |
61
|
9 |
|
return $this->delimiter; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Serializes the elements of the passed array. |
66
|
|
|
* |
67
|
|
|
* @param array|null $unserialized The serialized data |
68
|
|
|
* @param string|null $delimiter The delimiter used to serialize the values |
69
|
|
|
* |
70
|
|
|
* @return string The serialized array |
71
|
|
|
*/ |
72
|
5 |
|
public function serialize(array $unserialized = null, $delimiter = null) |
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
// do nothing, if the passed value is empty or NULL |
76
|
5 |
|
if ($unserialized === null || $unserialized === '') { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// load the global configuration |
81
|
5 |
|
$csvConfiguration = $this->getCsvConfiguration(); |
82
|
|
|
|
83
|
|
|
// initializet delimiter, enclosure and escape char |
84
|
5 |
|
$delimiter = $this->getDelimiter() ? $this->getDelimiter() : $csvConfiguration->getDelimiter(); |
85
|
5 |
|
$enclosure = $csvConfiguration->getEnclosure(); |
86
|
5 |
|
$escape = $csvConfiguration->getEscape(); |
87
|
|
|
|
88
|
|
|
// create hte callback method to enclose/escape the values |
89
|
5 |
|
$callback = function ($value) use ($enclosure, $escape) { |
90
|
4 |
|
return $enclosure . str_replace($escape, $escape . $escape, $value) . $enclosure; |
91
|
5 |
|
}; |
92
|
|
|
|
93
|
|
|
// implode and return the enclosed/escaped values |
94
|
5 |
|
return implode($delimiter, array_map($callback, $unserialized)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Unserializes the elements of the passed string. |
99
|
|
|
* |
100
|
|
|
* @param string|null $serialized The value to unserialize |
101
|
|
|
* @param string|null $delimiter The delimiter used to unserialize the elements |
102
|
|
|
* |
103
|
|
|
* @return array The unserialized values |
104
|
|
|
*/ |
105
|
5 |
|
public function unserialize($serialized = null, $delimiter = null) |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
// do nothing, if the passed value is empty or NULL |
109
|
5 |
|
if ($serialized === null || $serialized === '') { |
110
|
1 |
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// load the global configuration |
114
|
4 |
|
$csvConfiguration = $this->getCsvConfiguration(); |
115
|
|
|
|
116
|
|
|
// initializet delimiter, enclosure and escape char |
117
|
4 |
|
$delimiter = $this->getDelimiter() ? $this->getDelimiter() : $csvConfiguration->getDelimiter(); |
118
|
4 |
|
$enclosure = $csvConfiguration->getEnclosure(); |
119
|
4 |
|
$escape = $csvConfiguration->getEscape(); |
120
|
|
|
|
121
|
|
|
// parse and return the found data as array |
122
|
4 |
|
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 extract 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
|
|
|
public function implode(array $value = null, $delimiter = null) |
156
|
|
|
{ |
157
|
|
|
|
158
|
|
|
// set the delimiter |
159
|
|
|
$this->setDelimiter($delimiter); |
160
|
|
|
|
161
|
|
|
// serialize the value and return it |
162
|
|
|
return $this->serialize($value); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|