1 | <?php |
||
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) |
|
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() |
|
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) |
|
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) |
||
164 | } |
||
165 |