Completed
Push — 15.x ( 02c98b...d82ad5 )
by Tim
02:41
created

ValueCsvSerializer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 140
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setDelimiter() 0 4 1
A getDelimiter() 0 4 2
A serialize() 0 29 4
A unserialize() 0 21 4
A explode() 0 9 1
A implode() 0 9 1
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 29
    protected function getDelimiter()
60
    {
61 29
        return $this->delimiter ? $this->delimiter : $this->getCsvConfiguration()->getDelimiter();
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 6
    public function serialize(array $unserialized = null, $delimiter = null)
73
    {
74
75
        // do nothing, if the passed value is empty or NULL
76 6
        if ($unserialized === null || $unserialized === '') {
77
            return;
78
        }
79
80
        // initialize the delimiter char
81 6
        $delimiter = $delimiter ? $delimiter : $this->getDelimiter();
82
83
        // load the global configuration
84 6
        $csvConfiguration = $this->getCsvConfiguration();
85
86
        // initialize the enclosure and escape char
87 6
        $enclosure = $csvConfiguration->getEnclosure();
88 6
        $escape = $csvConfiguration->getEscape();
89
90
        // serialize the passed array into memory and rewind the stream
91 6
        $length = fputcsv($fp = fopen('php://memory', 'w'), $unserialized, $delimiter, $enclosure, $escape);
92 6
        rewind($fp);
93
94
        // load the serialized value - cut off the newnline char
95 6
        $serialized = trim(fread($fp, $length), PHP_EOL);
96 6
        fclose($fp);
97
98
        // return the serialized value
99 6
        return $serialized;
100
    }
101
102
    /**
103
     * Unserializes the elements of the passed string.
104
     *
105
     * @param string|null $serialized The value to unserialize
106
     * @param string|null $delimiter  The delimiter used to unserialize the elements
107
     *
108
     * @return array The unserialized values
109
     */
110 32
    public function unserialize($serialized = null, $delimiter = null)
111
    {
112
113
        // do nothing, if the passed value is empty or NULL
114 32
        if ($serialized === null || $serialized === '') {
115 1
            return;
116
        }
117
118
        // initialize the delimiter char
119 31
        $delimiter = $delimiter ? $delimiter : $this->getDelimiter();
120
121
        // load the global configuration
122 31
        $csvConfiguration = $this->getCsvConfiguration();
123
124
        // initialize the enclosure and escape char
125 31
        $enclosure = $csvConfiguration->getEnclosure();
126 31
        $escape = $csvConfiguration->getEscape();
127
128
        // parse and return the found data as array
129 31
        return str_getcsv($serialized, $delimiter, $enclosure, $escape);
130
    }
131
132
    /**
133
     * Extracts the elements of the passed value by exploding them
134
     * with the also passed delimiter.
135
     *
136
     * @param string|null $value     The value to extract
137
     * @param string|null $delimiter The delimiter used to extract the elements
138
     *
139
     * @return array|null The exploded values
140
     * @see \TechDivision\Import\Serializers\ValueCsvSerializer::unserialize()
141
     */
142 5
    public function explode($value = null, $delimiter = null)
143
    {
144
145
        // set the delimiter
146 5
        $this->setDelimiter($delimiter);
147
148
        // unserialize the value and return it
149 5
        return $this->unserialize($value);
150
    }
151
152
    /**
153
     * Compacts the elements of the passed value by imploding them
154
     * with the also passed delimiter.
155
     *
156
     * @param array|null  $value     The values to compact
157
     * @param string|null $delimiter The delimiter use to implode the values
158
     *
159
     * @return string|null The compatected value
160
     * @see \TechDivision\Import\Serializers\ValueCsvSerializer::serialize()
161
     */
162
    public function implode(array $value = null, $delimiter = null)
163
    {
164
165
        // set the delimiter
166
        $this->setDelimiter($delimiter);
167
168
        // serialize the value and return it
169
        return $this->serialize($value);
170
    }
171
}
172