Completed
Push — master ( f28df4...c293b4 )
by Babak
05:02 queued 38s
created

ExcelHelper::getInnerValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
nc 2
nop 2
dl 0
loc 9
c 0
b 0
f 0
cc 2
rs 9.6666
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\ExcelHelper;
10
11
use Maatwebsite\Excel\Facades\Excel;
12
13
class ExcelHelper
14
{
15
    protected $options = [
16
        'store_format' => 'xls',
17
        'download_format' => 'xls',
18
        'title' => 'title',
19
        'creator' => 'user',
20
        'company' => 'Alive Co',
21
        'description' => 'this is excel file from table',
22
        'sheet' => 'sheet1',
23
    ];
24
25
    protected $excel = '';
26
27
    protected $table = '';
28
29
    protected $arrayTable = [];
30
31
    /**
32
     * ExcelHelper constructor.
33
     */
34
    public function __construct()
35
    {
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getOptions()
42
    {
43
        return $this->options;
44
    }
45
46
    /**
47
     * @param array $options
48
     * @return $this
49
     */
50
    public function setOptions($options)
51
    {
52
        $this->options = array_merge($this->options, $options);
53
        return $this;
54
    }
55
56
    /**
57
     * download excel
58
     */
59
    public function download()
60
    {
61
        $this->excel->store($this->options['store_format'])->download($this->options['download_format']);
62
    }
63
64
    /**
65
     * @param $arrayTable
66
     */
67
    public function exportTable($arrayTable)
68
    {
69
        $this->arrayTable = $arrayTable;
70
        $this->table();
71
        $this->options;
72
        $this->createExcelFile();
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function createExcelFile()
79
    {
80
        $options = $this->options;
81
        $table = $this->table;
82
        $this->excel = Excel::create('payments', function ($excel) use ($table, $options) {
83
            $excel->setTitle($options['title']);
84
            $excel->setCreator($options['creator'])->setCompany($options['company']);
85
            $excel->setDescription($options['description']);
86
            $excel->sheet("sheet", function ($sheet) use ($table) {
87
                $sheet->fromArray($table, null, 'A1', false, false);
88
            });
89
        });
90
        return $this;
91
    }
92
93
    /**
94
     * @param $arrayTables
95
     * @return array
96
     * @internal param string $key
97
     * @internal param $arrayTable
98
     */
99
    public function getTitleOfArrayTable($arrayTables)
100
    {
101
        $result = [];
102
        foreach ($arrayTables as $arrayTable) {
103
            list($titles, $key) = $this->titleCrawler($arrayTable);
104
            foreach ($titles as $title) {
105
                if (!is_int(array_search($title, $result))) {
106
                    array_push($result, $title);
107
                }
108
            }
109
        }
110
        return $result;
111
    }
112
113
    /**
114
     * @param $arrayTable
115
     * @param string $parentKey
116
     * @return array
117
     */
118
    public function titleCrawler($arrayTable, $parentKey = '')
119
    {
120
        $result = [];
121
        $titlesKey = '';
122
        foreach ($arrayTable as $key => $value) {
123
            if (is_array($value)) {
124
                list($titles, $titlesKey) = $this->titleCrawler($value, $parentKey == '' ? $key : $parentKey . '.' . $key);
125
                foreach ($titles as $titleKey => $titleValue) {
126
                    array_push($result, $titlesKey == '' ? $titleValue : $titlesKey . '.' . $titleValue);
127
                }
128
            } else {
129
                array_push($result, $parentKey == '' ? $key : $parentKey . '.' . $key);
130
            }
131
        }
132
        return array($result, $titlesKey);
133
    }
134
135
    /**
136
     * @param array $titles
137
     * @param bool $setHeader
138
     * @return $this
139
     */
140
    public function table($titles = [], $setHeader = true)
141
    {
142
        $arrayTable = $this->arrayTable;
143
        $result = [];
144
        if (count($titles) == 0) {
145
            $titles = $this->getTitleOfArrayTable($arrayTable);
146
        }
147
        if ($setHeader) {
148
            array_push($result, $titles);
149
        }
150
        foreach ($arrayTable as $item) {
151
            $arrayTableRecord = [];
152
            foreach ($titles as $title) {
153
                if (array_key_exists(explode('.', $title)[0], $item)) {
154
                    array_push($arrayTableRecord, $this->getInnerValue($item, $title));
155
                } else {
156
                    array_push($arrayTableRecord, null);
157
                }
158
            }
159
            array_push($result, $arrayTableRecord);
160
        }
161
        $this->table = $result;
162
        return $this;
163
    }
164
165
    /**
166
     * @param $array
167
     * @param $key
168
     * @return mixed
169
     */
170
    public function getInnerValue($array, $key)
171
    {
172
        $keyTree = explode('.', $key);
173
        if (count($keyTree) > 1) {
174
            $key = $keyTree[0];
175
            unset($keyTree[0]);
176
            return $this->getInnerValue($array[$key], implode('.', $keyTree));
177
        } else {
178
            return $array[$keyTree[0]];
179
        }
180
    }
181
182
    /**
183
     * @param $arrayTable
184
     * @return mixed
185
     */
186
    public function arrayToTable($arrayTable)
187
    {
188
        $this->getTitleOfArrayTable($arrayTable);
189
        $this->table($arrayTable);
190
        return $arrayTable;
191
    }
192
}