Skip to content

Commit 72acab3

Browse files
committed
Initial commit
0 parents  commit 72acab3

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "sujan/exporter",
3+
"description": "A PHP library to export data to csv, xlsx etc",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Sujan Mahmud",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "dev",
13+
"require": {},
14+
"autoload": {
15+
"psr-4": {
16+
"Sujan\\Exporter\\": "src/"
17+
}
18+
}
19+
}

src/Export.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
4+
namespace Sujan\Exporter;
5+
6+
7+
use Generator;
8+
use Illuminate\Database\Eloquent\Builder;
9+
10+
class Export
11+
{
12+
/**
13+
* @var Builder|null
14+
*/
15+
private $model;
16+
17+
/**
18+
* @var array
19+
*/
20+
private $heading = [];
21+
22+
/**
23+
* @var array
24+
*/
25+
private $columns;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $contentType;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $filename;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $delimiter;
41+
42+
/**
43+
* Export constructor.
44+
* @param Builder|null $model
45+
* @param array $columns
46+
* @param string $contentType
47+
* @param string $filename
48+
* @param string $delimiter
49+
*/
50+
public function __construct(
51+
Builder $model = null,
52+
array $columns = [],
53+
string $contentType = 'application/csv',
54+
string $filename = 'export.csv',
55+
string $delimiter = ";"
56+
)
57+
{
58+
$this->setModel($model);
59+
$this->setColumns($columns);
60+
$this->setHeading($columns);
61+
$this->setFilename($filename);
62+
$this->setContentType($contentType);
63+
$this->setDelimiter($delimiter);
64+
}
65+
66+
/**
67+
* @param $contentType
68+
*/
69+
public function setContentType($contentType)
70+
{
71+
header("Content-Type: {$contentType}");
72+
header("Content-Disposition: attachment; filename={$this->filename};");
73+
}
74+
75+
/**
76+
* return generated CSV
77+
*/
78+
public function export()
79+
{
80+
$generator = $this->write();
81+
82+
while ($generator->valid()) {
83+
$generator->next();
84+
}
85+
}
86+
87+
/**
88+
* @return Generator
89+
*/
90+
public function write()
91+
{
92+
// open the "output" stream
93+
$file = fopen('php://output', 'w');
94+
95+
fputcsv($file, $this->heading, $this->delimiter);
96+
97+
foreach ($this->model->get() as $data) {
98+
$line = $this->getLine($data);
99+
100+
yield fputcsv($file, $line, $this->delimiter);
101+
}
102+
103+
fclose($file);
104+
}
105+
106+
/**
107+
* @param $data
108+
* @return array
109+
*/
110+
public function getLine($data)
111+
{
112+
$line = [];
113+
114+
foreach ($this->columns as $k => $key) {
115+
if (is_array($key)) {
116+
$value = $this->getNestedData($data, $key, $k);
117+
array_push($line, $value);
118+
} else {
119+
array_push($line, $data->{$key});
120+
}
121+
}
122+
123+
return $line;
124+
}
125+
126+
/**
127+
* @param $data
128+
* @param $keys
129+
* @param $k
130+
* @return string
131+
*/
132+
public function getNestedData($data, $keys, $k)
133+
{
134+
foreach ($keys as $kk => $key) {
135+
$data = isset($data->{$k}->{$key}) ? $data->{$k}->{$key} : '';
136+
137+
if (is_array($data)) {
138+
$this->getNestedData($data, $key, $kk);
139+
}
140+
}
141+
142+
return $data;
143+
}
144+
145+
/**
146+
* @param string $delimiter
147+
*/
148+
protected function setDelimiter(string $delimiter): void
149+
{
150+
$this->delimiter = $delimiter;
151+
}
152+
153+
/**
154+
* @param Builder $model
155+
*/
156+
protected function setModel(Builder $model): void
157+
{
158+
$this->model = $model;
159+
}
160+
161+
/**
162+
* @param string $filename
163+
*/
164+
protected function setFilename(string $filename): void
165+
{
166+
$this->filename = $filename;
167+
}
168+
169+
/**
170+
* @param array $heading
171+
*/
172+
protected function setHeading(array $heading): void
173+
{
174+
array_walk_recursive($heading, function ($item) {
175+
array_push($this->heading, $item);
176+
});
177+
}
178+
179+
/**
180+
* @param array $columns
181+
*/
182+
public function setColumns(array $columns): void
183+
{
184+
$this->columns = $columns;
185+
}
186+
}

0 commit comments

Comments
 (0)