AbstractExcelBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Excel\Builder;
4
5
use PHPExcel;
6
use PHPExcel_Worksheet;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Base excel builder
11
 *
12
 * @author    Antoine Guigan <[email protected]>
13
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
14
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
15
 */
16
abstract class AbstractExcelBuilder implements ExcelBuilderInterface
17
{
18
    /** @var array */
19
    protected $rowIndexes;
20
21
    /** @var array */
22
    protected $labels;
23
24
    /** @var array */
25
    protected $options;
26
27
    /** @var PHPExcel */
28
    protected $xls;
29
30
    /** @var boolean */
31
    protected $clean = false;
32
33
    /**
34
     * @param array $options
35
     */
36
    public function __construct(array $options = array())
37
    {
38
        $resolver = new OptionsResolver();
39
        $this->setDefaultOptions($resolver);
40
        $this->options = $resolver->resolve($options);
41
        $this->xls = new PHPExcel();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function add(array $item)
48
    {
49
        $worksheet = $this->getWorksheet($item);
50
        $this->writeLabels($worksheet, $item);
51
        $this->writeValues($worksheet, $item);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getExcelObject()
58
    {
59
        if (!$this->clean) {
60
            $this->cleanup();
61
        }
62
63
        return $this->xls;
64
    }
65
66
    /**
67
     * Returns the worksheet name for a set of data
68
     *
69
     * @param array $data
70
     *
71
     * @return string
72
     */
73
    abstract protected function getWorksheetName(array $data);
74
75
    /**
76
     * Returns the worksheet for a set of data
77
     *
78
     * @param array $data
79
     *
80
     * @return \PHPExcel_Worksheet
81
     */
82
    protected function getWorksheet(array $data)
83
    {
84
        $name = $this->getWorksheetName($data);
85
        if (!isset($this->labels[$name])) {
86
            return $this->createWorksheet($name, $data);
87
        } else {
88
            return $this->xls->getSheetByName($name);
89
        }
90
    }
91
92
    /**
93
     * Creates a worksheet with the given name
94
     *
95
     * @param string $name
96
     * @param array  $data
97
     *
98
     * @return PHPExcel_Worksheet
99
     */
100
    protected function createWorksheet($name, array $data)
101
    {
102
        $worksheet = $this->xls->createSheet();
103
        $worksheet->setTitle($name);
104
        $this->rowIndexes[$name] = $this->options['data_row'];
105
        $this->labels[$name] = [];
106
107
        return $worksheet;
108
    }
109
110
    /**
111
     * Cleanup the Excel file
112
     */
113
    protected function cleanup()
114
    {
115
        if (count($this->labels)) {
116
            $this->xls->removeSheetByIndex(0);
117
        }
118
    }
119
120
    /**
121
     * Writes labels for the submitted data
122
     *
123
     * @param PHPExcel_Worksheet $worksheet
124
     * @param array               $data
125
     */
126
    protected function writeLabels(PHPExcel_Worksheet $worksheet, array $data)
127
    {
128
        $worksheetName = $worksheet->getTitle();
129
        $missing = array_diff(array_keys($data), $this->labels[$worksheetName]);
130
        $column = count($this->labels[$worksheetName]);
131
        foreach ($missing as $label) {
132
            $this->labels[$worksheetName][] = $label;
133
            $worksheet->setCellValueByColumnAndRow($column, $this->options['label_row'], $label);
134
            $column++;
135
        }
136
    }
137
138
    /**
139
     * Writes a row of values
140
     *
141
     * @param PHPExcel_Worksheet $worksheet
142
     * @param array               $data      An array of values with column indexes as keys
143
     */
144
    protected function writeValues(PHPExcel_Worksheet $worksheet, array $data)
145
    {
146
        $worksheetName = $worksheet->getTitle();
147
        $row = $this->rowIndexes[$worksheetName];
148
        foreach ($this->labels[$worksheet->getTitle()] as $column => $label) {
149
            if (isset($data[$label])) {
150
                $worksheet->setCellValueByColumnAndRow($column, $row, $data[$label]);
151
            }
152
        }
153
154
        $this->rowIndexes[$worksheetName]++;
155
    }
156
157
    /**
158
     * Sets the default options
159
     *
160
     * @param OptionsResolver $resolver
161
     */
162
    protected function setDefaultOptions(OptionsResolver $resolver)
163
    {
164
        $resolver->setDefaults(
165
            array(
166
                'label_row' => 1,
167
                'data_row'  => 2
168
            )
169
        );
170
    }
171
}
172