ValueCsvSerializer::explode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Serializer\Csv\ValueCsvSerializer
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2021 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-serializer-csv
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Serializer\Csv;
16
17
/**
18
 * Serializer to serialize/unserialize simple column values.
19
 *
20
 * @author    Tim Wagner <[email protected]>
21
 * @copyright 2021 TechDivision GmbH <[email protected]>
22
 * @license   https://opensource.org/licenses/MIT
23
 * @link      https://github.com/techdivision/import-serializer-csv
24
 * @link      http://www.techdivision.com
25
 */
26
class ValueCsvSerializer extends AbstractCsvSerializer
27
{
28
29
    /**
30
     * The delimiter to override the one from the configuration with.
31
     *
32
     * @var string
33
     */
34
    private $delimiter;
35
36
    /**
37
     * The delimiter to use instead the one from the configuration.
38
     *
39
     * @param string $delimiter The delimiter
40
     *
41
     * @return void
42
     */
43
    protected function setDelimiter($delimiter)
44
    {
45
        $this->delimiter = $delimiter;
46
    }
47
48
    /**
49
     * Returns the delimiter to use instead the one from the configuration.
50
     *
51
     * @return string The delimiter
52
     */
53
    protected function getDelimiter()
54
    {
55
        return $this->delimiter ? $this->delimiter : $this->getSerializerConfiguration()->getDelimiter();
56
    }
57
58
    /**
59
     * Serializes the elements of the passed array.
60
     *
61
     * @param array|null  $unserialized The serialized data
62
     * @param string|null $delimiter    The delimiter used to serialize the values
63
     *
64
     * @return string The serialized array
65
     */
66
    public function serialize(array $unserialized = null, $delimiter = null)
67
    {
68
69
        // do nothing, if the passed value is empty or NULL
70
        if ($unserialized === null || $unserialized === '') {
71
            return;
72
        }
73
74
        // initialize the delimiter char
75
        $delimiter = $delimiter ? $delimiter : $this->getDelimiter();
76
77
        // load the global configuration
78
        $csvConfiguration = $this->getSerializerConfiguration();
79
80
        // initialize the enclosure and escape char
81
        $enclosure = $csvConfiguration->getEnclosure();
82
        $escape = $csvConfiguration->getEscape();
83
84
        // serialize the passed array into memory and rewind the stream
85
        $length = fputcsv($fp = fopen('php://memory', 'w'), $unserialized, $delimiter, $enclosure, $escape);
86
        rewind($fp);
87
88
        // load the serialized value - cut off the newnline char
89
        $serialized = trim(fread($fp, $length), PHP_EOL);
90
        fclose($fp);
91
92
        // return the serialized value
93
        return $serialized;
94
    }
95
96
    /**
97
     * Unserializes the elements of the passed string.
98
     *
99
     * @param string|null $serialized The value to unserialize
100
     * @param string|null $delimiter  The delimiter used to unserialize the elements
101
     *
102
     * @return array The unserialized values
103
     */
104
    public function unserialize($serialized = null, $delimiter = null)
105
    {
106
107
        // do nothing, if the passed value is empty or NULL
108
        if ($serialized === null || $serialized === '') {
109
            return;
110
        }
111
112
        // initialize the delimiter char
113
        $delimiter = $delimiter ? $delimiter : $this->getDelimiter();
114
115
        // load the global configuration
116
        $csvConfiguration = $this->getSerializerConfiguration();
117
118
        // initialize the enclosure and escape char
119
        $enclosure = $csvConfiguration->getEnclosure();
120
        $escape = $csvConfiguration->getEscape();
121
122
        // parse and return the found data as array
123
        return str_getcsv($serialized, $delimiter, $enclosure, $escape);
124
    }
125
126
    /**
127
     * Extracts the elements of the passed value by exploding them
128
     * with the also passed delimiter.
129
     *
130
     * @param string|null $value     The value to extract
131
     * @param string|null $delimiter The delimiter used to extract the elements
132
     *
133
     * @return array|null The exploded values
134
     * @see \TechDivision\Import\Serializer\SerializerInterface::unserialize()
135
     */
136
    public function explode($value = null, $delimiter = null)
137
    {
138
139
        // set the delimiter
140
        $this->setDelimiter($delimiter);
141
142
        // unserialize the value and return it
143
        return $this->unserialize($value);
144
    }
145
146
    /**
147
     * Compacts the elements of the passed value by imploding them
148
     * with the also passed delimiter.
149
     *
150
     * @param array|null  $value     The values to compact
151
     * @param string|null $delimiter The delimiter use to implode the values
152
     *
153
     * @return string|null The compatected value
154
     * @see  \TechDivision\Import\Serializer\SerializerInterface::serialize()
155
     */
156
    public function implode(array $value = null, $delimiter = null)
157
    {
158
159
        // set the delimiter
160
        $this->setDelimiter($delimiter);
161
162
        // serialize the value and return it
163
        return $this->serialize($value);
164
    }
165
}
166