Skip to content

Commit 5ce359c

Browse files
committed
merge conflict resolved
2 parents e5578b4 + 45e2e7a commit 5ce359c

File tree

5 files changed

+149
-27
lines changed

5 files changed

+149
-27
lines changed

README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ $columns = [
1919
'email'
2020
];
2121
22-
$exporter = new Export(
23-
User::query(), // Query Builder
24-
$columns
25-
);
22+
$users = User::query(); // Query builder
2623
27-
$exporter->export();
24+
Exporter::init($users, $columns, 'users.csv');
25+
Exporter::export();
2826
```
2927

28+
Or you can pass `Collection` or `Array`. But it is **highly recommended** to pass the **`Query Builder`** as it will use generator to save memory usage.
29+
3030
**For eloquent relation**
3131
```$xslt
3232
$columns = [
@@ -46,3 +46,43 @@ $exporter->export();
4646
```
4747

4848
Where `user` is the relation name, which is same as is in the `$columns` variable.
49+
50+
## Usage with raw PHP (PDOStatement)
51+
52+
Usage with PDO is straight forward. You **MUST** have to add the following code
53+
```$xslt
54+
$stmt->setFetchMode(PDO::FETCH_ASSOC); // N.B. must be included
55+
```
56+
57+
And then pass the `$stmt` to `Exporter`
58+
```$xslt
59+
$servername = "localhost";
60+
$username = "root";
61+
$password = "password";
62+
$dbname = "laravel";
63+
64+
$columns = [
65+
'id',
66+
'name',
67+
'email'
68+
];
69+
70+
try {
71+
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
72+
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
73+
$stmt = $conn->prepare("SELECT id, name, email FROM users");
74+
$stmt->execute();
75+
76+
// set the resulting array to associative
77+
$stmt->setFetchMode(PDO::FETCH_ASSOC);
78+
79+
Exporter::init($stmt, $userData, 'users.csv');
80+
Exporter::export();
81+
}
82+
catch(PDOException $e) {
83+
echo "Error: " . $e->getMessage();
84+
}
85+
$conn = null;
86+
```
87+
88+
## You are always welcome to contribute

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: 55 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
{
@@ -88,6 +86,7 @@ public function export()
8886

8987
/**
9088
* @return Generator
89+
* @throws Exception
9190
*/
9291
public function write()
9392
{
@@ -96,10 +95,40 @@ public function write()
9695

9796
fputcsv($file, $this->heading, $this->delimiter);
9897

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

102-
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+
}
103132
}
104133

105134
fclose($file);
@@ -118,7 +147,8 @@ public function getLine($data)
118147
$value = $this->getNestedData($data, $key, $k);
119148
array_push($line, $value);
120149
} else {
121-
array_push($line, $data->{$key});
150+
$value = is_array($data) ? $data[$key] : $data->{$key};
151+
array_push($line, $value);
122152
}
123153
}
124154

@@ -134,7 +164,11 @@ public function getLine($data)
134164
public function getNestedData($data, $keys, $k)
135165
{
136166
foreach ($keys as $kk => $key) {
137-
$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+
}
138172

139173
if (is_array($data)) {
140174
$this->getNestedData($data, $key, $kk);
@@ -153,9 +187,9 @@ protected function setDelimiter(string $delimiter): void
153187
}
154188

155189
/**
156-
* @param Builder $model
190+
* @param object | array $model
157191
*/
158-
protected function setModel(Builder $model): void
192+
protected function setModel($model): void
159193
{
160194
$this->model = $model;
161195
}

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)