Skip to content

Commit b1cef5b

Browse files
committed
Export from collection, array and PDOStatement added
1 parent e89b0d7 commit b1cef5b

File tree

4 files changed

+106
-22
lines changed

4 files changed

+106
-22
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
}
1111
],
1212
"minimum-stability": "dev",
13-
"require": {},
13+
"require": {
14+
"php": "^5.5",
15+
"ext-pdo": "*"
16+
},
1417
"autoload": {
1518
"psr-4": {
1619
"Sujan\\Exporter\\": "src/"

src/Export.php

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
namespace Sujan\Exporter;
55

66

7+
use Exception;
78
use Generator;
89
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\Collection;
911

1012
class Export
1113
{
@@ -27,7 +29,7 @@ class Export
2729
/**
2830
* @var string
2931
*/
30-
private $contentType;
32+
private $contentType = 'application/csv';
3133

3234
/**
3335
* @var string
@@ -37,43 +39,39 @@ class Export
3739
/**
3840
* @var string
3941
*/
40-
private $delimiter;
42+
private $delimiter = ";";
4143

4244
/**
4345
* Export constructor.
44-
* @param Builder|null $model
46+
* @param object | array $model
4547
* @param array $columns
46-
* @param string $contentType
4748
* @param string $filename
48-
* @param string $delimiter
4949
*/
5050
public function __construct(
51-
Builder $model = null,
51+
$model,
5252
array $columns = [],
53-
string $contentType = 'application/csv',
54-
string $filename = 'export.csv',
55-
string $delimiter = ";"
53+
string $filename = 'export.csv'
5654
)
5755
{
5856
$this->setModel($model);
5957
$this->setColumns($columns);
6058
$this->setHeading($columns);
6159
$this->setFilename($filename);
62-
$this->setContentType($contentType);
63-
$this->setDelimiter($delimiter);
60+
$this->setContentType();
6461
}
6562

6663
/**
67-
* @param $contentType
64+
* Set content type
6865
*/
69-
public function setContentType($contentType)
66+
public function setContentType()
7067
{
71-
header("Content-Type: {$contentType}");
68+
header("Content-Type: {$this->contentType}");
7269
header("Content-Disposition: attachment; filename={$this->filename};");
7370
}
7471

7572
/**
7673
* return generated CSV
74+
* @throws Exception
7775
*/
7876
public function export()
7977
{
@@ -82,10 +80,13 @@ public function export()
8280
while ($generator->valid()) {
8381
$generator->next();
8482
}
83+
84+
die();
8585
}
8686

8787
/**
8888
* @return Generator
89+
* @throws Exception
8990
*/
9091
public function write()
9192
{
@@ -94,10 +95,40 @@ public function write()
9495

9596
fputcsv($file, $this->heading, $this->delimiter);
9697

97-
foreach ($this->model->get() as $data) {
98-
$line = $this->getLine($data);
98+
if (is_array($this->model)) {
99+
foreach ($this->model as $data) {
100+
$line = $this->getLine($data);
99101

100-
yield fputcsv($file, $line, $this->delimiter);
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+
}
101132
}
102133

103134
fclose($file);
@@ -116,7 +147,8 @@ public function getLine($data)
116147
$value = $this->getNestedData($data, $key, $k);
117148
array_push($line, $value);
118149
} else {
119-
array_push($line, $data->{$key});
150+
$value = is_array($data) ? $data[$key] : $data->{$key};
151+
array_push($line, $value);
120152
}
121153
}
122154

@@ -132,7 +164,11 @@ public function getLine($data)
132164
public function getNestedData($data, $keys, $k)
133165
{
134166
foreach ($keys as $kk => $key) {
135-
$data = isset($data->{$k}->{$key}) ? $data->{$k}->{$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+
}
136172

137173
if (is_array($data)) {
138174
$this->getNestedData($data, $key, $kk);
@@ -151,9 +187,9 @@ protected function setDelimiter(string $delimiter): void
151187
}
152188

153189
/**
154-
* @param Builder $model
190+
* @param object | array $model
155191
*/
156-
protected function setModel(Builder $model): void
192+
protected function setModel($model): void
157193
{
158194
$this->model = $model;
159195
}

src/Exporter.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
4+
namespace Sujan\Exporter;
5+
6+
7+
use Exception;
8+
9+
class Exporter
10+
{
11+
protected static $exporter;
12+
13+
/**
14+
* @param $model
15+
* @param array $columns
16+
* @param string $filename
17+
* @throws Exception
18+
*/
19+
public static function init($model, array $columns, string $filename): void
20+
{
21+
if (gettype($model) == 'string') throw new Exception('Type string for model is not allowed.');
22+
23+
self::$exporter = new Export($model, $columns, $filename);
24+
}
25+
26+
/**
27+
* Export the data and die
28+
*/
29+
public static function export(): void
30+
{
31+
self::$exporter->export();
32+
}
33+
}

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 init($model, array $columns, $filename): void;
11+
public function export(): void;
12+
}

0 commit comments

Comments
 (0)