CategoryCsvSerializer::getValueCsvSerializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Serializer\Csv\CategoryCsvSerializer
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
use TechDivision\Import\Serializer\SerializerInterface;
18
use TechDivision\Import\Serializer\SerializerFactoryInterface;
19
use TechDivision\Import\Serializer\CategorySerializerInterface;
20
use TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface;
21
22
/**
23
 * Serializer implementation that un-/serializes the categories found in the CSV file
24
 * in the row 'path'.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2021 TechDivision GmbH <[email protected]>
28
 * @license   https://opensource.org/licenses/MIT
29
 * @link      https://github.com/techdivision/import-serializer-csv
30
 * @link      http://www.techdivision.com
31
 */
32
class CategoryCsvSerializer extends AbstractCsvSerializer implements CategorySerializerInterface
33
{
34
35
    /**
36
     * The factory instance for the CSV value serializer.
37
     *
38
     * @var \TechDivision\Import\Serializer\SerializerFactoryInterface
39
     */
40
    private $valueCsvSerializerFactory;
41
42
    /**
43
     * The CSV value serializer instance.
44
     *
45
     * @var \TechDivision\Import\Serializer\SerializerInterface
46
     */
47
    private $valueCsvSerializer;
48
49
    /**
50
     *  The configuration instance.
51
     *
52
     * @var \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface
53
     */
54
    private $configuration;
55
56
    /**
57
     * Initialize the serializer with the passed CSV value serializer factory.
58
     *
59
     * @param \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface $configuration             The configuration instance
60
     * @param \TechDivision\Import\Serializer\SerializerFactoryInterface                     $valueCsvSerializerFactory The CSV value serializer factory
61
     */
62
    public function __construct(
63
        SerializerConfigurationInterface $configuration,
64
        SerializerFactoryInterface $valueCsvSerializerFactory
65
    ) {
66
67
        // set the passed instances
68
        $this->configuration = $configuration;
69
        $this->valueCsvSerializerFactory = $valueCsvSerializerFactory;
70
71
        // pre-initialize the serialize with the values
72
        // found in the main configuration
73
        $this->init($configuration);
74
    }
75
76
    /**
77
     * Returns the configuration instance.
78
     *
79
     * @return \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface The configuration instance
80
     */
81
    protected function getConfiguration()
82
    {
83
        return $this->configuration;
84
    }
85
86
    /**
87
     * Returns the factory instance for the CSV value serializer.
88
     *
89
     * @return \TechDivision\Import\Serializer\SerializerFactoryInterface The CSV value serializer factory instance
90
     */
91
    protected function getValueCsvSerializerFactory()
92
    {
93
        return $this->valueCsvSerializerFactory;
94
    }
95
96
    /**
97
     * Returns the CSV value serializer instance.
98
     *
99
     * @param \TechDivision\Import\Serializer\SerializerInterface $valueCsvSerializer The CSV value serializer instance
100
     *
101
     * @return void
102
     */
103
    protected function setValueCsvSerializer(SerializerInterface $valueCsvSerializer)
104
    {
105
        $this->valueCsvSerializer = $valueCsvSerializer;
106
    }
107
108
    /**
109
     * Returns the CSV value serializer instance.
110
     *
111
     * @return \TechDivision\Import\Serializer\SerializerInterface The CSV value serializer instance
112
     */
113
    protected function getValueCsvSerializer()
114
    {
115
        return $this->valueCsvSerializer;
116
    }
117
118
    /**
119
     * Return's the delimiter character for categories, default value is comma (/).
120
     *
121
     * @return string The delimiter character for categories
122
     */
123
    protected function getCategoryDelimiter()
124
    {
125
        return $this->getConfiguration()->getCategoryDelimiter();
0 ignored issues
show
Bug introduced by
The method getCategoryDelimiter() does not exist on TechDivision\Import\Seri...rConfigurationInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
        return $this->getConfiguration()->/** @scrutinizer ignore-call */ getCategoryDelimiter();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
    }
127
128
    /**
129
     * Passes the configuration and initializes the serializer.
130
     *
131
     * @param \TechDivision\Import\Serializer\Configuration\SerializerConfigurationInterface $configuration The CSV configuration
132
     *
133
     * @return void
134
     */
135
    public function init(SerializerConfigurationInterface $configuration)
136
    {
137
138
        // pass the configuration to the parent instance
139
        parent::init($configuration);
140
141
        // create the CSV value serializer instance
142
        $this->setValueCsvSerializer($this->getValueCsvSerializerFactory()->createSerializer($configuration));
143
    }
144
145
    /**
146
     * Extracts the elements of the passed value by exploding them
147
     * with the also passed delimiter.
148
     *
149
     * @param string|null $value     The value to extract
150
     * @param string|null $delimiter The delimiter used to extrace the elements
151
     *
152
     * @return array|null The exploded values
153
     * @see \TechDivision\Import\Serializer\SerializerInterface::unserialize()
154
     */
155
    public function explode($value = null, $delimiter = null)
156
    {
157
        return $this->unserialize($value, $delimiter);
158
    }
159
160
    /**
161
     * Compacts the elements of the passed value by imploding them
162
     * with the also passed delimiter.
163
     *
164
     * @param array|null  $value     The values to compact
165
     * @param string|null $delimiter The delimiter use to implode the values
166
     *
167
     * @return string|null The compatected value
168
     * @see \TechDivision\Import\Serializer\SerializerInterface::serialize()
169
     */
170
    public function implode(array $value = null, $delimiter = null)
171
    {
172
        return $this->serialize($value, $delimiter);
173
    }
174
175
    /**
176
     * Unserializes the elements of the passed string.
177
     *
178
     * @param string|null $serialized The value to unserialize
179
     * @param string|null $delimiter  The delimiter used to unserialize the elements
180
     *
181
     * @return array The unserialized values
182
     * @see \TechDivision\Import\Serializer\SerializerInterface::unserialize()
183
     */
184
    public function unserialize($serialized = null, $delimiter = null)
185
    {
186
        return $this->getValueCsvSerializer()->explode($serialized, $delimiter ? $delimiter : $this->getCategoryDelimiter());
187
    }
188
189
    /**
190
     * Serializes the elements of the passed array.
191
     *
192
     * @param array|null  $unserialized The serialized data
193
     * @param string|null $delimiter    The delimiter used to serialize the values
194
     *
195
     * @return string The serialized array
196
     * @see \TechDivision\Import\Serializer\SerializerInterface::serialize()
197
     */
198
    public function serialize(array $unserialized = null, $delimiter = null)
199
    {
200
        return $this->getValueCsvSerializer()->implode($unserialized, $delimiter ? $delimiter : $this->getCategoryDelimiter());
201
    }
202
203
    /**
204
     * Denormalizes the passed path.
205
     *
206
     * @param string $path The path that has to be normalized
207
     *
208
     * @return string The denormalized path
209
     * @throws \Exception Is thrown, because the method has not yet been implemented
210
     */
211
    public function denormalize(string $path) : string
212
    {
213
        throw new \Exception(sprintf('Method "%s" has not been implemented yet', __METHOD__));
214
    }
215
216
    /**
217
     * Normalizes the category path in a standard representation.
218
     *
219
     * For example this means, that a category  path `Default Category/Gear`
220
     * will be normalized into `"Default Category"/Gear`.
221
     *
222
     * @param string $path The category path that has to be normalized
223
     *
224
     * @return string The normalized category path
225
     */
226
    public function normalize(string $path) : string
227
    {
228
        return $this->implode($this->explode(trim($path)));
229
    }
230
}
231