Issues (4)

src/ExcelHelper.php (1 issue)

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alive
5
 * Date: 4/17/18
6
 * Time: 2:58 AM
7
 */
8
9
namespace Alive2212\LaravelExcelHelper;
10
11
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
12
use Maatwebsite\Excel\Facades\Excel;
13
use Maatwebsite\Excel\Writers\LaravelExcelWriter;
14
15
class ExcelHelper
16
{
17
    protected $options = [
18
        'store_format' => 'xls',
19
        'download_format' => 'xls',
20
        'title' => 'title',
21
        'creator' => 'user',
22
        'company' => 'Alive Co',
23
        'description' => 'this is excel file from table',
24
        'sheet' => 'sheet1',
25
    ];
26
27
    protected $excel = '';
28
29
    protected $table = '';
30
31
    protected $arrayTable = [];
32
33
    /**
34
     * ExcelHelper constructor.
35
     */
36 1
    public function __construct()
37
    {
38 1
    }
39
40 1
    static function create(){
41
//        dd("Taylor Swift felt to Babak Nodoust Love");
42 1
    }
43
44
    /**
45
     * @return array
46
     */
47 1
    public function getOptions()
48
    {
49 1
        return $this->options;
50
    }
51
52
    /**
53
     * @param array $options
54
     * @return $this
55
     */
56 1
    public function setOptions($options)
57
    {
58 1
        $this->options = array_merge($this->options, $options);
59 1
        return $this;
60
    }
61
62
    /**
63
     * download excel
64
     */
65
    public function download()
66
    {
67
        return $this->excel->store($this->options['store_format'])->download($this->options['download_format']);
68
    }
69
70
    /**
71
     * @param $arrayTable
72
     */
73 1
    public function exportTable($arrayTable)
74
    {
75 1
        $this->arrayTable = $arrayTable;
76 1
        $this->table();
77 1
        $this->options;
78 1
        $this->createExcelFile();
79 1
    }
80
81 1
    public function createExcelFile()
82
    {
83 1
        $options = $this->options;
84 1
        $table = $this->table;
85 1
        $this->excel = Excel::create('payments', function (LaravelExcelWriter $excel) use ($table, $options) {
86
            $excel->setTitle($options['title']);
87
            $excel->setCreator($options['creator'])->setCompany($options['company']);
88
            $excel->setDescription($options['description']);
89
            $closer = function (LaravelExcelWorksheet $sheet) use ($table){
90
                $sheet->fromArray($table, null, 'A1', false, false);
0 ignored issues
show
$table of type string is incompatible with the type array expected by parameter $source of Maatwebsite\Excel\Classe...lWorksheet::fromArray(). ( Ignorable by Annotation )

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

90
                $sheet->fromArray(/** @scrutinizer ignore-type */ $table, null, 'A1', false, false);
Loading history...
91
            };
92
            $excel->sheet("sheet", $closer);
93 1
        });
94 1
        return $this;
95
    }
96
97
    /**
98
     * @param $arrayTables
99
     * @return array
100
     * @internal param string $key
101
     * @internal param $arrayTable
102
     */
103 1
    public function getTitleOfArrayTable($arrayTables)
104
    {
105 1
        $result = [];
106 1
        foreach ($arrayTables as $arrayTable) {
107 1
            list($titles, $key) = $this->titleCrawler($arrayTable);
108 1
            foreach ($titles as $title) {
109 1
                if (!is_int(array_search($title, $result))) {
110 1
                    array_push($result, $title);
111
                }
112
            }
113
        }
114 1
        return $result;
115
    }
116
117
    /**
118
     * @param $arrayTable
119
     * @param string $parentKey
120
     * @return array
121
     */
122 1
    public function titleCrawler($arrayTable, $parentKey = '')
123
    {
124 1
        $result = [];
125 1
        $titlesKey = '';
126 1
        foreach ($arrayTable as $key => $value) {
127 1
            if (is_array($value)) {
128 1
                list($titles, $titlesKey) = $this->titleCrawler($value, $parentKey == '' ? $key : $parentKey . '.' . $key);
129 1
                foreach ($titles as $titleKey => $titleValue) {
130 1
                    array_push($result, $titlesKey == '' ? $titleValue : $titlesKey . '.' . $titleValue);
131
                }
132
            } else {
133 1
                array_push($result, $parentKey == '' ? $key : $parentKey . '.' . $key);
134
            }
135
        }
136 1
        return array($result, $titlesKey);
137
    }
138
139
    /**
140
     * @param array $titles
141
     * @param bool $setHeader
142
     * @return $this
143
     */
144 1
    public function table($titles = [], $setHeader = true)
145
    {
146 1
        $arrayTable = $this->arrayTable;
147 1
        $result = [];
148 1
        if (count($titles) == 0) {
149 1
            $titles = $this->getTitleOfArrayTable($arrayTable);
150
        }
151 1
        if ($setHeader) {
152 1
            array_push($result, $titles);
153
        }
154 1
        foreach ($arrayTable as $item) {
155 1
            $arrayTableRecord = [];
156 1
            foreach ($titles as $title) {
157 1
                if (array_key_exists(explode('.', $title)[0], $item)) {
158 1
                    array_push($arrayTableRecord, $this->getInnerValue($item, $title));
159
                } else {
160 1
                    array_push($arrayTableRecord, null);
161
                }
162
            }
163 1
            array_push($result, $arrayTableRecord);
164
        }
165 1
        $this->table = $result;
166 1
        return $this;
167
    }
168
169
    /**
170
     * @param $array
171
     * @param $key
172
     * @return mixed
173
     */
174 1
    public function getInnerValue($array, $key)
175
    {
176 1
        $keyTree = explode('.', $key);
177 1
        if (count($keyTree) > 1) {
178 1
            $key = $keyTree[0];
179 1
            unset($keyTree[0]);
180 1
            return $this->getInnerValue($array[$key], implode('.', $keyTree));
181
        } else {
182 1
            return $array[$keyTree[0]];
183
        }
184
    }
185
186
    /**
187
     * @param $arrayTable
188
     * @return mixed
189
     */
190
    public function arrayToTable($arrayTable)
191
    {
192
        $this->getTitleOfArrayTable($arrayTable);
193
        $this->table($arrayTable);
194
        return $arrayTable;
195
    }
196
197
    /**
198
     * @param $name
199
     * @param $arguments
200
     * @return mixed
201
     */
202
    public static function __callStatic($name, $arguments)
203
    {
204
        return (new static)->$name(...$arguments);
205
    }
206
207
    /**
208
     * @return array
209
     */
210 1
    public function getArrayTable(): array
211
    {
212 1
        return $this->arrayTable;
213
    }
214
215
    /**
216
     * @param array $arrayTable
217
     */
218 1
    public function setArrayTable(array $arrayTable)
219
    {
220 1
        $this->arrayTable = $arrayTable;
221 1
    }
222
223
    /**
224
     * @return string
225
     */
226 1
    public function getExcel()
227
    {
228 1
        return $this->excel;
229
    }
230
231
    /**
232
     * @param string $excel
233
     */
234 1
    public function setExcel(string $excel)
235
    {
236 1
        $this->excel = $excel;
237 1
    }
238
239
    /**
240
     * @return string
241
     */
242
    public function getTable()
243
    {
244
        return $this->table;
245
    }
246
247
    /**
248
     * @param string $table
249
     */
250
    public function setTable(string $table)
251
    {
252
        $this->table = $table;
253
    }
254
255
}