Skip to content

Commit e463ebf

Browse files
committed
seperate exporter for seperate data structure
1 parent 33cf0b5 commit e463ebf

File tree

8 files changed

+272
-166
lines changed

8 files changed

+272
-166
lines changed

src/Contracts/ExporterContract.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace Sujan\Exporter\Contracts;
5+
6+
7+
8+
interface ExporterContract
9+
{
10+
public function set();
11+
public function get();
12+
}

src/Export.php

Lines changed: 49 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -5,193 +5,84 @@
55

66

77
use Exception;
8-
use Generator;
9-
use Illuminate\Database\Eloquent\Builder;
10-
use Illuminate\Database\Eloquent\Collection;
8+
use Sujan\Exporter\Contracts\ExporterContract;
119

1210
class Export
1311
{
1412
/**
15-
* @var Builder|null
13+
* @var array|object
1614
*/
17-
private $model;
15+
protected $model;
1816

1917
/**
2018
* @var array
2119
*/
22-
private $heading = [];
20+
protected $columns;
2321

24-
/**
22+
/*
2523
* @var array
2624
*/
27-
private $columns;
25+
private $heading = [];
2826

2927
/**
3028
* @var string
3129
*/
32-
private $contentType = 'application/csv';
30+
protected $filename;
3331

34-
/**
35-
* @var string
32+
/*
33+
* Array of content types
3634
*/
37-
private $filename;
35+
private $contentTypes = [
36+
'.csv' => 'application/csv',
37+
'.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
38+
];
39+
40+
private $contentType;
3841

3942
/**
40-
* @var string
43+
* @var false|resource
4144
*/
42-
private $delimiter = ";";
45+
protected $file;
4346

4447
/**
45-
* Export constructor.
46-
* @param object | array $model
47-
* @param array $columns
48-
* @param string $filename
48+
* @var string
4949
*/
50-
public function __construct(
51-
$model,
52-
array $columns = [],
53-
string $filename = 'export.csv'
54-
)
50+
protected $delimiter = ";";
51+
52+
protected function setConfiguration($model, array $columns, string $filename)
5553
{
5654
$this->setModel($model);
5755
$this->setColumns($columns);
5856
$this->setHeading($columns);
5957
$this->setFilename($filename);
6058
$this->setContentType();
59+
$this->openOutputStream();
6160
}
6261

6362
/**
64-
* Set content type
65-
*/
66-
public function setContentType()
67-
{
68-
header("Content-Type: {$this->contentType}");
69-
header("Content-Disposition: attachment; filename={$this->filename};");
70-
}
71-
72-
/**
73-
* return generated CSV
74-
* @throws Exception
75-
*/
76-
public function export()
77-
{
78-
$generator = $this->write();
79-
80-
while ($generator->valid()) {
81-
$generator->next();
82-
}
83-
84-
die();
85-
}
86-
87-
/**
88-
* @return Generator
89-
* @throws Exception
90-
*/
91-
public function write()
92-
{
93-
// open the "output" stream
94-
$file = fopen('php://output', 'w');
95-
96-
fputcsv($file, $this->heading, $this->delimiter);
97-
98-
if (is_array($this->model)) {
99-
foreach ($this->model as $data) {
100-
$line = $this->getLine($data);
101-
102-
yield fputcsv($file, $line, $this->delimiter);
103-
}
104-
} else {
105-
$className = !is_string($this->model) ? class_basename($this->model) : null;
106-
107-
switch ($className) {
108-
case 'Collection':
109-
foreach ($this->model as $data) {
110-
$line = $this->getLine($data);
111-
112-
yield fputcsv($file, $line, $this->delimiter);
113-
}
114-
break;
115-
case 'Builder':
116-
foreach ($this->model->get() as $data) {
117-
$line = $this->getLine($data);
118-
119-
yield fputcsv($file, $line, $this->delimiter);
120-
}
121-
break;
122-
case 'PDOStatement':
123-
foreach ($this->model->fetchAll() as $data) {
124-
$line = $this->getLine($data);
125-
126-
yield fputcsv($file, $line, $this->delimiter);
127-
}
128-
break;
129-
default:
130-
throw new Exception('Type unknown');
131-
}
132-
}
133-
134-
fclose($file);
135-
}
136-
137-
/**
138-
* @param $data
139-
* @return array
140-
*/
141-
public function getLine($data)
142-
{
143-
$line = [];
144-
145-
foreach ($this->columns as $k => $key) {
146-
if (is_array($key)) {
147-
$value = $this->getNestedData($data, $key, $k);
148-
array_push($line, $value);
149-
} else {
150-
$value = is_array($data) ? $data[$key] : $data->{$key};
151-
array_push($line, $value);
152-
}
153-
}
154-
155-
return $line;
156-
}
157-
158-
/**
159-
* @param $data
160-
* @param $keys
161-
* @param $k
162-
* @return string
63+
* @param object | array $model
16364
*/
164-
public function getNestedData($data, $keys, $k)
65+
protected function setModel($model): void
16566
{
166-
foreach ($keys as $kk => $key) {
167-
if (is_array($data)) {
168-
$data = isset($data[$k][$key]) ? $data[$k][$key] : '';
169-
} else {
170-
$data = isset($data->{$k}->{$key}) ? $data->{$k}->{$key} : '';
171-
}
172-
173-
if (is_array($data)) {
174-
$this->getNestedData($data, $key, $kk);
175-
}
176-
}
177-
178-
return $data;
67+
$this->model = $model;
17968
}
18069

18170
/**
182-
* @param string $delimiter
71+
* @param array $columns
18372
*/
184-
protected function setDelimiter(string $delimiter): void
73+
public function setColumns(array $columns): void
18574
{
186-
$this->delimiter = $delimiter;
75+
$this->columns = $columns;
18776
}
18877

18978
/**
190-
* @param object | array $model
79+
* @param array $heading
19180
*/
192-
protected function setModel($model): void
81+
protected function setHeading(array $heading): void
19382
{
194-
$this->model = $model;
83+
array_walk_recursive($heading, function ($item) {
84+
array_push($this->heading, $item);
85+
});
19586
}
19687

19788
/**
@@ -203,20 +94,26 @@ protected function setFilename(string $filename): void
20394
}
20495

20596
/**
206-
* @param array $heading
97+
* Set content type
20798
*/
208-
protected function setHeading(array $heading): void
99+
public function setContentType()
209100
{
210-
array_walk_recursive($heading, function ($item) {
211-
array_push($this->heading, $item);
212-
});
101+
preg_match('/(?:.csv|.xlsx)/i', $this->filename, $parts);
102+
103+
if (!$parts[0]) {
104+
$this->filename = $this->filename . '.csv';
105+
$this->contentType = $this->contentTypes['.csv'];
106+
} else {
107+
$this->contentType = $this->contentTypes[strtolower($parts[0])];
108+
}
109+
110+
header("Content-Type: {$this->contentType}");
111+
header("Content-Disposition: attachment; filename={$this->filename};");
213112
}
214113

215-
/**
216-
* @param array $columns
217-
*/
218-
public function setColumns(array $columns): void
114+
private function openOutputStream()
219115
{
220-
$this->columns = $columns;
116+
// open the "output" stream
117+
$this->file = fopen('php://output', 'w');
221118
}
222119
}

