AbstractXlsxFileIterator::isReadableWorksheet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Iterator;
4
5
use Akeneo\Component\SpreadsheetParser\SpreadsheetInterface;
6
use Akeneo\Component\SpreadsheetParser\SpreadsheetLoader;
7
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * XLSX File iterator
13
 *
14
 * @author    Antoine Guigan <[email protected]>
15
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
16
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
17
 */
18
abstract class AbstractXlsxFileIterator extends AbstractFileIterator implements ContainerAwareInterface
19
{
20
    /** @var ContainerInterface */
21
    protected $container;
22
23
    /** @var Iterator */
24
    protected $worksheetIterator;
25
26
    /** @var Iterator */
27
    protected $valuesIterator;
28
29
    /** @var SpreadsheetInterface */
30
    private $xls;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function current()
36
    {
37
        return $this->valuesIterator->current();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function key()
44
    {
45
        return sprintf('%s/%s', $this->worksheetIterator->current(), $this->valuesIterator->key());
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function next()
52
    {
53
        $this->valuesIterator->next();
54
        if (!$this->valuesIterator->valid()) {
55
            $this->worksheetIterator->next();
56
            if ($this->worksheetIterator->valid()) {
57
                $this->initializeValuesIterator();
58
            }
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function rewind()
66
    {
67
        $xls = $this->getExcelObject();
68
        $this->worksheetIterator = new \CallbackFilterIterator(
69
            new \ArrayIterator($xls->getWorksheets()),
70
            function ($title, $key) use ($xls) {
71
                return $this->isReadableWorksheet($title);
72
73
                return false;
74
            }
75
        );
76
        $this->worksheetIterator->rewind();
77
        if ($this->worksheetIterator->valid()) {
78
            $this->initializeValuesIterator();
79
        } else {
80
            $this->valuesIterator = null;
81
        }
82
    }
83
84
    /**
85
     * Returns the associated Excel object
86
     *
87
     * @return SpreadsheetInterface
88
     */
89
    public function getExcelObject()
90
    {
91
        if (!$this->xls) {
92
            $this->xls = $this->getWorkbookLoader()->open($this->filePath);
93
        }
94
95
        return $this->xls;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function valid()
102
    {
103
        return $this->worksheetIterator->valid() && $this->valuesIterator && $this->valuesIterator->valid();
104
    }
105
106
    /**
107
     * Initializes the current worksheet
108
     */
109
    protected function initializeValuesIterator()
110
    {
111
        $this->valuesIterator = $this->createValuesIterator();
112
113
        if (!$this->valuesIterator->valid()) {
114
            $this->valuesIterator = null;
115
            $this->worksheetIterator->next();
116
            if ($this->worksheetIterator->valid()) {
117
                $this->initializeValuesIterator();
118
            }
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function setContainer(ContainerInterface $container = null)
126
    {
127
        $this->container = $container;
128
    }
129
130
    /**
131
     * Returns true if the worksheet should be read
132
     *
133
     * @param string $title
134
     *
135
     * @return boolean
136
     */
137
    protected function isReadableWorksheet($title)
138
    {
139
        return $this->isIncludedWorksheet($title) && !$this->isExcludedWorksheet($title);
140
    }
141
142
    /**
143
     * Returns true if the worksheet should be indluded
144
     *
145
     * @param string $title The title of the worksheet
146
     *
147
     * @return boolean
148
     */
149
    protected function isIncludedWorksheet($title)
150
    {
151
        if (!isset($this->options['include_worksheets'])) {
152
            return true;
153
        }
154
155
        foreach ($this->options['include_worksheets'] as $regexp) {
156
            if (preg_match($regexp, $title)) {
157
                return true;
158
            }
159
        }
160
161
        return false;
162
    }
163
164
    /**
165
     * Returns true if the worksheet should be excluded
166
     *
167
     * @param string $title The title of the worksheet
168
     *
169
     * @return boolean
170
     */
171
    protected function isExcludedWorksheet($title)
172
    {
173
        foreach ($this->options['exclude_worksheets'] as $regexp) {
174
            if (preg_match($regexp, $title)) {
175
                return true;
176
            }
177
        }
178
179
        return false;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    protected function setDefaultOptions(OptionsResolver $resolver)
186
    {
187
        $resolver->setDefaults(
188
            array(
189
                'exclude_worksheets' => array(),
190
                'parser_options'     => array()
191
            )
192
        );
193
        $resolver->setDefined(array('include_worksheets'));
194
    }
195
196
    /**
197
     * Returns the Array Helper service
198
     *
199
     * @return ArrayHelper
200
     */
201
    protected function getArrayHelper()
202
    {
203
        return $this->container->get('pim_excel_connector.iterator.array_helper');
204
    }
205
206
    /**
207
     * Returns the workbook reader
208
     *
209
     * @return SpreadsheetLoader
210
     */
211
    protected function getWorkbookLoader()
212
    {
213
        return $this->container->get('akeneo_spreadsheet_parser.spreadsheet_loader');
214
    }
215
216
    /**
217
     * Returns an iterator for the specified worksheet
218
     *
219
     * @param int $worksheetIndex
220
     *
221
     * @return \Iterator
222
     */
223
    protected function createIterator($worksheetIndex)
224
    {
225
       return $this->getExcelObject()->createRowIterator($worksheetIndex, $this->options['parser_options']);
226
    }
227
228
    /**
229
     * Creates the value iterator
230
     *
231
     * @return Iterator
232
     */
233
    abstract protected function createValuesIterator();
234
}
235