SpreadsheetReader::getIteratorOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Reader;
4
5
use Pim\Component\Catalog\Repository\AttributeRepositoryInterface;
6
use Symfony\Component\Validator\Constraints as Assert;
7
use Pim\Bundle\CatalogBundle\Validator\Constraints\File as AssertFile;
8
9
/**
10
 * Spreadsheet file reader
11
 *
12
 * @author    Antoine Guigan <[email protected]>
13
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
14
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
15
 */
16
class SpreadsheetReader extends FileIteratorReader
17
{
18
    /** @var AttributeRepositoryInterface */
19
    protected $attributeRepository;
20
21
    /** @var string */
22
    protected $identifierCode;
23
24
    /**
25
     * @var string
26
     *
27
     * @Assert\NotBlank(groups={"Execution"})
28
     * @AssertFile(
29
     *     groups={"Execution"},
30
     *     allowedExtensions={"xlsx", "xlsm", "csv"},
31
     * )
32
     */
33
    protected $filePath;
34
35
    /**
36
     * @var string
37
     *
38
     * @Assert\NotBlank
39
     * @Assert\Choice(choices={",", ";", "|"}, message="The value must be one of , or ; or |")
40
     */
41
    protected $delimiter = ';';
42
43
    /**
44
     * @var string
45
     *
46
     * @Assert\NotBlank
47
     * @Assert\Choice(choices={"""", "'"}, message="The value must be one of "" or '")
48
     */
49
    protected $enclosure = '"';
50
51
    /**
52
     * @var string
53
     *
54
     * @Assert\NotBlank
55
     */
56
    protected $escape = '\\';
57
58
    /**
59
     * @var string
60
     *
61
     * @Assert\NotBlank
62
     */
63
    protected $encoding = 'UTF8';
64
65
    /**
66
     * @param AttributeRepositoryInterface $attributeRepository
67
     */
68
    public function setAttributeRepository(AttributeRepositoryInterface $attributeRepository)
69
    {
70
        $this->attributeRepository = $attributeRepository;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getUploadedFileConstraints()
77
    {
78
        return array(
79
            new Assert\NotBlank(),
80
            new AssertFile(
81
                array(
82
                    'allowedExtensions' => array('csv', 'xlsx', 'xlsm')
83
                )
84
            )
85
        );
86
    }
87
88
89
    /**
90
     * @param string $delimiter
91
     *
92
     * @return SpreadsheetReader
93
     */
94
    public function setDelimiter($delimiter)
95
    {
96
        $this->delimiter = $delimiter;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string $delimiter
103
     */
104
    public function getDelimiter()
105
    {
106
        return $this->delimiter;
107
    }
108
109
    /**
110
     * @param string $enclosure
111
     *
112
     * @return SpreadsheetReader
113
     */
114
    public function setEnclosure($enclosure)
115
    {
116
        $this->enclosure = $enclosure;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return string $enclosure
123
     */
124
    public function getEnclosure()
125
    {
126
        return $this->enclosure;
127
    }
128
129
    /**
130
     * @param string $escape
131
     *
132
     * @return SpreadsheetReader
133
     */
134
    public function setEscape($escape)
135
    {
136
        $this->escape = $escape;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return string $escape
143
     */
144
    public function getEscape()
145
    {
146
        return $this->escape;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getEncoding()
153
    {
154
        return $this->encoding;
155
    }
156
157
    /**
158
     * @param string $encoding
159
     *
160
     * @return SpreadsheetReader
161
     */
162
    public function setEncoding($encoding)
163
    {
164
        $this->encoding = $encoding;
165
166
        return $this;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getConfigurationFields()
173
    {
174
        return array(
175
            'filePath' => array(
176
                'options' => array(
177
                    'label' => 'pim_base_connector.import.filePath.label',
178
                    'help'  => 'pim_base_connector.import.filePath.help'
179
                )
180
            ),
181
            'uploadAllowed' => array(
182
                'type'    => 'switch',
183
                'options' => array(
184
                    'label' => 'pim_base_connector.import.uploadAllowed.label',
185
                    'help'  => 'pim_base_connector.import.uploadAllowed.help'
186
                )
187
            ),
188
            'delimiter' => array(
189
                'options' => array(
190
                    'label' => 'pim_base_connector.import.delimiter.label',
191
                    'help'  => 'pim_base_connector.import.delimiter.help'
192
                )
193
            ),
194
            'enclosure' => array(
195
                'options' => array(
196
                    'label' => 'pim_base_connector.import.enclosure.label',
197
                    'help'  => 'pim_base_connector.import.enclosure.help'
198
                )
199
            ),
200
            'escape' => array(
201
                'options' => array(
202
                    'label' => 'pim_base_connector.import.escape.label',
203
                    'help'  => 'pim_base_connector.import.escape.help'
204
                )
205
            ),
206
            'encoding' => array(
207
                'options' => array(
208
                    'label' => 'pim_excel_connector.import.encoding.label',
209
                    'help'  => 'pim_excel_connector.import.encoding.help'
210
                )
211
            ),
212
        );
213
    }
214
215
    /**
216
     * Returns the extension of the read file
217
     *
218
     * @return string
219
     */
220
    protected function getExtension()
221
    {
222
        return pathinfo($this->getFilePath(), PATHINFO_EXTENSION);
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    protected function getIteratorOptions()
229
    {
230
        $options = parent::getIteratorOptions();
231
        if ('csv' === $this->getExtension()) {
232
            $options['parser_options'] = [
233
                'delimiter' => $this->delimiter,
234
                'escape'    => $this->escape,
235
                'enclosure' => $this->enclosure,
236
                'encoding'  => $this->encoding
237
            ];
238
        }
239
240
        return $options;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    protected function convertNumericIdentifierToString(array $item)
247
    {
248
        $item = parent::convertNumericIdentifierToString($item);
249
250
        if (isset($item[$this->getIdentifierCode()]) && is_int($item[$this->getIdentifierCode()])) {
251
            $item[$this->getIdentifierCode()] = (string) $item[$this->getIdentifierCode()];
252
        }
253
254
        return $item;
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    protected function getIdentifierCode()
261
    {
262
        if (null === $this->identifierCode) {
263
            $this->identifierCode = $this->attributeRepository->getIdentifierCode();
264
        }
265
266
        return $this->identifierCode;
267
    }
268
}
269