src/ExportFromArray.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace Sujan\Exporter;
5+
6+
7+
use Sujan\Exporter\Contracts\ExporterContract;
8+
use Sujan\Exporter\Traits\ExportGenerator;
9+
10+
11+
class ExportFromArray extends Export implements ExporterContract
12+
{
13+
use ExportGenerator;
14+
15+
public function __construct($model, $columns, $filename)
16+
{
17+
$this->setConfiguration($model, $columns, $filename);
18+
}
19+
20+
public function set()
21+
{
22+
foreach ($this->model as $data) {
23+
$line = $this->getLine($data, $this->columns);
24+
25+
yield fputcsv($this->file, $line, $this->delimiter);
26+
}
27+
}
28+
29+
public function get()
30+
{
31+
$generator = $this->set();
32+
33+
while ($generator->valid()) {
34+
$generator->next();
35+
}
36+
37+
die();
38+
}
39+
}

src/ExportFromPDOStatement.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace Sujan\Exporter;
5+
6+
7+
use Sujan\Exporter\Contracts\ExporterContract;
8+
use Sujan\Exporter\Traits\ExportGenerator;
9+
10+
11+
class ExportFromPDOStatement extends Export implements ExporterContract
12+
{
13+
use ExportGenerator;
14+
15+
public function __construct($model, $columns, $filename)
16+
{
17+
$this->setConfiguration($model, $columns, $filename);
18+
}
19+
20+
public function set()
21+
{
22+
foreach ($this->model->fetchAll() as $data) {
23+
$line = $this->getLine($data, $this->columns);
24+
25+
yield fputcsv($this->file, $line, $this->delimiter);
26+
}
27+
}
28+
29+
public function get()
30+
{
31+
$generator = $this->set();
32+
33+
while ($generator->valid()) {
34+
$generator->next();
35+
}
36+
37+
die();
38+
}
39+
}

src/ExportFromQueryBuilder.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace Sujan\Exporter;
5+
6+
7+
use Sujan\Exporter\Contracts\ExporterContract;
8+
use Sujan\Exporter\Traits\ExportGenerator;
9+
10+
11+
class ExportFromQueryBuilder extends Export implements ExporterContract
12+
{
13+
use ExportGenerator;
14+
15+
public function __construct($model, $columns, $filename)
16+
{
17+
$this->setConfiguration($model, $columns, $filename);
18+
}
19+
20+
public function set()
21+
{
22+
foreach ($this->model->get() as $data) {
23+
$line = $this->getLine($data, $this->columns);
24+
25+
yield fputcsv($this->file, $line, $this->delimiter);
26+
}
27+
}
28+
29+
public function get()
30+
{
31+
$generator = $this->set();
32+
33+
while ($generator->valid()) {
34+
$generator->next();
35+
}
36+
37+
die();
38+
}
39+
}

0 commit comments

Comments
 (0)